Use entity class vars in Flo (#50991)

This commit is contained in:
Franck Nijhof 2021-05-26 18:47:04 +02:00 committed by GitHub
parent daff62f42d
commit 67536b52c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 92 deletions

View File

@ -37,6 +37,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity): class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity):
"""Binary sensor that reports on if there are any pending system alerts.""" """Binary sensor that reports on if there are any pending system alerts."""
_attr_device_class = DEVICE_CLASS_PROBLEM
def __init__(self, device): def __init__(self, device):
"""Initialize the pending alerts binary sensor.""" """Initialize the pending alerts binary sensor."""
super().__init__("pending_system_alerts", "Pending System Alerts", device) super().__init__("pending_system_alerts", "Pending System Alerts", device)
@ -57,15 +59,12 @@ class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity):
"critical": self._device.pending_critical_alerts_count, "critical": self._device.pending_critical_alerts_count,
} }
@property
def device_class(self):
"""Return the device class for the binary sensor."""
return DEVICE_CLASS_PROBLEM
class FloWaterDetectedBinarySensor(FloEntity, BinarySensorEntity): class FloWaterDetectedBinarySensor(FloEntity, BinarySensorEntity):
"""Binary sensor that reports if water is detected (for leak detectors).""" """Binary sensor that reports if water is detected (for leak detectors)."""
_attr_device_class = DEVICE_CLASS_PROBLEM
def __init__(self, device): def __init__(self, device):
"""Initialize the pending alerts binary sensor.""" """Initialize the pending alerts binary sensor."""
super().__init__("water_detected", "Water Detected", device) super().__init__("water_detected", "Water Detected", device)
@ -74,8 +73,3 @@ class FloWaterDetectedBinarySensor(FloEntity, BinarySensorEntity):
def is_on(self): def is_on(self):
"""Return true if the Flo device is detecting water.""" """Return true if the Flo device is detecting water."""
return self._device.water_detected return self._device.water_detected
@property
def device_class(self):
"""Return the device class for the binary sensor."""
return DEVICE_CLASS_PROBLEM

View File

@ -13,6 +13,9 @@ from .device import FloDeviceDataUpdateCoordinator
class FloEntity(Entity): class FloEntity(Entity):
"""A base class for Flo entities.""" """A base class for Flo entities."""
_attr_force_update = False
_attr_should_poll = False
def __init__( def __init__(
self, self,
entity_type: str, entity_type: str,
@ -21,21 +24,12 @@ class FloEntity(Entity):
**kwargs, **kwargs,
) -> None: ) -> None:
"""Init Flo entity.""" """Init Flo entity."""
self._unique_id: str = f"{device.mac_address}_{entity_type}" self._attr_name = name
self._name: str = name self._attr_unique_id = f"{device.mac_address}_{entity_type}"
self._device: FloDeviceDataUpdateCoordinator = device self._device: FloDeviceDataUpdateCoordinator = device
self._state: Any = None self._state: Any = None
@property
def name(self) -> str:
"""Return Entity's default name."""
return self._name
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id
@property @property
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:
"""Return a device description for device registry.""" """Return a device description for device registry."""
@ -53,16 +47,6 @@ class FloEntity(Entity):
"""Return True if device is available.""" """Return True if device is available."""
return self._device.available return self._device.available
@property
def force_update(self) -> bool:
"""Force update this entity."""
return False
@property
def should_poll(self) -> bool:
"""Poll state from device."""
return False
async def async_update(self): async def async_update(self):
"""Update Flo entity.""" """Update Flo entity."""
await self._device.async_request_refresh() await self._device.async_request_refresh()

View File

@ -60,16 +60,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class FloDailyUsageSensor(FloEntity, SensorEntity): class FloDailyUsageSensor(FloEntity, SensorEntity):
"""Monitors the daily water usage.""" """Monitors the daily water usage."""
_attr_icon = WATER_ICON
_attr_unit_of_measurement = VOLUME_GALLONS
def __init__(self, device): def __init__(self, device):
"""Initialize the daily water usage sensor.""" """Initialize the daily water usage sensor."""
super().__init__("daily_consumption", NAME_DAILY_USAGE, device) super().__init__("daily_consumption", NAME_DAILY_USAGE, device)
self._state: float = None self._state: float = None
@property
def icon(self) -> str:
"""Return the daily usage icon."""
return WATER_ICON
@property @property
def state(self) -> float | None: def state(self) -> float | None:
"""Return the current daily usage.""" """Return the current daily usage."""
@ -77,11 +75,6 @@ class FloDailyUsageSensor(FloEntity, SensorEntity):
return None return None
return round(self._device.consumption_today, 1) return round(self._device.consumption_today, 1)
@property
def unit_of_measurement(self) -> str:
"""Return gallons as the unit measurement for water."""
return VOLUME_GALLONS
class FloSystemModeSensor(FloEntity, SensorEntity): class FloSystemModeSensor(FloEntity, SensorEntity):
"""Monitors the current Flo system mode.""" """Monitors the current Flo system mode."""
@ -102,16 +95,14 @@ class FloSystemModeSensor(FloEntity, SensorEntity):
class FloCurrentFlowRateSensor(FloEntity, SensorEntity): class FloCurrentFlowRateSensor(FloEntity, SensorEntity):
"""Monitors the current water flow rate.""" """Monitors the current water flow rate."""
_attr_icon = GAUGE_ICON
_attr_unit_of_measurement = "gpm"
def __init__(self, device): def __init__(self, device):
"""Initialize the flow rate sensor.""" """Initialize the flow rate sensor."""
super().__init__("current_flow_rate", NAME_FLOW_RATE, device) super().__init__("current_flow_rate", NAME_FLOW_RATE, device)
self._state: float = None self._state: float = None
@property
def icon(self) -> str:
"""Return the daily usage icon."""
return GAUGE_ICON
@property @property
def state(self) -> float | None: def state(self) -> float | None:
"""Return the current flow rate.""" """Return the current flow rate."""
@ -119,15 +110,13 @@ class FloCurrentFlowRateSensor(FloEntity, SensorEntity):
return None return None
return round(self._device.current_flow_rate, 1) return round(self._device.current_flow_rate, 1)
@property
def unit_of_measurement(self) -> str:
"""Return the unit measurement."""
return "gpm"
class FloTemperatureSensor(FloEntity, SensorEntity): class FloTemperatureSensor(FloEntity, SensorEntity):
"""Monitors the temperature.""" """Monitors the temperature."""
_attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_unit_of_measurement = TEMP_FAHRENHEIT
def __init__(self, name, device): def __init__(self, name, device):
"""Initialize the temperature sensor.""" """Initialize the temperature sensor."""
super().__init__("temperature", name, device) super().__init__("temperature", name, device)
@ -140,20 +129,13 @@ class FloTemperatureSensor(FloEntity, SensorEntity):
return None return None
return round(self._device.temperature, 1) return round(self._device.temperature, 1)
@property
def unit_of_measurement(self) -> str:
"""Return fahrenheit as the unit measurement for temperature."""
return TEMP_FAHRENHEIT
@property
def device_class(self) -> str | None:
"""Return the device class for this sensor."""
return DEVICE_CLASS_TEMPERATURE
class FloHumiditySensor(FloEntity, SensorEntity): class FloHumiditySensor(FloEntity, SensorEntity):
"""Monitors the humidity.""" """Monitors the humidity."""
_attr_device_class = DEVICE_CLASS_HUMIDITY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, device): def __init__(self, device):
"""Initialize the humidity sensor.""" """Initialize the humidity sensor."""
super().__init__("humidity", NAME_HUMIDITY, device) super().__init__("humidity", NAME_HUMIDITY, device)
@ -166,20 +148,13 @@ class FloHumiditySensor(FloEntity, SensorEntity):
return None return None
return round(self._device.humidity, 1) return round(self._device.humidity, 1)
@property
def unit_of_measurement(self) -> str:
"""Return percent as the unit measurement for humidity."""
return PERCENTAGE
@property
def device_class(self) -> str | None:
"""Return the device class for this sensor."""
return DEVICE_CLASS_HUMIDITY
class FloPressureSensor(FloEntity, SensorEntity): class FloPressureSensor(FloEntity, SensorEntity):
"""Monitors the water pressure.""" """Monitors the water pressure."""
_attr_device_class = DEVICE_CLASS_PRESSURE
_attr_unit_of_measurement = PRESSURE_PSI
def __init__(self, device): def __init__(self, device):
"""Initialize the pressure sensor.""" """Initialize the pressure sensor."""
super().__init__("water_pressure", NAME_WATER_PRESSURE, device) super().__init__("water_pressure", NAME_WATER_PRESSURE, device)
@ -192,20 +167,13 @@ class FloPressureSensor(FloEntity, SensorEntity):
return None return None
return round(self._device.current_psi, 1) return round(self._device.current_psi, 1)
@property
def unit_of_measurement(self) -> str:
"""Return gallons as the unit measurement for water."""
return PRESSURE_PSI
@property
def device_class(self) -> str | None:
"""Return the device class for this sensor."""
return DEVICE_CLASS_PRESSURE
class FloBatterySensor(FloEntity, SensorEntity): class FloBatterySensor(FloEntity, SensorEntity):
"""Monitors the battery level for battery-powered leak detectors.""" """Monitors the battery level for battery-powered leak detectors."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, device): def __init__(self, device):
"""Initialize the battery sensor.""" """Initialize the battery sensor."""
super().__init__("battery", NAME_BATTERY, device) super().__init__("battery", NAME_BATTERY, device)
@ -215,13 +183,3 @@ class FloBatterySensor(FloEntity, SensorEntity):
def state(self) -> float | None: def state(self) -> float | None:
"""Return the current battery level.""" """Return the current battery level."""
return self._device.battery_level return self._device.battery_level
@property
def unit_of_measurement(self) -> str:
"""Return percentage as the unit measurement for battery."""
return PERCENTAGE
@property
def device_class(self) -> str | None:
"""Return the device class for this sensor."""
return DEVICE_CLASS_BATTERY