Use EntityDescription - aqualogic (#55791)

This commit is contained in:
Marc Mueller 2021-09-06 11:59:03 +02:00 committed by GitHub
parent 3001df99cb
commit 4475cf24c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,15 @@
"""Support for AquaLogic sensors.""" """Support for AquaLogic sensors."""
from __future__ import annotations
from dataclasses import dataclass
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_MONITORED_CONDITIONS, CONF_MONITORED_CONDITIONS,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
@ -16,40 +23,88 @@ import homeassistant.helpers.config_validation as cv
from . import DOMAIN, UPDATE_TOPIC from . import DOMAIN, UPDATE_TOPIC
TEMP_UNITS = [TEMP_CELSIUS, TEMP_FAHRENHEIT]
PERCENT_UNITS = [PERCENTAGE, PERCENTAGE]
SALT_UNITS = ["g/L", "PPM"]
WATT_UNITS = [POWER_WATT, POWER_WATT]
NO_UNITS = [None, None]
# sensor_type [ description, unit, icon, device_class ] @dataclass
# sensor_type corresponds to property names in aqualogic.core.AquaLogic class AquaLogicSensorEntityDescription(SensorEntityDescription):
SENSOR_TYPES = { """Describes AquaLogic sensor entity."""
"air_temp": ["Air Temperature", TEMP_UNITS, None, DEVICE_CLASS_TEMPERATURE],
"pool_temp": [ unit_metric: str | None = None
"Pool Temperature", unit_imperial: str | None = None
TEMP_UNITS,
"mdi:oil-temperature",
DEVICE_CLASS_TEMPERATURE, # keys correspond to property names in aqualogic.core.AquaLogic
], SENSOR_TYPES: tuple[AquaLogicSensorEntityDescription, ...] = (
"spa_temp": [ AquaLogicSensorEntityDescription(
"Spa Temperature", key="air_temp",
TEMP_UNITS, name="Air Temperature",
"mdi:oil-temperature", unit_metric=TEMP_CELSIUS,
DEVICE_CLASS_TEMPERATURE, unit_imperial=TEMP_FAHRENHEIT,
], device_class=DEVICE_CLASS_TEMPERATURE,
"pool_chlorinator": ["Pool Chlorinator", PERCENT_UNITS, "mdi:gauge", None], ),
"spa_chlorinator": ["Spa Chlorinator", PERCENT_UNITS, "mdi:gauge", None], AquaLogicSensorEntityDescription(
"salt_level": ["Salt Level", SALT_UNITS, "mdi:gauge", None], key="pool_temp",
"pump_speed": ["Pump Speed", PERCENT_UNITS, "mdi:speedometer", None], name="Pool Temperature",
"pump_power": ["Pump Power", WATT_UNITS, "mdi:gauge", None], unit_metric=TEMP_CELSIUS,
"status": ["Status", NO_UNITS, "mdi:alert", None], unit_imperial=TEMP_FAHRENHEIT,
} icon="mdi:oil-temperature",
device_class=DEVICE_CLASS_TEMPERATURE,
),
AquaLogicSensorEntityDescription(
key="spa_temp",
name="Spa Temperature",
unit_metric=TEMP_CELSIUS,
unit_imperial=TEMP_FAHRENHEIT,
icon="mdi:oil-temperature",
device_class=DEVICE_CLASS_TEMPERATURE,
),
AquaLogicSensorEntityDescription(
key="pool_chlorinator",
name="Pool Chlorinator",
unit_metric=PERCENTAGE,
unit_imperial=PERCENTAGE,
icon="mdi:gauge",
),
AquaLogicSensorEntityDescription(
key="spa_chlorinator",
name="Spa Chlorinator",
unit_metric=PERCENTAGE,
unit_imperial=PERCENTAGE,
icon="mdi:gauge",
),
AquaLogicSensorEntityDescription(
key="salt_level",
name="Salt Level",
unit_metric="g/L",
unit_imperial="PPM",
icon="mdi:gauge",
),
AquaLogicSensorEntityDescription(
key="pump_speed",
name="Pump Speed",
unit_metric=PERCENTAGE,
unit_imperial=PERCENTAGE,
icon="mdi:speedometer",
),
AquaLogicSensorEntityDescription(
key="pump_power",
name="Pump Power",
unit_metric=POWER_WATT,
unit_imperial=POWER_WATT,
icon="mdi:gauge",
),
AquaLogicSensorEntityDescription(
key="status",
name="Status",
icon="mdi:alert",
),
)
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All( vol.Required(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)] cv.ensure_list, [vol.In(SENSOR_KEYS)]
) )
} }
) )
@ -57,26 +112,29 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the sensor platform.""" """Set up the sensor platform."""
sensors = []
processor = hass.data[DOMAIN] processor = hass.data[DOMAIN]
for sensor_type in config[CONF_MONITORED_CONDITIONS]: monitored_conditions = config[CONF_MONITORED_CONDITIONS]
sensors.append(AquaLogicSensor(processor, sensor_type))
async_add_entities(sensors) entities = [
AquaLogicSensor(processor, description)
for description in SENSOR_TYPES
if description.key in monitored_conditions
]
async_add_entities(entities)
class AquaLogicSensor(SensorEntity): class AquaLogicSensor(SensorEntity):
"""Sensor implementation for the AquaLogic component.""" """Sensor implementation for the AquaLogic component."""
entity_description: AquaLogicSensorEntityDescription
_attr_should_poll = False _attr_should_poll = False
def __init__(self, processor, sensor_type): def __init__(self, processor, description: AquaLogicSensorEntityDescription):
"""Initialize sensor.""" """Initialize sensor."""
self.entity_description = description
self._processor = processor self._processor = processor
self._type = sensor_type self._attr_name = f"AquaLogic {description.name}"
self._attr_name = f"AquaLogic {SENSOR_TYPES[sensor_type][0]}"
self._attr_icon = SENSOR_TYPES[sensor_type][2]
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
@ -92,11 +150,15 @@ class AquaLogicSensor(SensorEntity):
panel = self._processor.panel panel = self._processor.panel
if panel is not None: if panel is not None:
if panel.is_metric: if panel.is_metric:
self._attr_native_unit_of_measurement = SENSOR_TYPES[self._type][1][0] self._attr_native_unit_of_measurement = (
self.entity_description.unit_metric
)
else: else:
self._attr_native_unit_of_measurement = SENSOR_TYPES[self._type][1][1] self._attr_native_unit_of_measurement = (
self.entity_description.unit_imperial
)
self._attr_native_value = getattr(panel, self._type) self._attr_native_value = getattr(panel, self.entity_description.key)
self.async_write_ha_state() self.async_write_ha_state()
else: else:
self._attr_native_unit_of_measurement = None self._attr_native_unit_of_measurement = None