diff --git a/homeassistant/components/rympro/coordinator.py b/homeassistant/components/rympro/coordinator.py index f07929c0ab4..592c82adc68 100644 --- a/homeassistant/components/rympro/coordinator.py +++ b/homeassistant/components/rympro/coordinator.py @@ -33,7 +33,12 @@ class RymProDataUpdateCoordinator(DataUpdateCoordinator[dict[int, dict]]): async def _async_update_data(self) -> dict[int, dict]: """Fetch data from Rym Pro.""" 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: assert self.config_entry await self.hass.config_entries.async_reload(self.config_entry.entry_id) diff --git a/homeassistant/components/rympro/sensor.py b/homeassistant/components/rympro/sensor.py index 35e4b155b28..a6b5b8df93d 100644 --- a/homeassistant/components/rympro/sensor.py +++ b/homeassistant/components/rympro/sensor.py @@ -1,9 +1,12 @@ """Sensor for RymPro meters.""" from __future__ import annotations +from dataclasses import dataclass + from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, + SensorEntityDescription, SensorStateClass, ) from homeassistant.config_entries import ConfigEntry @@ -17,6 +20,30 @@ from .const import DOMAIN 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( hass: HomeAssistant, config_entry: ConfigEntry, @@ -25,8 +52,9 @@ async def async_setup_entry( """Set up sensors for device.""" coordinator: RymProDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] 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 description in SENSOR_DESCRIPTIONS ) @@ -34,32 +62,31 @@ class RymProSensor(CoordinatorEntity[RymProDataUpdateCoordinator], SensorEntity) """Sensor for RymPro meters.""" _attr_has_entity_name = True - _attr_translation_key = "total_consumption" _attr_device_class = SensorDeviceClass.WATER _attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS - _attr_state_class = SensorStateClass.TOTAL_INCREASING + entity_description: RymProSensorEntityDescription def __init__( self, coordinator: RymProDataUpdateCoordinator, meter_id: int, - last_read: int, + description: RymProSensorEntityDescription, entry_id: str, ) -> None: """Initialize sensor.""" super().__init__(coordinator) self._meter_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_device_info = DeviceInfo( identifiers={(DOMAIN, unique_id)}, manufacturer="Read Your Meter Pro", name=f"Meter {meter_id}", ) - self._attr_native_value = last_read + self.entity_description = description @property def native_value(self) -> float | None: """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] diff --git a/homeassistant/components/rympro/strings.json b/homeassistant/components/rympro/strings.json index 2909d6c1b9b..c58bf5b93ba 100644 --- a/homeassistant/components/rympro/strings.json +++ b/homeassistant/components/rympro/strings.json @@ -21,6 +21,9 @@ "sensor": { "total_consumption": { "name": "Total consumption" + }, + "monthly_forecast": { + "name": "Monthly forecast" } } }