Move Solarlog state to entity description (#62093)

* Move value to const

* Move value to const

* remove cast

* Remove Statetype import

* Add in and output for callable

* fix mypy

* Add int to callable

* fix callable

* Only convert value

* Add datetime import

* Update homeassistant/components/solarlog/const.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Ernst Klamer 2021-12-17 12:40:32 +01:00 committed by GitHub
parent 0f2a3c074e
commit e771421ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 26 deletions

View File

@ -1,7 +1,9 @@
"""Constants for the Solar-Log integration.""" """Constants for the Solar-Log integration."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -14,6 +16,7 @@ from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
POWER_WATT, POWER_WATT,
) )
from homeassistant.util.dt import as_local
DOMAIN = "solarlog" DOMAIN = "solarlog"
@ -26,7 +29,7 @@ DEFAULT_NAME = "solarlog"
class SolarLogSensorEntityDescription(SensorEntityDescription): class SolarLogSensorEntityDescription(SensorEntityDescription):
"""Describes Solarlog sensor entity.""" """Describes Solarlog sensor entity."""
factor: float | None = None value: Callable[[float | int], float] | Callable[[datetime], datetime] | None = None
SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
@ -34,6 +37,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
key="time", key="time",
name="last update", name="last update",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
value=as_local,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_ac", key="power_ac",
@ -68,36 +72,41 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
name="yield day", name="yield day",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
factor=0.001, device_class=SensorDeviceClass.ENERGY,
value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_yesterday", key="yield_yesterday",
name="yield yesterday", name="yield yesterday",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
factor=0.001, device_class=SensorDeviceClass.ENERGY,
value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_month", key="yield_month",
name="yield month", name="yield month",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
factor=0.001, device_class=SensorDeviceClass.ENERGY,
value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_year", key="yield_year",
name="yield year", name="yield year",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
factor=0.001, device_class=SensorDeviceClass.ENERGY,
value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_total", key="yield_total",
name="yield total", name="yield total",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_ac", key="consumption_ac",
@ -111,28 +120,28 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
name="consumption day", name="consumption day",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_yesterday", key="consumption_yesterday",
name="consumption yesterday", name="consumption yesterday",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_month", key="consumption_month",
name="consumption month", name="consumption month",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_year", key="consumption_year",
name="consumption year", name="consumption year",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_total", key="consumption_total",
@ -140,7 +149,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
factor=0.001, value=lambda value: round(value / 1000, 3),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="total_power", key="total_power",
@ -164,7 +173,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, value=lambda value: round(value * 100, 1),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="efficiency", key="efficiency",
@ -172,7 +181,7 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, value=lambda value: round(value * 100, 1),
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_available", key="power_available",
@ -188,6 +197,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, value=lambda value: round(value * 100, 1),
), ),
) )

View File

@ -2,7 +2,6 @@
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.helpers import update_coordinator from homeassistant.helpers import update_coordinator
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.util.dt import as_local
from . import SolarlogData from . import SolarlogData
from .const import DOMAIN, SENSOR_TYPES, SolarLogSensorEntityDescription from .const import DOMAIN, SENSOR_TYPES, SolarLogSensorEntityDescription
@ -41,14 +40,7 @@ class SolarlogSensor(update_coordinator.CoordinatorEntity, SensorEntity):
@property @property
def native_value(self): def native_value(self):
"""Return the native sensor value.""" """Return the native sensor value."""
if self.entity_description.key == "time": raw_attr = getattr(self.coordinator.data, self.entity_description.key)
state = as_local( if self.entity_description.value:
getattr(self.coordinator.data, self.entity_description.key) return self.entity_description.value(raw_attr)
) return raw_attr
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