mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add monthly forecast sensor to RymPro (#101012)
* Add monthly forecast * Apply suggestions from code review Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Remove state class and add precision * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Fix ruff and mypy --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
d7787cdfd8
commit
d4be6632cc
@ -33,7 +33,12 @@ class RymProDataUpdateCoordinator(DataUpdateCoordinator[dict[int, dict]]):
|
|||||||
async def _async_update_data(self) -> dict[int, dict]:
|
async def _async_update_data(self) -> dict[int, dict]:
|
||||||
"""Fetch data from Rym Pro."""
|
"""Fetch data from Rym Pro."""
|
||||||
try:
|
try:
|
||||||
return await self.rympro.last_read()
|
meters = await self.rympro.last_read()
|
||||||
|
for meter_id, meter in meters.items():
|
||||||
|
meter["consumption_forecast"] = await self.rympro.consumption_forecast(
|
||||||
|
meter_id
|
||||||
|
)
|
||||||
|
return meters
|
||||||
except UnauthorizedError as error:
|
except UnauthorizedError as error:
|
||||||
assert self.config_entry
|
assert self.config_entry
|
||||||
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
"""Sensor for RymPro meters."""
|
"""Sensor for RymPro meters."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -17,6 +20,30 @@ from .const import DOMAIN
|
|||||||
from .coordinator import RymProDataUpdateCoordinator
|
from .coordinator import RymProDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True, frozen=True)
|
||||||
|
class RymProSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""Class describing RymPro sensor entities."""
|
||||||
|
|
||||||
|
value_key: str
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_DESCRIPTIONS: tuple[RymProSensorEntityDescription, ...] = (
|
||||||
|
RymProSensorEntityDescription(
|
||||||
|
key="total_consumption",
|
||||||
|
translation_key="total_consumption",
|
||||||
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
suggested_display_precision=3,
|
||||||
|
value_key="read",
|
||||||
|
),
|
||||||
|
RymProSensorEntityDescription(
|
||||||
|
key="monthly_forecast",
|
||||||
|
translation_key="monthly_forecast",
|
||||||
|
suggested_display_precision=3,
|
||||||
|
value_key="consumption_forecast",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
@ -25,8 +52,9 @@ async def async_setup_entry(
|
|||||||
"""Set up sensors for device."""
|
"""Set up sensors for device."""
|
||||||
coordinator: RymProDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator: RymProDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
RymProSensor(coordinator, meter_id, meter["read"], config_entry.entry_id)
|
RymProSensor(coordinator, meter_id, description, config_entry.entry_id)
|
||||||
for meter_id, meter in coordinator.data.items()
|
for meter_id, meter in coordinator.data.items()
|
||||||
|
for description in SENSOR_DESCRIPTIONS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -34,32 +62,31 @@ class RymProSensor(CoordinatorEntity[RymProDataUpdateCoordinator], SensorEntity)
|
|||||||
"""Sensor for RymPro meters."""
|
"""Sensor for RymPro meters."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
_attr_translation_key = "total_consumption"
|
|
||||||
_attr_device_class = SensorDeviceClass.WATER
|
_attr_device_class = SensorDeviceClass.WATER
|
||||||
_attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS
|
_attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS
|
||||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
entity_description: RymProSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: RymProDataUpdateCoordinator,
|
coordinator: RymProDataUpdateCoordinator,
|
||||||
meter_id: int,
|
meter_id: int,
|
||||||
last_read: int,
|
description: RymProSensorEntityDescription,
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize sensor."""
|
"""Initialize sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._meter_id = meter_id
|
self._meter_id = meter_id
|
||||||
unique_id = f"{entry_id}_{meter_id}"
|
unique_id = f"{entry_id}_{meter_id}"
|
||||||
self._attr_unique_id = f"{unique_id}_total_consumption"
|
self._attr_unique_id = f"{unique_id}_{description.key}"
|
||||||
self._attr_extra_state_attributes = {"meter_id": str(meter_id)}
|
self._attr_extra_state_attributes = {"meter_id": str(meter_id)}
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, unique_id)},
|
identifiers={(DOMAIN, unique_id)},
|
||||||
manufacturer="Read Your Meter Pro",
|
manufacturer="Read Your Meter Pro",
|
||||||
name=f"Meter {meter_id}",
|
name=f"Meter {meter_id}",
|
||||||
)
|
)
|
||||||
self._attr_native_value = last_read
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> float | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data[self._meter_id]["read"]
|
return self.coordinator.data[self._meter_id][self.entity_description.value_key]
|
||||||
|
@ -21,6 +21,9 @@
|
|||||||
"sensor": {
|
"sensor": {
|
||||||
"total_consumption": {
|
"total_consumption": {
|
||||||
"name": "Total consumption"
|
"name": "Total consumption"
|
||||||
|
},
|
||||||
|
"monthly_forecast": {
|
||||||
|
"name": "Monthly forecast"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user