mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add sensor when meter last sent its data to Discovergy (#97223)
* Add sensor for last reading by Discovergy * Rename sensor to make it clear what it actually is * Revert back to single sensor classe and extend entity_description with a value function
This commit is contained in:
parent
69b3ae4588
commit
9713466817
@ -1,7 +1,9 @@
|
|||||||
"""Discovergy sensor entity."""
|
"""Discovergy sensor entity."""
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from pydiscovergy.models import Meter
|
from pydiscovergy.models import Meter, Reading
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
@ -11,6 +13,7 @@ from homeassistant.components.sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
EntityCategory,
|
||||||
UnitOfElectricPotential,
|
UnitOfElectricPotential,
|
||||||
UnitOfEnergy,
|
UnitOfEnergy,
|
||||||
UnitOfPower,
|
UnitOfPower,
|
||||||
@ -19,7 +22,6 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import DiscovergyData, DiscovergyUpdateCoordinator
|
from . import DiscovergyData, DiscovergyUpdateCoordinator
|
||||||
@ -32,6 +34,9 @@ PARALLEL_UPDATES = 1
|
|||||||
class DiscovergyMixin:
|
class DiscovergyMixin:
|
||||||
"""Mixin for alternative keys."""
|
"""Mixin for alternative keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[Reading, str, int], datetime | float | None] = field(
|
||||||
|
default=lambda reading, key, scale: float(reading.values[key] / 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)
|
||||||
|
|
||||||
@ -144,6 +149,17 @@ ELECTRICITY_SENSORS: tuple[DiscovergySensorEntityDescription, ...] = (
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ADDITIONAL_SENSORS: tuple[DiscovergySensorEntityDescription, ...] = (
|
||||||
|
DiscovergySensorEntityDescription(
|
||||||
|
key="last_transmitted",
|
||||||
|
translation_key="last_transmitted",
|
||||||
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
value_fn=lambda reading, key, scale: reading.time_with_timezone,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
@ -160,18 +176,22 @@ async def async_setup_entry(
|
|||||||
elif meter.measurement_type == "GAS":
|
elif meter.measurement_type == "GAS":
|
||||||
sensors = GAS_SENSORS
|
sensors = GAS_SENSORS
|
||||||
|
|
||||||
|
coordinator: DiscovergyUpdateCoordinator = data.coordinators[meter.meter_id]
|
||||||
|
|
||||||
if sensors is not None:
|
if sensors is not None:
|
||||||
for description in sensors:
|
for description in sensors:
|
||||||
# check if this meter has this data, then add this sensor
|
# check if this meter has this data, then add this sensor
|
||||||
for key in {description.key, *description.alternative_keys}:
|
for key in {description.key, *description.alternative_keys}:
|
||||||
coordinator: DiscovergyUpdateCoordinator = data.coordinators[
|
|
||||||
meter.meter_id
|
|
||||||
]
|
|
||||||
if key in coordinator.data.values:
|
if key in coordinator.data.values:
|
||||||
entities.append(
|
entities.append(
|
||||||
DiscovergySensor(key, description, meter, coordinator)
|
DiscovergySensor(key, description, meter, coordinator)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
for description in ADDITIONAL_SENSORS:
|
||||||
|
entities.append(
|
||||||
|
DiscovergySensor(description.key, description, meter, coordinator)
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
||||||
@ -204,8 +224,8 @@ class DiscovergySensor(CoordinatorEntity[DiscovergyUpdateCoordinator], SensorEnt
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> datetime | float | None:
|
||||||
"""Return the sensor state."""
|
"""Return the sensor state."""
|
||||||
return float(
|
return self.entity_description.value_fn(
|
||||||
self.coordinator.data.values[self.data_key] / self.entity_description.scale
|
self.coordinator.data, self.data_key, self.entity_description.scale
|
||||||
)
|
)
|
||||||
|
@ -60,6 +60,9 @@
|
|||||||
},
|
},
|
||||||
"phase_3_power": {
|
"phase_3_power": {
|
||||||
"name": "Phase 3 power"
|
"name": "Phase 3 power"
|
||||||
|
},
|
||||||
|
"last_transmitted": {
|
||||||
|
"name": "Last transmitted"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user