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>pull/62196/head
parent
0f2a3c074e
commit
e771421ed0
|
@ -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),
|
||||
),
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue