diff --git a/homeassistant/components/discovergy/sensor.py b/homeassistant/components/discovergy/sensor.py index c0c610fa98a..508af900a1c 100644 --- a/homeassistant/components/discovergy/sensor.py +++ b/homeassistant/components/discovergy/sensor.py @@ -30,12 +30,19 @@ from .const import DOMAIN, MANUFACTURER PARALLEL_UPDATES = 1 +def _get_and_scale(reading: Reading, key: str, scale: int) -> datetime | float | None: + """Get a value from a Reading and divide with scale it.""" + if (value := reading.values.get(key)) is not None: + return value / scale + return None + + @dataclass(kw_only=True) class DiscovergySensorEntityDescription(SensorEntityDescription): """Class to describe a Discovergy sensor entity.""" value_fn: Callable[[Reading, str, int], datetime | float | None] = field( - default=lambda reading, key, scale: float(reading.values[key] / scale) + default=_get_and_scale ) alternative_keys: list[str] = field(default_factory=lambda: []) scale: int = field(default_factory=lambda: 1000) @@ -165,33 +172,27 @@ async def async_setup_entry( entities: list[DiscovergySensor] = [] for meter in meters: - sensors = None - if meter.measurement_type == "ELECTRICITY": - sensors = ELECTRICITY_SENSORS - elif meter.measurement_type == "GAS": - sensors = GAS_SENSORS - + sensors: tuple[DiscovergySensorEntityDescription, ...] = () coordinator: DiscovergyUpdateCoordinator = data.coordinators[meter.meter_id] - if sensors is not None: - for description in sensors: - # check if this meter has this data, then add this sensor - for key in {description.key, *description.alternative_keys}: - if key in coordinator.data.values: - entities.append( - DiscovergySensor(key, description, meter, coordinator) - ) + # select sensor descriptions based on meter type and combine with additional sensors + if meter.measurement_type == "ELECTRICITY": + sensors = ELECTRICITY_SENSORS + ADDITIONAL_SENSORS + elif meter.measurement_type == "GAS": + sensors = GAS_SENSORS + ADDITIONAL_SENSORS - for description in ADDITIONAL_SENSORS: - entities.append( - DiscovergySensor(description.key, description, meter, coordinator) - ) + entities.extend( + DiscovergySensor(value_key, description, meter, coordinator) + for description in sensors + for value_key in {description.key, *description.alternative_keys} + if description.value_fn(coordinator.data, value_key, description.scale) + ) - async_add_entities(entities, False) + async_add_entities(entities) class DiscovergySensor(CoordinatorEntity[DiscovergyUpdateCoordinator], SensorEntity): - """Represents a discovergy smart meter sensor.""" + """Represents a Discovergy smart meter sensor.""" entity_description: DiscovergySensorEntityDescription data_key: str diff --git a/tests/components/discovergy/snapshots/test_sensor.ambr b/tests/components/discovergy/snapshots/test_sensor.ambr index 36af1276fe1..981d1119a93 100644 --- a/tests/components/discovergy/snapshots/test_sensor.ambr +++ b/tests/components/discovergy/snapshots/test_sensor.ambr @@ -1,4 +1,38 @@ # serializer version: 1 +# name: test_sensor[electricity last transmitted] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': , + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.electricity_teststrasse_1_last_transmitted', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last transmitted', + 'platform': 'discovergy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'last_transmitted', + 'unique_id': 'abc123-last_transmitted', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor[electricity last transmitted].1 + None +# --- # name: test_sensor[electricity total consumption] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -101,6 +135,40 @@ 'state': '531.75', }) # --- +# name: test_sensor[gas last transmitted] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': , + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.gas_teststrasse_1_last_transmitted', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last transmitted', + 'platform': 'discovergy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'last_transmitted', + 'unique_id': 'def456-last_transmitted', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor[gas last transmitted].1 + None +# --- # name: test_sensor[gas total consumption] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/discovergy/test_sensor.py b/tests/components/discovergy/test_sensor.py index 33fb1a37cd9..aba8229acf5 100644 --- a/tests/components/discovergy/test_sensor.py +++ b/tests/components/discovergy/test_sensor.py @@ -16,12 +16,16 @@ from homeassistant.helpers import entity_registry as er [ "sensor.electricity_teststrasse_1_total_consumption", "sensor.electricity_teststrasse_1_total_power", + "sensor.electricity_teststrasse_1_last_transmitted", "sensor.gas_teststrasse_1_total_gas_consumption", + "sensor.gas_teststrasse_1_last_transmitted", ], ids=[ "electricity total consumption", "electricity total power", + "electricity last transmitted", "gas total consumption", + "gas last transmitted", ], ) @pytest.mark.usefixtures("setup_integration")