Use entity descriptions classes in DSMR (#53549)

This commit is contained in:
Franck Nijhof 2021-07-27 14:44:58 +02:00 committed by GitHub
parent b51c666817
commit 27e69037d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 101 deletions

View file

@ -42,7 +42,7 @@ from .const import (
LOGGER,
SENSORS,
)
from .models import DSMRSensor
from .models import DSMRSensorEntityDescription
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
@ -83,10 +83,13 @@ async def async_setup_entry(
"""Set up the DSMR sensor."""
dsmr_version = entry.data[CONF_DSMR_VERSION]
entities = [
DSMREntity(sensor, entry)
for sensor in SENSORS
if (sensor.dsmr_versions is None or dsmr_version in sensor.dsmr_versions)
and (not sensor.is_gas or CONF_SERIAL_ID_GAS in entry.data)
DSMREntity(description, entry)
for description in SENSORS
if (
description.dsmr_versions is None
or dsmr_version in description.dsmr_versions
)
and (not description.is_gas or CONF_SERIAL_ID_GAS in entry.data)
]
async_add_entities(entities)
@ -184,50 +187,46 @@ async def async_setup_entry(
class DSMREntity(SensorEntity):
"""Entity reading values from DSMR telegram."""
entity_description: DSMRSensorEntityDescription
_attr_should_poll = False
def __init__(self, sensor: DSMRSensor, entry: ConfigEntry) -> None:
def __init__(
self, entity_description: DSMRSensorEntityDescription, entry: ConfigEntry
) -> None:
"""Initialize entity."""
self._sensor = sensor
self.entity_description = entity_description
self._entry = entry
self.telegram: dict[str, DSMRObject] = {}
device_serial = entry.data[CONF_SERIAL_ID]
device_name = DEVICE_NAME_ENERGY
if sensor.is_gas:
if entity_description.is_gas:
device_serial = entry.data[CONF_SERIAL_ID_GAS]
device_name = DEVICE_NAME_GAS
self._attr_device_class = sensor.device_class
self._attr_device_info = {
"identifiers": {(DOMAIN, device_serial)},
"name": device_name,
}
self._attr_entity_registry_enabled_default = (
sensor.entity_registry_enabled_default
self._attr_unique_id = f"{device_serial}_{entity_description.name}".replace(
" ", "_"
)
self._attr_force_update = sensor.force_update
self._attr_icon = sensor.icon
self._attr_last_reset = sensor.last_reset
self._attr_name = sensor.name
self._attr_state_class = sensor.state_class
self._attr_unique_id = f"{device_serial}_{sensor.name}".replace(" ", "_")
@callback
def update_data(self, telegram: dict[str, DSMRObject]) -> None:
"""Update data."""
self.telegram = telegram
if self.hass and self._sensor.obis_reference in self.telegram:
if self.hass and self.entity_description.key in self.telegram:
self.async_write_ha_state()
def get_dsmr_object_attr(self, attribute: str) -> str | None:
"""Read attribute from last received telegram for this DSMR object."""
# Make sure telegram contains an object for this entities obis
if self._sensor.obis_reference not in self.telegram:
if self.entity_description.key not in self.telegram:
return None
# Get the attribute value if the object has it
dsmr_object = self.telegram[self._sensor.obis_reference]
dsmr_object = self.telegram[self.entity_description.key]
attr: str | None = getattr(dsmr_object, attribute)
return attr
@ -238,7 +237,7 @@ class DSMREntity(SensorEntity):
if value is None:
return None
if self._sensor.obis_reference == obis_ref.ELECTRICITY_ACTIVE_TARIFF:
if self.entity_description.key == obis_ref.ELECTRICITY_ACTIVE_TARIFF:
return self.translate_tariff(value, self._entry.data[CONF_DSMR_VERSION])
with suppress(TypeError):