Use EntityDescription - poolsense (#55743)

This commit is contained in:
Marc Mueller 2021-09-06 11:58:47 +02:00 committed by GitHub
parent 364edbfd8a
commit 3001df99cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 134 deletions

View File

@ -7,16 +7,17 @@ from poolsense import PoolSense
from poolsense.exceptions import PoolSenseError from poolsense.exceptions import PoolSenseError
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
UpdateFailed, UpdateFailed,
) )
from .const import DOMAIN from .const import ATTRIBUTION, DOMAIN
PLATFORMS = ["sensor", "binary_sensor"] PLATFORMS = ["sensor", "binary_sensor"]
@ -61,16 +62,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
class PoolSenseEntity(CoordinatorEntity): class PoolSenseEntity(CoordinatorEntity):
"""Implements a common class elements representing the PoolSense component.""" """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.""" """Initialize poolsense sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._unique_id = f"{email}-{info_type}" self.entity_description = description
self.info_type = info_type self._attr_name = f"PoolSense {description.name}"
self._attr_unique_id = f"{email}-{description.key}"
@property
def unique_id(self):
"""Return a unique id."""
return self._unique_id
class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator): class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator):

View File

@ -1,42 +1,40 @@
"""Support for PoolSense binary sensors.""" """Support for PoolSense binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_PROBLEM, DEVICE_CLASS_PROBLEM,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription,
) )
from homeassistant.const import CONF_EMAIL from homeassistant.const import CONF_EMAIL
from . import PoolSenseEntity from . import PoolSenseEntity
from .const import DOMAIN from .const import DOMAIN
BINARY_SENSORS = { BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
"pH Status": { BinarySensorEntityDescription(
"unit": None, key="pH Status",
"icon": None, name="pH Status",
"name": "pH Status", device_class=DEVICE_CLASS_PROBLEM,
"device_class": DEVICE_CLASS_PROBLEM, ),
}, BinarySensorEntityDescription(
"Chlorine Status": { key="Chlorine Status",
"unit": None, name="Chlorine Status",
"icon": None, device_class=DEVICE_CLASS_PROBLEM,
"name": "Chlorine Status", ),
"device_class": DEVICE_CLASS_PROBLEM, )
},
}
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Defer sensor setup to the shared sensor module.""" """Defer sensor setup to the shared sensor module."""
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
binary_sensors_list = [] entities = [
for binary_sensor in BINARY_SENSORS: PoolSenseBinarySensor(coordinator, config_entry.data[CONF_EMAIL], description)
binary_sensors_list.append( for description in BINARY_SENSOR_TYPES
PoolSenseBinarySensor( ]
coordinator, config_entry.data[CONF_EMAIL], binary_sensor
)
)
async_add_entities(binary_sensors_list, False) async_add_entities(entities, False)
class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity): class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity):
@ -45,19 +43,4 @@ class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity):
@property @property
def is_on(self): def is_on(self):
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self.coordinator.data[self.info_type] == "red" return self.coordinator.data[self.entity_description.key] == "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']}"

View File

@ -1,7 +1,8 @@
"""Sensor platform for the PoolSense sensor.""" """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 ( from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_EMAIL, CONF_EMAIL,
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
@ -12,103 +13,80 @@ from homeassistant.const import (
) )
from . import PoolSenseEntity from . import PoolSenseEntity
from .const import ATTRIBUTION, DOMAIN from .const import DOMAIN
SENSORS = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
"Chlorine": { SensorEntityDescription(
"unit": ELECTRIC_POTENTIAL_MILLIVOLT, key="Chlorine",
"icon": "mdi:pool", native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
"name": "Chlorine", icon="mdi:pool",
"device_class": None, name="Chlorine",
}, ),
"pH": {"unit": None, "icon": "mdi:pool", "name": "pH", "device_class": None}, SensorEntityDescription(
"Battery": { key="pH",
"unit": PERCENTAGE, icon="mdi:pool",
"icon": None, name="pH",
"name": "Battery", ),
"device_class": DEVICE_CLASS_BATTERY, SensorEntityDescription(
}, key="Battery",
"Water Temp": { native_unit_of_measurement=PERCENTAGE,
"unit": TEMP_CELSIUS, name="Battery",
"icon": "mdi:coolant-temperature", device_class=DEVICE_CLASS_BATTERY,
"name": "Temperature", ),
"device_class": DEVICE_CLASS_TEMPERATURE, SensorEntityDescription(
}, key="Water Temp",
"Last Seen": { native_unit_of_measurement=TEMP_CELSIUS,
"unit": None, icon="mdi:coolant-temperature",
"icon": "mdi:clock", name="Temperature",
"name": "Last Seen", device_class=DEVICE_CLASS_TEMPERATURE,
"device_class": DEVICE_CLASS_TIMESTAMP, ),
}, SensorEntityDescription(
"Chlorine High": { key="Last Seen",
"unit": ELECTRIC_POTENTIAL_MILLIVOLT, icon="mdi:clock",
"icon": "mdi:pool", name="Last Seen",
"name": "Chlorine High", device_class=DEVICE_CLASS_TIMESTAMP,
"device_class": None, ),
}, SensorEntityDescription(
"Chlorine Low": { key="Chlorine High",
"unit": ELECTRIC_POTENTIAL_MILLIVOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
"icon": "mdi:pool", icon="mdi:pool",
"name": "Chlorine Low", name="Chlorine High",
"device_class": None, ),
}, SensorEntityDescription(
"pH High": { key="Chlorine Low",
"unit": None, native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
"icon": "mdi:pool", icon="mdi:pool",
"name": "pH High", name="Chlorine Low",
"device_class": None, ),
}, SensorEntityDescription(
"pH Low": { key="pH High",
"unit": None, icon="mdi:pool",
"icon": "mdi:pool", name="pH High",
"name": "pH Low", ),
"device_class": None, SensorEntityDescription(
}, key="pH Low",
} icon="mdi:pool",
name="pH Low",
),
)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Defer sensor setup to the shared sensor module.""" """Defer sensor setup to the shared sensor module."""
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
sensors_list = [] entities = [
for sensor in SENSORS: PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], description)
sensors_list.append( for description in SENSOR_TYPES
PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], sensor) ]
)
async_add_entities(sensors_list, False) async_add_entities(entities, False)
class PoolSenseSensor(PoolSenseEntity, SensorEntity): class PoolSenseSensor(PoolSenseEntity, SensorEntity):
"""Sensor representing poolsense data.""" """Sensor representing poolsense data."""
@property
def name(self):
"""Return the name of the particular component."""
return f"PoolSense {SENSORS[self.info_type]['name']}"
@property @property
def native_value(self): def native_value(self):
"""State of the sensor.""" """State of the sensor."""
return self.coordinator.data[self.info_type] return self.coordinator.data[self.entity_description.key]
@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}