Use EntityDescription - aqualogic (#55791)
This commit is contained in:
parent
3001df99cb
commit
4475cf24c8
1 changed files with 105 additions and 43 deletions
|
@ -1,8 +1,15 @@
|
|||
"""Support for AquaLogic sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
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 (
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
|
@ -16,40 +23,88 @@ import homeassistant.helpers.config_validation as cv
|
|||
|
||||
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 ]
|
||||
# sensor_type corresponds to property names in aqualogic.core.AquaLogic
|
||||
SENSOR_TYPES = {
|
||||
"air_temp": ["Air Temperature", TEMP_UNITS, None, DEVICE_CLASS_TEMPERATURE],
|
||||
"pool_temp": [
|
||||
"Pool Temperature",
|
||||
TEMP_UNITS,
|
||||
"mdi:oil-temperature",
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
],
|
||||
"spa_temp": [
|
||||
"Spa Temperature",
|
||||
TEMP_UNITS,
|
||||
"mdi:oil-temperature",
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
],
|
||||
"pool_chlorinator": ["Pool Chlorinator", PERCENT_UNITS, "mdi:gauge", None],
|
||||
"spa_chlorinator": ["Spa Chlorinator", PERCENT_UNITS, "mdi:gauge", None],
|
||||
"salt_level": ["Salt Level", SALT_UNITS, "mdi:gauge", None],
|
||||
"pump_speed": ["Pump Speed", PERCENT_UNITS, "mdi:speedometer", None],
|
||||
"pump_power": ["Pump Power", WATT_UNITS, "mdi:gauge", None],
|
||||
"status": ["Status", NO_UNITS, "mdi:alert", None],
|
||||
}
|
||||
@dataclass
|
||||
class AquaLogicSensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes AquaLogic sensor entity."""
|
||||
|
||||
unit_metric: str | None = None
|
||||
unit_imperial: str | None = None
|
||||
|
||||
|
||||
# keys correspond to property names in aqualogic.core.AquaLogic
|
||||
SENSOR_TYPES: tuple[AquaLogicSensorEntityDescription, ...] = (
|
||||
AquaLogicSensorEntityDescription(
|
||||
key="air_temp",
|
||||
name="Air Temperature",
|
||||
unit_metric=TEMP_CELSIUS,
|
||||
unit_imperial=TEMP_FAHRENHEIT,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
AquaLogicSensorEntityDescription(
|
||||
key="pool_temp",
|
||||
name="Pool Temperature",
|
||||
unit_metric=TEMP_CELSIUS,
|
||||
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(
|
||||
{
|
||||
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
|
||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
||||
vol.Required(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
|
||||
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):
|
||||
"""Set up the sensor platform."""
|
||||
sensors = []
|
||||
|
||||
processor = hass.data[DOMAIN]
|
||||
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
||||
sensors.append(AquaLogicSensor(processor, sensor_type))
|
||||
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||
|
||||
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):
|
||||
"""Sensor implementation for the AquaLogic component."""
|
||||
|
||||
entity_description: AquaLogicSensorEntityDescription
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(self, processor, sensor_type):
|
||||
def __init__(self, processor, description: AquaLogicSensorEntityDescription):
|
||||
"""Initialize sensor."""
|
||||
self.entity_description = description
|
||||
self._processor = processor
|
||||
self._type = sensor_type
|
||||
self._attr_name = f"AquaLogic {SENSOR_TYPES[sensor_type][0]}"
|
||||
self._attr_icon = SENSOR_TYPES[sensor_type][2]
|
||||
self._attr_name = f"AquaLogic {description.name}"
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
|
@ -92,11 +150,15 @@ class AquaLogicSensor(SensorEntity):
|
|||
panel = self._processor.panel
|
||||
if panel is not None:
|
||||
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:
|
||||
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()
|
||||
else:
|
||||
self._attr_native_unit_of_measurement = None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue