diff --git a/homeassistant/components/homewizard/sensor.py b/homeassistant/components/homewizard/sensor.py index 6462c281346..c27fb2187a1 100644 --- a/homeassistant/components/homewizard/sensor.py +++ b/homeassistant/components/homewizard/sensor.py @@ -38,6 +38,7 @@ PARALLEL_UPDATES = 1 class HomeWizardEntityDescriptionMixin: """Mixin values for HomeWizard entities.""" + has_fn: Callable[[Data], bool] value_fn: Callable[[Data], float | int | str | None] @@ -47,6 +48,8 @@ class HomeWizardSensorEntityDescription( ): """Class describing HomeWizard sensor entities.""" + enabled_fn: Callable[[Data], bool] = lambda data: True + SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( HomeWizardSensorEntityDescription( @@ -54,6 +57,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="DSMR version", icon="mdi:counter", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.smr_version is not None, value_fn=lambda data: data.smr_version, ), HomeWizardSensorEntityDescription( @@ -61,6 +65,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Smart meter model", icon="mdi:gauge", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.meter_model is not None, value_fn=lambda data: data.meter_model, ), HomeWizardSensorEntityDescription( @@ -68,6 +73,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Smart meter identifier", icon="mdi:alphabetical-variant", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.unique_meter_id is not None, value_fn=lambda data: data.unique_meter_id, ), HomeWizardSensorEntityDescription( @@ -75,12 +81,14 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Wi-Fi SSID", icon="mdi:wifi", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.wifi_ssid is not None, value_fn=lambda data: data.wifi_ssid, ), HomeWizardSensorEntityDescription( key="active_tariff", name="Active tariff", icon="mdi:calendar-clock", + has_fn=lambda data: data.active_tariff is not None, value_fn=lambda data: ( None if data.active_tariff is None else str(data.active_tariff) ), @@ -95,6 +103,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, + has_fn=lambda data: data.wifi_strength is not None, value_fn=lambda data: data.wifi_strength, ), HomeWizardSensorEntityDescription( @@ -103,7 +112,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_import_kwh, + has_fn=lambda data: data.total_power_import_kwh is not None, + value_fn=lambda data: data.total_power_import_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_import_t1_kwh", @@ -111,7 +121,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_import_t1_kwh, + has_fn=lambda data: data.total_power_import_t1_kwh is not None, + value_fn=lambda data: data.total_power_import_t1_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_import_t2_kwh", @@ -119,7 +130,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_import_t2_kwh, + has_fn=lambda data: data.total_power_import_t2_kwh is not None, + value_fn=lambda data: data.total_power_import_t2_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_import_t3_kwh", @@ -127,7 +139,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_import_t3_kwh, + has_fn=lambda data: data.total_power_import_t3_kwh is not None, + value_fn=lambda data: data.total_power_import_t3_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_import_t4_kwh", @@ -135,7 +148,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_import_t4_kwh, + has_fn=lambda data: data.total_power_import_t4_kwh is not None, + value_fn=lambda data: data.total_power_import_t4_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_export_kwh", @@ -143,7 +157,9 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_export_kwh, + has_fn=lambda data: data.total_power_export_kwh is not None, + enabled_fn=lambda data: data.total_power_export_kwh != 0, + value_fn=lambda data: data.total_power_export_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_export_t1_kwh", @@ -151,7 +167,9 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_export_t1_kwh, + has_fn=lambda data: data.total_power_export_t1_kwh is not None, + enabled_fn=lambda data: data.total_power_export_t1_kwh != 0, + value_fn=lambda data: data.total_power_export_t1_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_export_t2_kwh", @@ -159,7 +177,9 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_export_t2_kwh, + has_fn=lambda data: data.total_power_export_t2_kwh is not None, + enabled_fn=lambda data: data.total_power_export_t2_kwh != 0, + value_fn=lambda data: data.total_power_export_t2_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_export_t3_kwh", @@ -167,7 +187,9 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_export_t3_kwh, + has_fn=lambda data: data.total_power_export_t3_kwh is not None, + enabled_fn=lambda data: data.total_power_export_t3_kwh != 0, + value_fn=lambda data: data.total_power_export_t3_kwh or None, ), HomeWizardSensorEntityDescription( key="total_power_export_t4_kwh", @@ -175,7 +197,9 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_power_export_t4_kwh, + has_fn=lambda data: data.total_power_export_t4_kwh is not None, + enabled_fn=lambda data: data.total_power_export_t4_kwh != 0, + value_fn=lambda data: data.total_power_export_t4_kwh or None, ), HomeWizardSensorEntityDescription( key="active_power_w", @@ -183,6 +207,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + has_fn=lambda data: data.active_power_w is not None, value_fn=lambda data: data.active_power_w, ), HomeWizardSensorEntityDescription( @@ -191,6 +216,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + has_fn=lambda data: data.active_power_l1_w is not None, value_fn=lambda data: data.active_power_l1_w, ), HomeWizardSensorEntityDescription( @@ -199,6 +225,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + has_fn=lambda data: data.active_power_l2_w is not None, value_fn=lambda data: data.active_power_l2_w, ), HomeWizardSensorEntityDescription( @@ -207,6 +234,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + has_fn=lambda data: data.active_power_l3_w is not None, value_fn=lambda data: data.active_power_l3_w, ), HomeWizardSensorEntityDescription( @@ -216,6 +244,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_voltage_l1_v is not None, value_fn=lambda data: data.active_voltage_l1_v, ), HomeWizardSensorEntityDescription( @@ -225,6 +254,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_voltage_l2_v is not None, value_fn=lambda data: data.active_voltage_l2_v, ), HomeWizardSensorEntityDescription( @@ -234,6 +264,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_voltage_l3_v is not None, value_fn=lambda data: data.active_voltage_l3_v, ), HomeWizardSensorEntityDescription( @@ -243,6 +274,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_current_l1_a is not None, value_fn=lambda data: data.active_current_l1_a, ), HomeWizardSensorEntityDescription( @@ -252,6 +284,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_current_l2_a is not None, value_fn=lambda data: data.active_current_l2_a, ), HomeWizardSensorEntityDescription( @@ -261,6 +294,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_current_l3_a is not None, value_fn=lambda data: data.active_current_l3_a, ), HomeWizardSensorEntityDescription( @@ -270,6 +304,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( device_class=SensorDeviceClass.FREQUENCY, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, + has_fn=lambda data: data.active_frequency_hz is not None, value_fn=lambda data: data.active_frequency_hz, ), HomeWizardSensorEntityDescription( @@ -277,6 +312,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage sags detected L1", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_sag_l1_count is not None, value_fn=lambda data: data.voltage_sag_l1_count, ), HomeWizardSensorEntityDescription( @@ -284,6 +320,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage sags detected L2", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_sag_l2_count is not None, value_fn=lambda data: data.voltage_sag_l2_count, ), HomeWizardSensorEntityDescription( @@ -291,6 +328,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage sags detected L3", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_sag_l3_count is not None, value_fn=lambda data: data.voltage_sag_l3_count, ), HomeWizardSensorEntityDescription( @@ -298,6 +336,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage swells detected L1", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_swell_l1_count is not None, value_fn=lambda data: data.voltage_swell_l1_count, ), HomeWizardSensorEntityDescription( @@ -305,6 +344,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage swells detected L2", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_swell_l2_count is not None, value_fn=lambda data: data.voltage_swell_l2_count, ), HomeWizardSensorEntityDescription( @@ -312,6 +352,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Voltage swells detected L3", icon="mdi:alert", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.voltage_swell_l3_count is not None, value_fn=lambda data: data.voltage_swell_l3_count, ), HomeWizardSensorEntityDescription( @@ -319,6 +360,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Power failures detected", icon="mdi:transmission-tower-off", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.any_power_fail_count is not None, value_fn=lambda data: data.any_power_fail_count, ), HomeWizardSensorEntityDescription( @@ -326,6 +368,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Long power failures detected", icon="mdi:transmission-tower-off", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.long_power_fail_count is not None, value_fn=lambda data: data.long_power_fail_count, ), HomeWizardSensorEntityDescription( @@ -333,6 +376,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Active average demand", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, + has_fn=lambda data: data.active_power_average_w is not None, value_fn=lambda data: data.active_power_average_w, ), HomeWizardSensorEntityDescription( @@ -340,6 +384,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( name="Peak demand current month", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, + has_fn=lambda data: data.monthly_power_peak_w is not None, value_fn=lambda data: data.monthly_power_peak_w, ), HomeWizardSensorEntityDescription( @@ -348,13 +393,15 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, device_class=SensorDeviceClass.GAS, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_gas_m3, + has_fn=lambda data: data.total_gas_m3 is not None, + value_fn=lambda data: data.total_gas_m3 or None, ), HomeWizardSensorEntityDescription( key="gas_unique_id", name="Gas meter identifier", icon="mdi:alphabetical-variant", entity_category=EntityCategory.DIAGNOSTIC, + has_fn=lambda data: data.gas_unique_id is not None, value_fn=lambda data: data.gas_unique_id, ), HomeWizardSensorEntityDescription( @@ -363,6 +410,7 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( native_unit_of_measurement="l/min", icon="mdi:water", state_class=SensorStateClass.MEASUREMENT, + has_fn=lambda data: data.active_liter_lpm is not None, value_fn=lambda data: data.active_liter_lpm, ), HomeWizardSensorEntityDescription( @@ -372,7 +420,8 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( icon="mdi:gauge", device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, - value_fn=lambda data: data.total_liter_m3, + has_fn=lambda data: data.total_liter_m3 is not None, + value_fn=lambda data: data.total_liter_m3 or None, ), ) @@ -386,7 +435,7 @@ async def async_setup_entry( async_add_entities( HomeWizardSensorEntity(coordinator, entry, description) for description in SENSORS - if description.value_fn(coordinator.data.data) is not None + if description.has_fn(coordinator.data.data) ) @@ -405,20 +454,7 @@ class HomeWizardSensorEntity(HomeWizardEntity, SensorEntity): super().__init__(coordinator) self.entity_description = description self._attr_unique_id = f"{entry.unique_id}_{description.key}" - - # Special case for export, not everyone has solar panels - # The chance that 'export' is non-zero when you have solar panels is nil - if ( - description.key - in [ - "total_power_export_kwh", - "total_power_export_t1_kwh", - "total_power_export_t2_kwh", - "total_power_export_t3_kwh", - "total_power_export_t4_kwh", - ] - and self.native_value == 0 - ): + if not description.enabled_fn(self.coordinator.data.data): self._attr_entity_registry_enabled_default = False @property