mirror of
https://github.com/home-assistant/core.git
synced 2025-07-04 03:47:09 +00:00
Reduce nesting in discovergy setup (#104127)
* Reduce nesting in discovergy setup * Update homeassistant/components/discovergy/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
e2f6fbd59b
commit
2d891c77ef
@ -30,12 +30,19 @@ from .const import DOMAIN, MANUFACTURER
|
|||||||
PARALLEL_UPDATES = 1
|
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)
|
@dataclass(kw_only=True)
|
||||||
class DiscovergySensorEntityDescription(SensorEntityDescription):
|
class DiscovergySensorEntityDescription(SensorEntityDescription):
|
||||||
"""Class to describe a Discovergy sensor entity."""
|
"""Class to describe a Discovergy sensor entity."""
|
||||||
|
|
||||||
value_fn: Callable[[Reading, str, int], datetime | float | None] = field(
|
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: [])
|
alternative_keys: list[str] = field(default_factory=lambda: [])
|
||||||
scale: int = field(default_factory=lambda: 1000)
|
scale: int = field(default_factory=lambda: 1000)
|
||||||
@ -165,33 +172,27 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
entities: list[DiscovergySensor] = []
|
entities: list[DiscovergySensor] = []
|
||||||
for meter in meters:
|
for meter in meters:
|
||||||
sensors = None
|
sensors: tuple[DiscovergySensorEntityDescription, ...] = ()
|
||||||
if meter.measurement_type == "ELECTRICITY":
|
|
||||||
sensors = ELECTRICITY_SENSORS
|
|
||||||
elif meter.measurement_type == "GAS":
|
|
||||||
sensors = GAS_SENSORS
|
|
||||||
|
|
||||||
coordinator: DiscovergyUpdateCoordinator = data.coordinators[meter.meter_id]
|
coordinator: DiscovergyUpdateCoordinator = data.coordinators[meter.meter_id]
|
||||||
|
|
||||||
if sensors is not None:
|
# select sensor descriptions based on meter type and combine with additional sensors
|
||||||
for description in sensors:
|
if meter.measurement_type == "ELECTRICITY":
|
||||||
# check if this meter has this data, then add this sensor
|
sensors = ELECTRICITY_SENSORS + ADDITIONAL_SENSORS
|
||||||
for key in {description.key, *description.alternative_keys}:
|
elif meter.measurement_type == "GAS":
|
||||||
if key in coordinator.data.values:
|
sensors = GAS_SENSORS + ADDITIONAL_SENSORS
|
||||||
entities.append(
|
|
||||||
DiscovergySensor(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)
|
||||||
)
|
)
|
||||||
|
|
||||||
for description in ADDITIONAL_SENSORS:
|
async_add_entities(entities)
|
||||||
entities.append(
|
|
||||||
DiscovergySensor(description.key, description, meter, coordinator)
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, False)
|
|
||||||
|
|
||||||
|
|
||||||
class DiscovergySensor(CoordinatorEntity[DiscovergyUpdateCoordinator], SensorEntity):
|
class DiscovergySensor(CoordinatorEntity[DiscovergyUpdateCoordinator], SensorEntity):
|
||||||
"""Represents a discovergy smart meter sensor."""
|
"""Represents a Discovergy smart meter sensor."""
|
||||||
|
|
||||||
entity_description: DiscovergySensorEntityDescription
|
entity_description: DiscovergySensorEntityDescription
|
||||||
data_key: str
|
data_key: str
|
||||||
|
@ -1,4 +1,38 @@
|
|||||||
# serializer version: 1
|
# serializer version: 1
|
||||||
|
# name: test_sensor[electricity last transmitted]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': <RegistryEntryDisabler.INTEGRATION: 'integration'>,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||||
|
'entity_id': 'sensor.electricity_teststrasse_1_last_transmitted',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||||
|
'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]
|
# name: test_sensor[electricity total consumption]
|
||||||
EntityRegistryEntrySnapshot({
|
EntityRegistryEntrySnapshot({
|
||||||
'aliases': set({
|
'aliases': set({
|
||||||
@ -101,6 +135,40 @@
|
|||||||
'state': '531.75',
|
'state': '531.75',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
|
# name: test_sensor[gas last transmitted]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': <RegistryEntryDisabler.INTEGRATION: 'integration'>,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||||
|
'entity_id': 'sensor.gas_teststrasse_1_last_transmitted',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||||
|
'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]
|
# name: test_sensor[gas total consumption]
|
||||||
EntityRegistryEntrySnapshot({
|
EntityRegistryEntrySnapshot({
|
||||||
'aliases': set({
|
'aliases': set({
|
||||||
|
@ -16,12 +16,16 @@ from homeassistant.helpers import entity_registry as er
|
|||||||
[
|
[
|
||||||
"sensor.electricity_teststrasse_1_total_consumption",
|
"sensor.electricity_teststrasse_1_total_consumption",
|
||||||
"sensor.electricity_teststrasse_1_total_power",
|
"sensor.electricity_teststrasse_1_total_power",
|
||||||
|
"sensor.electricity_teststrasse_1_last_transmitted",
|
||||||
"sensor.gas_teststrasse_1_total_gas_consumption",
|
"sensor.gas_teststrasse_1_total_gas_consumption",
|
||||||
|
"sensor.gas_teststrasse_1_last_transmitted",
|
||||||
],
|
],
|
||||||
ids=[
|
ids=[
|
||||||
"electricity total consumption",
|
"electricity total consumption",
|
||||||
"electricity total power",
|
"electricity total power",
|
||||||
|
"electricity last transmitted",
|
||||||
"gas total consumption",
|
"gas total consumption",
|
||||||
|
"gas last transmitted",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@pytest.mark.usefixtures("setup_integration")
|
@pytest.mark.usefixtures("setup_integration")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user