Improvements to the solarlog integration (#55405)

This commit is contained in:
Ernst Klamer 2021-08-31 16:46:19 +02:00 committed by GitHub
parent 08a0377dcb
commit bd60a58765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 68 deletions

View File

@ -54,49 +54,18 @@ class SolarlogData(update_coordinator.DataUpdateCoordinator):
async def _async_update_data(self): async def _async_update_data(self):
"""Update the data from the SolarLog device.""" """Update the data from the SolarLog device."""
try: try:
api = await self.hass.async_add_executor_job(SolarLog, self.host) data = await self.hass.async_add_executor_job(SolarLog, self.host)
except (OSError, Timeout, HTTPError) as err: except (OSError, Timeout, HTTPError) as err:
raise update_coordinator.UpdateFailed(err) raise update_coordinator.UpdateFailed(err)
if api.time.year == 1999: if data.time.year == 1999:
raise update_coordinator.UpdateFailed( raise update_coordinator.UpdateFailed(
"Invalid data returned (can happen after Solarlog restart)." "Invalid data returned (can happen after Solarlog restart)."
) )
self.logger.debug( self.logger.debug(
"Connection to Solarlog successful. Retrieving latest Solarlog update of %s", "Connection to Solarlog successful. Retrieving latest Solarlog update of %s",
api.time, data.time,
) )
data = {}
try:
data["TIME"] = api.time
data["powerAC"] = api.power_ac
data["powerDC"] = api.power_dc
data["voltageAC"] = api.voltage_ac
data["voltageDC"] = api.voltage_dc
data["yieldDAY"] = api.yield_day / 1000
data["yieldYESTERDAY"] = api.yield_yesterday / 1000
data["yieldMONTH"] = api.yield_month / 1000
data["yieldYEAR"] = api.yield_year / 1000
data["yieldTOTAL"] = api.yield_total / 1000
data["consumptionAC"] = api.consumption_ac
data["consumptionDAY"] = api.consumption_day / 1000
data["consumptionYESTERDAY"] = api.consumption_yesterday / 1000
data["consumptionMONTH"] = api.consumption_month / 1000
data["consumptionYEAR"] = api.consumption_year / 1000
data["consumptionTOTAL"] = api.consumption_total / 1000
data["totalPOWER"] = api.total_power
data["alternatorLOSS"] = api.alternator_loss
data["CAPACITY"] = round(api.capacity * 100, 0)
data["EFFICIENCY"] = round(api.efficiency * 100, 0)
data["powerAVAILABLE"] = api.power_available
data["USAGE"] = round(api.usage * 100, 0)
except AttributeError as err:
raise update_coordinator.UpdateFailed(
f"Missing details data in Solarlog response: {err}"
) from err
_LOGGER.debug("Updated Solarlog overview data: %s", data)
return data return data

View File

@ -19,6 +19,7 @@ from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
POWER_WATT, POWER_WATT,
) )
from homeassistant.util import dt
DOMAIN = "solarlog" DOMAIN = "solarlog"
@ -28,29 +29,20 @@ DEFAULT_NAME = "solarlog"
@dataclass @dataclass
class SolarlogRequiredKeysMixin: class SolarLogSensorEntityDescription(SensorEntityDescription):
"""Mixin for required keys."""
json_key: str
@dataclass
class SolarLogSensorEntityDescription(
SensorEntityDescription, SolarlogRequiredKeysMixin
):
"""Describes Solarlog sensor entity.""" """Describes Solarlog sensor entity."""
factor: float | None = None
SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="time", key="time",
json_key="TIME",
name="last update", name="last update",
device_class=DEVICE_CLASS_TIMESTAMP, device_class=DEVICE_CLASS_TIMESTAMP,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_ac", key="power_ac",
json_key="powerAC",
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,
@ -58,7 +50,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_dc", key="power_dc",
json_key="powerDC",
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,
@ -66,7 +57,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="voltage_ac", key="voltage_ac",
json_key="voltageAC",
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=DEVICE_CLASS_VOLTAGE,
@ -74,7 +64,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="voltage_dc", key="voltage_dc",
json_key="voltageDC",
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=DEVICE_CLASS_VOLTAGE,
@ -82,43 +71,42 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_day", key="yield_day",
json_key="yieldDAY",
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,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_yesterday", key="yield_yesterday",
json_key="yieldYESTERDAY",
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,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_month", key="yield_month",
json_key="yieldMONTH",
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,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_year", key="yield_year",
json_key="yieldYEAR",
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,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="yield_total", key="yield_total",
json_key="yieldTOTAL",
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_INCREASING, state_class=STATE_CLASS_TOTAL_INCREASING,
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_ac", key="consumption_ac",
json_key="consumptionAC",
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=DEVICE_CLASS_POWER,
@ -126,43 +114,43 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_day", key="consumption_day",
json_key="consumptionDAY",
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=DEVICE_CLASS_ENERGY,
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_yesterday", key="consumption_yesterday",
json_key="consumptionYESTERDAY",
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=DEVICE_CLASS_ENERGY,
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_month", key="consumption_month",
json_key="consumptionMONTH",
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=DEVICE_CLASS_ENERGY,
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_year", key="consumption_year",
json_key="consumptionYEAR",
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=DEVICE_CLASS_ENERGY,
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="consumption_total", key="consumption_total",
json_key="consumptionTOTAL",
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=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=STATE_CLASS_MEASUREMENT,
last_reset=dt.utc_from_timestamp(0),
factor=0.001,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="total_power", key="total_power",
json_key="totalPOWER",
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,
@ -170,7 +158,6 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="alternator_loss", key="alternator_loss",
json_key="alternatorLOSS",
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,
@ -179,24 +166,23 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="capacity", key="capacity",
json_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=DEVICE_CLASS_POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
factor=100,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="efficiency", key="efficiency",
json_key="EFFICIENCY",
name="efficiency", name="efficiency",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=DEVICE_CLASS_POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
factor=100,
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="power_available", key="power_available",
json_key="powerAVAILABLE",
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,
@ -205,10 +191,10 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
), ),
SolarLogSensorEntityDescription( SolarLogSensorEntityDescription(
key="usage", key="usage",
json_key="USAGE",
name="usage", name="usage",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=DEVICE_CLASS_POWER_FACTOR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
factor=100,
), ),
) )

View File

@ -39,4 +39,9 @@ class SolarlogSensor(update_coordinator.CoordinatorEntity, SensorEntity):
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return the native sensor value.""" """Return the native sensor value."""
return self.coordinator.data[self.entity_description.json_key] 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