diff --git a/homeassistant/components/solarlog/const.py b/homeassistant/components/solarlog/const.py index 3159bc46218..769064089b2 100644 --- a/homeassistant/components/solarlog/const.py +++ b/homeassistant/components/solarlog/const.py @@ -1,7 +1,9 @@ """Constants for the Solar-Log integration.""" from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass +from datetime import datetime from homeassistant.components.sensor import ( SensorDeviceClass, @@ -14,6 +16,7 @@ from homeassistant.const import ( PERCENTAGE, POWER_WATT, ) +from homeassistant.util.dt import as_local DOMAIN = "solarlog" @@ -26,7 +29,7 @@ DEFAULT_NAME = "solarlog" class SolarLogSensorEntityDescription(SensorEntityDescription): """Describes Solarlog sensor entity.""" - factor: float | None = None + value: Callable[[float | int], float] | Callable[[datetime], datetime] | None = None SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( @@ -34,6 +37,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( key="time", name="last update", device_class=SensorDeviceClass.TIMESTAMP, + value=as_local, ), SolarLogSensorEntityDescription( key="power_ac", @@ -68,36 +72,41 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( name="yield day", icon="mdi:solar-power", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, - factor=0.001, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="yield_yesterday", name="yield yesterday", icon="mdi:solar-power", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, - factor=0.001, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="yield_month", name="yield month", icon="mdi:solar-power", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, - factor=0.001, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="yield_year", name="yield year", icon="mdi:solar-power", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, - factor=0.001, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="yield_total", name="yield total", icon="mdi:solar-power", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="consumption_ac", @@ -111,28 +120,28 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( name="consumption day", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="consumption_yesterday", name="consumption yesterday", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="consumption_month", name="consumption month", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="consumption_year", name="consumption year", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="consumption_total", @@ -140,7 +149,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - factor=0.001, + value=lambda value: round(value / 1000, 3), ), SolarLogSensorEntityDescription( key="total_power", @@ -164,7 +173,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - factor=100, + value=lambda value: round(value * 100, 1), ), SolarLogSensorEntityDescription( key="efficiency", @@ -172,7 +181,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - factor=100, + value=lambda value: round(value * 100, 1), ), SolarLogSensorEntityDescription( key="power_available", @@ -188,6 +197,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - factor=100, + value=lambda value: round(value * 100, 1), ), ) diff --git a/homeassistant/components/solarlog/sensor.py b/homeassistant/components/solarlog/sensor.py index 5c4c2bfad28..511f29338b8 100644 --- a/homeassistant/components/solarlog/sensor.py +++ b/homeassistant/components/solarlog/sensor.py @@ -2,7 +2,6 @@ from homeassistant.components.sensor import SensorEntity from homeassistant.helpers import update_coordinator from homeassistant.helpers.entity import DeviceInfo -from homeassistant.util.dt import as_local from . import SolarlogData from .const import DOMAIN, SENSOR_TYPES, SolarLogSensorEntityDescription @@ -41,14 +40,7 @@ class SolarlogSensor(update_coordinator.CoordinatorEntity, SensorEntity): @property def native_value(self): """Return the native sensor value.""" - if self.entity_description.key == "time": - state = as_local( - getattr(self.coordinator.data, self.entity_description.key) - ) - else: - result = getattr(self.coordinator.data, self.entity_description.key) - if self.entity_description.factor: - state = round(result * self.entity_description.factor, 3) - else: - state = result - return state + raw_attr = getattr(self.coordinator.data, self.entity_description.key) + if self.entity_description.value: + return self.entity_description.value(raw_attr) + return raw_attr