Improve typing of HomeWizard sensors (#85997)

* Improve typing of HomeWizard sensors

* Fix typo when tried to fix a typo :)
This commit is contained in:
Franck Nijhof 2023-01-16 11:22:46 +01:00 committed by GitHub
parent f9662e0af0
commit 11a81dc485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 71 deletions

View File

@ -1,7 +1,11 @@
"""Creates Homewizard sensor entities.""" """Creates HomeWizard sensor entities."""
from __future__ import annotations from __future__ import annotations
from typing import Final, cast from collections.abc import Callable
from dataclasses import dataclass
from typing import Final
from homewizard_energy.models import Data
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -22,39 +26,57 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import DOMAIN, DeviceResponseEntry from .const import DOMAIN
from .coordinator import HWEnergyDeviceUpdateCoordinator from .coordinator import HWEnergyDeviceUpdateCoordinator
from .entity import HomeWizardEntity from .entity import HomeWizardEntity
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription( @dataclass
class HomeWizardEntityDescriptionMixin:
"""Mixin values for HomeWizard entities."""
value_fn: Callable[[Data], float | int | str | None]
@dataclass
class HomeWizardSensorEntityDescription(
SensorEntityDescription, HomeWizardEntityDescriptionMixin
):
"""Class describing HomeWizard sensor entities."""
SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = (
HomeWizardSensorEntityDescription(
key="smr_version", key="smr_version",
name="DSMR version", name="DSMR version",
icon="mdi:counter", icon="mdi:counter",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.smr_version,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="meter_model", key="meter_model",
name="Smart meter model", name="Smart meter model",
icon="mdi:gauge", icon="mdi:gauge",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.meter_model,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="wifi_ssid", key="wifi_ssid",
name="Wi-Fi SSID", name="Wi-Fi SSID",
icon="mdi:wifi", icon="mdi:wifi",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.wifi_ssid,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_tariff", key="active_tariff",
name="Active tariff", name="Active tariff",
icon="mdi:calendar-clock", icon="mdi:calendar-clock",
value_fn=lambda data: data.active_tariff,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="wifi_strength", key="wifi_strength",
name="Wi-Fi strength", name="Wi-Fi strength",
icon="mdi:wifi", icon="mdi:wifi",
@ -62,242 +84,277 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.wifi_strength,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_import_kwh", key="total_power_import_kwh",
name="Total power import", name="Total power import",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_import_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_import_t1_kwh", key="total_power_import_t1_kwh",
name="Total power import T1", name="Total power import T1",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_import_t1_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_import_t2_kwh", key="total_power_import_t2_kwh",
name="Total power import T2", name="Total power import T2",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_import_t2_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_import_t3_kwh", key="total_power_import_t3_kwh",
name="Total power import T3", name="Total power import T3",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_import_t3_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_import_t4_kwh", key="total_power_import_t4_kwh",
name="Total power import T4", name="Total power import T4",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_import_t4_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_export_kwh", key="total_power_export_kwh",
name="Total power export", name="Total power export",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_export_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_export_t1_kwh", key="total_power_export_t1_kwh",
name="Total power export T1", name="Total power export T1",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_export_t1_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_export_t2_kwh", key="total_power_export_t2_kwh",
name="Total power export T2", name="Total power export T2",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_export_t2_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_export_t3_kwh", key="total_power_export_t3_kwh",
name="Total power export T3", name="Total power export T3",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_export_t3_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_power_export_t4_kwh", key="total_power_export_t4_kwh",
name="Total power export T4", name="Total power export T4",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_power_export_t4_kwh,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_power_w", key="active_power_w",
name="Active power", name="Active power",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.active_power_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_power_l1_w", key="active_power_l1_w",
name="Active power L1", name="Active power L1",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.active_power_l1_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_power_l2_w", key="active_power_l2_w",
name="Active power L2", name="Active power L2",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.active_power_l2_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_power_l3_w", key="active_power_l3_w",
name="Active power L3", name="Active power L3",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.active_power_l3_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_voltage_l1_v", key="active_voltage_l1_v",
name="Active voltage L1", name="Active voltage L1",
native_unit_of_measurement=UnitOfElectricPotential.VOLT, native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_voltage_l1_v,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_voltage_l2_v", key="active_voltage_l2_v",
name="Active voltage L2", name="Active voltage L2",
native_unit_of_measurement=UnitOfElectricPotential.VOLT, native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_voltage_l2_v,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_voltage_l3_v", key="active_voltage_l3_v",
name="Active voltage L3", name="Active voltage L3",
native_unit_of_measurement=UnitOfElectricPotential.VOLT, native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_voltage_l3_v,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_current_l1_a", key="active_current_l1_a",
name="Active current L1", name="Active current L1",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_current_l1_a,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_current_l2_a", key="active_current_l2_a",
name="Active current L2", name="Active current L2",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_current_l2_a,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_current_l3_a", key="active_current_l3_a",
name="Active current L3", name="Active current L3",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_current_l3_a,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_frequency_hz", key="active_frequency_hz",
name="Active frequency", name="Active frequency",
native_unit_of_measurement=UnitOfFrequency.HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY, device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: data.active_frequency_hz,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_sag_l1_count", key="voltage_sag_l1_count",
name="Voltage sags detected L1", name="Voltage sags detected L1",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_sag_l1_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_sag_l2_count", key="voltage_sag_l2_count",
name="Voltage sags detected L2", name="Voltage sags detected L2",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_sag_l2_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_sag_l3_count", key="voltage_sag_l3_count",
name="Voltage sags detected L3", name="Voltage sags detected L3",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_sag_l3_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_swell_l1_count", key="voltage_swell_l1_count",
name="Voltage swells detected L1", name="Voltage swells detected L1",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_swell_l1_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_swell_l2_count", key="voltage_swell_l2_count",
name="Voltage swells detected L2", name="Voltage swells detected L2",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_swell_l2_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="voltage_swell_l3_count", key="voltage_swell_l3_count",
name="Voltage swells detected L3", name="Voltage swells detected L3",
icon="mdi:alert", icon="mdi:alert",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.voltage_swell_l3_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="any_power_fail_count", key="any_power_fail_count",
name="Power failures detected", name="Power failures detected",
icon="mdi:transmission-tower-off", icon="mdi:transmission-tower-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.any_power_fail_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="long_power_fail_count", key="long_power_fail_count",
name="Long power failures detected", name="Long power failures detected",
icon="mdi:transmission-tower-off", icon="mdi:transmission-tower-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.long_power_fail_count,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_power_average_w", key="active_power_average_w",
name="Active average demand", name="Active average demand",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
value_fn=lambda data: data.active_power_average_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="montly_power_peak_w", key="montly_power_peak_w",
name="Peak demand current month", name="Peak demand current month",
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
value_fn=lambda data: data.montly_power_peak_w,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_gas_m3", key="total_gas_m3",
name="Total gas", name="Total gas",
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS, device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_gas_m3,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="active_liter_lpm", key="active_liter_lpm",
name="Active water usage", name="Active water usage",
native_unit_of_measurement="l/min", native_unit_of_measurement="l/min",
icon="mdi:water", icon="mdi:water",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.active_liter_lpm,
), ),
SensorEntityDescription( HomeWizardSensorEntityDescription(
key="total_liter_m3", key="total_liter_m3",
name="Total water usage", name="Total water usage",
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
icon="mdi:gauge", icon="mdi:gauge",
device_class=SensorDeviceClass.WATER, device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data.total_liter_m3,
), ),
) )
@ -308,39 +365,33 @@ async def async_setup_entry(
"""Initialize sensors.""" """Initialize sensors."""
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
entities: list[HWEnergySensor] = [] async_add_entities(
if coordinator.data.data is not None: HomeWizardSensorEntity(coordinator, entry, description)
entities.extend(
HWEnergySensor(coordinator, entry, description)
for description in SENSORS for description in SENSORS
if getattr(coordinator.data.data, description.key) is not None if description.value_fn(coordinator.data.data) is not None
) )
async_add_entities(entities)
class HWEnergySensor(HomeWizardEntity, SensorEntity): class HomeWizardSensorEntity(HomeWizardEntity, SensorEntity):
"""Representation of a HomeWizard Sensor.""" """Representation of a HomeWizard Sensor."""
entity_description: HomeWizardSensorEntityDescription
def __init__( def __init__(
self, self,
coordinator: HWEnergyDeviceUpdateCoordinator, coordinator: HWEnergyDeviceUpdateCoordinator,
entry: ConfigEntry, entry: ConfigEntry,
description: SensorEntityDescription, description: HomeWizardSensorEntityDescription,
) -> None: ) -> None:
"""Initialize Sensor Domain.""" """Initialize Sensor Domain."""
super().__init__(coordinator) super().__init__(coordinator)
self.entity_description = description self.entity_description = description
self.entry = entry
# Config attributes.
self.data_type = description.key
self._attr_unique_id = f"{entry.unique_id}_{description.key}" self._attr_unique_id = f"{entry.unique_id}_{description.key}"
# Special case for export, not everyone has solarpanels # Special case for export, not everyone has solar panels
# The chance that 'export' is non-zero when you have solar panels is nil # The chance that 'export' is non-zero when you have solar panels is nil
if ( if (
self.data_type description.key
in [ in [
"total_power_export_kwh", "total_power_export_kwh",
"total_power_export_t1_kwh", "total_power_export_t1_kwh",
@ -353,14 +404,9 @@ class HWEnergySensor(HomeWizardEntity, SensorEntity):
self._attr_entity_registry_enabled_default = False self._attr_entity_registry_enabled_default = False
@property @property
def data(self) -> DeviceResponseEntry: def native_value(self) -> float | int | str | None:
"""Return data object from DataUpdateCoordinator.""" """Return the sensor value."""
return self.coordinator.data return self.entity_description.value_fn(self.coordinator.data.data)
@property
def native_value(self) -> StateType:
"""Return state of meter."""
return cast(StateType, getattr(self.data.data, self.data_type))
@property @property
def available(self) -> bool: def available(self) -> bool:

View File

@ -3,7 +3,7 @@
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
from homewizard_energy.features import Features from homewizard_energy.features import Features
from homewizard_energy.models import Device from homewizard_energy.models import Data, Device
def get_mock_device( def get_mock_device(
@ -26,7 +26,7 @@ def get_mock_device(
firmware_version=firmware_version, firmware_version=firmware_version,
) )
) )
mock_device.data = AsyncMock(return_value=None) mock_device.data = AsyncMock(return_value=Data.from_dict({}))
mock_device.state = AsyncMock(return_value=None) mock_device.state = AsyncMock(return_value=None)
mock_device.system = AsyncMock(return_value=None) mock_device.system = AsyncMock(return_value=None)
mock_device.features = AsyncMock( mock_device.features = AsyncMock(