diff --git a/homeassistant/components/poolsense/__init__.py b/homeassistant/components/poolsense/__init__.py index 5ec1cb475b5..134be1cefba 100644 --- a/homeassistant/components/poolsense/__init__.py +++ b/homeassistant/components/poolsense/__init__.py @@ -7,16 +7,17 @@ from poolsense import PoolSense from poolsense.exceptions import PoolSenseError from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_EMAIL, CONF_PASSWORD +from homeassistant.const import ATTR_ATTRIBUTION, CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client +from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, UpdateFailed, ) -from .const import DOMAIN +from .const import ATTRIBUTION, DOMAIN PLATFORMS = ["sensor", "binary_sensor"] @@ -61,16 +62,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): class PoolSenseEntity(CoordinatorEntity): """Implements a common class elements representing the PoolSense component.""" - def __init__(self, coordinator, email, info_type): + _attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION} + + def __init__(self, coordinator, email, description: EntityDescription): """Initialize poolsense sensor.""" super().__init__(coordinator) - self._unique_id = f"{email}-{info_type}" - self.info_type = info_type - - @property - def unique_id(self): - """Return a unique id.""" - return self._unique_id + self.entity_description = description + self._attr_name = f"PoolSense {description.name}" + self._attr_unique_id = f"{email}-{description.key}" class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator): diff --git a/homeassistant/components/poolsense/binary_sensor.py b/homeassistant/components/poolsense/binary_sensor.py index ea07f1637a6..1b45ee15f0f 100644 --- a/homeassistant/components/poolsense/binary_sensor.py +++ b/homeassistant/components/poolsense/binary_sensor.py @@ -1,42 +1,40 @@ """Support for PoolSense binary sensors.""" +from __future__ import annotations + from homeassistant.components.binary_sensor import ( DEVICE_CLASS_PROBLEM, BinarySensorEntity, + BinarySensorEntityDescription, ) from homeassistant.const import CONF_EMAIL from . import PoolSenseEntity from .const import DOMAIN -BINARY_SENSORS = { - "pH Status": { - "unit": None, - "icon": None, - "name": "pH Status", - "device_class": DEVICE_CLASS_PROBLEM, - }, - "Chlorine Status": { - "unit": None, - "icon": None, - "name": "Chlorine Status", - "device_class": DEVICE_CLASS_PROBLEM, - }, -} +BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = ( + BinarySensorEntityDescription( + key="pH Status", + name="pH Status", + device_class=DEVICE_CLASS_PROBLEM, + ), + BinarySensorEntityDescription( + key="Chlorine Status", + name="Chlorine Status", + device_class=DEVICE_CLASS_PROBLEM, + ), +) async def async_setup_entry(hass, config_entry, async_add_entities): """Defer sensor setup to the shared sensor module.""" coordinator = hass.data[DOMAIN][config_entry.entry_id] - binary_sensors_list = [] - for binary_sensor in BINARY_SENSORS: - binary_sensors_list.append( - PoolSenseBinarySensor( - coordinator, config_entry.data[CONF_EMAIL], binary_sensor - ) - ) + entities = [ + PoolSenseBinarySensor(coordinator, config_entry.data[CONF_EMAIL], description) + for description in BINARY_SENSOR_TYPES + ] - async_add_entities(binary_sensors_list, False) + async_add_entities(entities, False) class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity): @@ -45,19 +43,4 @@ class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity): @property def is_on(self): """Return true if the binary sensor is on.""" - return self.coordinator.data[self.info_type] == "red" - - @property - def icon(self): - """Return the icon.""" - return BINARY_SENSORS[self.info_type]["icon"] - - @property - def device_class(self): - """Return the class of this device.""" - return BINARY_SENSORS[self.info_type]["device_class"] - - @property - def name(self): - """Return the name of the binary sensor.""" - return f"PoolSense {BINARY_SENSORS[self.info_type]['name']}" + return self.coordinator.data[self.entity_description.key] == "red" diff --git a/homeassistant/components/poolsense/sensor.py b/homeassistant/components/poolsense/sensor.py index e9aeaca20f5..82df8b4d208 100644 --- a/homeassistant/components/poolsense/sensor.py +++ b/homeassistant/components/poolsense/sensor.py @@ -1,7 +1,8 @@ """Sensor platform for the PoolSense sensor.""" -from homeassistant.components.sensor import SensorEntity +from __future__ import annotations + +from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.const import ( - ATTR_ATTRIBUTION, CONF_EMAIL, DEVICE_CLASS_BATTERY, DEVICE_CLASS_TEMPERATURE, @@ -12,103 +13,80 @@ from homeassistant.const import ( ) from . import PoolSenseEntity -from .const import ATTRIBUTION, DOMAIN +from .const import DOMAIN -SENSORS = { - "Chlorine": { - "unit": ELECTRIC_POTENTIAL_MILLIVOLT, - "icon": "mdi:pool", - "name": "Chlorine", - "device_class": None, - }, - "pH": {"unit": None, "icon": "mdi:pool", "name": "pH", "device_class": None}, - "Battery": { - "unit": PERCENTAGE, - "icon": None, - "name": "Battery", - "device_class": DEVICE_CLASS_BATTERY, - }, - "Water Temp": { - "unit": TEMP_CELSIUS, - "icon": "mdi:coolant-temperature", - "name": "Temperature", - "device_class": DEVICE_CLASS_TEMPERATURE, - }, - "Last Seen": { - "unit": None, - "icon": "mdi:clock", - "name": "Last Seen", - "device_class": DEVICE_CLASS_TIMESTAMP, - }, - "Chlorine High": { - "unit": ELECTRIC_POTENTIAL_MILLIVOLT, - "icon": "mdi:pool", - "name": "Chlorine High", - "device_class": None, - }, - "Chlorine Low": { - "unit": ELECTRIC_POTENTIAL_MILLIVOLT, - "icon": "mdi:pool", - "name": "Chlorine Low", - "device_class": None, - }, - "pH High": { - "unit": None, - "icon": "mdi:pool", - "name": "pH High", - "device_class": None, - }, - "pH Low": { - "unit": None, - "icon": "mdi:pool", - "name": "pH Low", - "device_class": None, - }, -} +SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key="Chlorine", + native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT, + icon="mdi:pool", + name="Chlorine", + ), + SensorEntityDescription( + key="pH", + icon="mdi:pool", + name="pH", + ), + SensorEntityDescription( + key="Battery", + native_unit_of_measurement=PERCENTAGE, + name="Battery", + device_class=DEVICE_CLASS_BATTERY, + ), + SensorEntityDescription( + key="Water Temp", + native_unit_of_measurement=TEMP_CELSIUS, + icon="mdi:coolant-temperature", + name="Temperature", + device_class=DEVICE_CLASS_TEMPERATURE, + ), + SensorEntityDescription( + key="Last Seen", + icon="mdi:clock", + name="Last Seen", + device_class=DEVICE_CLASS_TIMESTAMP, + ), + SensorEntityDescription( + key="Chlorine High", + native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT, + icon="mdi:pool", + name="Chlorine High", + ), + SensorEntityDescription( + key="Chlorine Low", + native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT, + icon="mdi:pool", + name="Chlorine Low", + ), + SensorEntityDescription( + key="pH High", + icon="mdi:pool", + name="pH High", + ), + SensorEntityDescription( + key="pH Low", + icon="mdi:pool", + name="pH Low", + ), +) async def async_setup_entry(hass, config_entry, async_add_entities): """Defer sensor setup to the shared sensor module.""" coordinator = hass.data[DOMAIN][config_entry.entry_id] - sensors_list = [] - for sensor in SENSORS: - sensors_list.append( - PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], sensor) - ) + entities = [ + PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], description) + for description in SENSOR_TYPES + ] - async_add_entities(sensors_list, False) + async_add_entities(entities, False) class PoolSenseSensor(PoolSenseEntity, SensorEntity): """Sensor representing poolsense data.""" - @property - def name(self): - """Return the name of the particular component.""" - return f"PoolSense {SENSORS[self.info_type]['name']}" - @property def native_value(self): """State of the sensor.""" - return self.coordinator.data[self.info_type] - - @property - def device_class(self): - """Return the device class.""" - return SENSORS[self.info_type]["device_class"] - - @property - def icon(self): - """Return the icon.""" - return SENSORS[self.info_type]["icon"] - - @property - def native_unit_of_measurement(self): - """Return unit of measurement.""" - return SENSORS[self.info_type]["unit"] - - @property - def extra_state_attributes(self): - """Return device attributes.""" - return {ATTR_ATTRIBUTION: ATTRIBUTION} + return self.coordinator.data[self.entity_description.key]