Fix for failing Solarlog integration in HA 2021.12 (#61602)

pull/61627/head
Ernst Klamer 2021-12-12 23:09:15 +01:00 committed by GitHub
parent aff74f7969
commit 6e7de8f85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 40 deletions

View File

@ -4,16 +4,11 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT, SensorDeviceClass,
STATE_CLASS_TOTAL,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass,
) )
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_POWER_FACTOR,
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLTAGE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
PERCENTAGE, PERCENTAGE,
@ -38,35 +33,35 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="time", key="time",
name="last update", name="last update",
device_class=DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_ac", key="power_ac",
name="power AC", name="power AC",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_dc", key="power_dc",
name="power DC", name="power DC",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="voltage_ac", key="voltage_ac",
name="voltage AC", name="voltage AC",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="voltage_dc", key="voltage_dc",
name="voltage DC", name="voltage DC",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_day", key="yield_day",
@ -101,50 +96,50 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
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,
state_class=STATE_CLASS_TOTAL, state_class=SensorStateClass.TOTAL,
factor=0.001, factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_ac", key="consumption_ac",
name="consumption AC", name="consumption AC",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_day", key="consumption_day",
name="consumption day", name="consumption day",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, factor=0.001,
), ),
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=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, factor=0.001,
), ),
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=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, factor=0.001,
), ),
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=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
factor=0.001, factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_total", key="consumption_total",
name="consumption total", name="consumption total",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=STATE_CLASS_TOTAL, state_class=SensorStateClass.TOTAL,
factor=0.001, factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
@ -152,31 +147,31 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
name="installed peak power", name="installed peak power",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="alternator_loss", key="alternator_loss",
name="alternator loss", name="alternator loss",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="capacity", key="capacity",
name="capacity", name="capacity",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, factor=100,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="efficiency", key="efficiency",
name="efficiency", name="efficiency",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, factor=100,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
@ -184,15 +179,15 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
name="power available", name="power available",
icon="mdi:solar-power", icon="mdi:solar-power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="usage", key="usage",
name="usage", name="usage",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
factor=100, factor=100,
), ),
) )

View File

@ -1,7 +1,8 @@
"""Platform for solarlog sensors.""" """Platform for solarlog sensors."""
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, StateType 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
@ -38,11 +39,16 @@ class SolarlogSensor(update_coordinator.CoordinatorEntity, SensorEntity):
) )
@property @property
def native_value(self) -> StateType: def native_value(self):
"""Return the native sensor value.""" """Return the native sensor value."""
result = getattr(self.coordinator.data, self.entity_description.key) if self.entity_description.key == "time":
if self.entity_description.factor: state = as_local(
state = round(result * self.entity_description.factor, 3) getattr(self.coordinator.data, self.entity_description.key)
)
else: else:
state = result 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 return state