From 242b50a8710e7b330065e454060c52e71ba3203a Mon Sep 17 00:00:00 2001 From: Thibaut Etienne Date: Sat, 4 Sep 2021 21:00:29 +0000 Subject: [PATCH 1/2] Migrate to native unit of measurement --- custom_components/tahoma/entity.py | 4 +- custom_components/tahoma/sensor.py | 64 +++++++++++++++--------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/custom_components/tahoma/entity.py b/custom_components/tahoma/entity.py index a287f2967..60e1ce869 100644 --- a/custom_components/tahoma/entity.py +++ b/custom_components/tahoma/entity.py @@ -118,7 +118,9 @@ def extra_state_attributes(self) -> dict[str, Any]: class OverkizSensorDescription(SensorEntityDescription): """Class to describe an Overkiz sensor.""" - value: Callable[[str | int | float], str | int | float] | None = lambda val: val + native_value: Callable[ + [str | int | float], str | int | float + ] | None = lambda val: val @dataclass diff --git a/custom_components/tahoma/sensor.py b/custom_components/tahoma/sensor.py index e36c33159..afa81349d 100644 --- a/custom_components/tahoma/sensor.py +++ b/custom_components/tahoma/sensor.py @@ -30,7 +30,7 @@ OverkizSensorDescription( key="core:BatteryLevelState", name="Battery Level", - unit_of_measurement=PERCENTAGE, + native_unit_of_measurement=PERCENTAGE, device_class=sensor.DEVICE_CLASS_BATTERY, state_class=STATE_CLASS_MEASUREMENT, ), @@ -38,15 +38,15 @@ key="core:BatteryState", name="Battery", device_class=sensor.DEVICE_CLASS_BATTERY, - value=lambda value: str(value).capitalize(), + native_value=lambda value: str(value).capitalize(), ), OverkizSensorDescription( key="core:RSSILevelState", name="RSSI Level", - unit_of_measurement=SIGNAL_STRENGTH_DECIBELS, + native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS, device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, state_class=STATE_CLASS_MEASUREMENT, - value=lambda value: round(value), + native_value=lambda value: round(value), ), OverkizSensorDescription( key="core:ExpectedNumberOfShowerState", @@ -65,7 +65,7 @@ key="core:V40WaterVolumeEstimationState", name="Water Volume Estimation at 40 °C", icon="mdi:water", - unit_of_measurement=VOLUME_LITERS, + native_unit_of_measurement=VOLUME_LITERS, entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -73,21 +73,21 @@ key="core:WaterConsumptionState", name="Water Consumption", icon="mdi:water", - unit_of_measurement=VOLUME_LITERS, + native_unit_of_measurement=VOLUME_LITERS, state_class=STATE_CLASS_MEASUREMENT, ), OverkizSensorDescription( key="io:OutletEngineState", name="Outlet Engine", icon="mdi:fan-chevron-down", - unit_of_measurement=VOLUME_LITERS, + native_unit_of_measurement=VOLUME_LITERS, state_class=STATE_CLASS_MEASUREMENT, ), OverkizSensorDescription( key="io:InletEngineState", name="Inlet Engine", icon="mdi:fan-chevron-up", - unit_of_measurement=VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR, + native_unit_of_measurement=VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR, state_class=STATE_CLASS_MEASUREMENT, ), OverkizSensorDescription( @@ -120,7 +120,7 @@ key="core:LuminanceState", name="Luminance", device_class=sensor.DEVICE_CLASS_ILLUMINANCE, - unit_of_measurement=LIGHT_LUX, # core:MeasuredValueType = core:LuminanceInLux + native_unit_of_measurement=LIGHT_LUX, # core:MeasuredValueType = core:LuminanceInLux state_class=STATE_CLASS_MEASUREMENT, ), # ElectricitySensor/CumulativeElectricPowerConsumptionSensor @@ -135,14 +135,14 @@ key="core:ElectricPowerConsumptionState", name="Electric Power Consumption", device_class=sensor.DEVICE_CLASS_POWER, - unit_of_measurement=POWER_WATT, # core:MeasuredValueType = core:ElectricalEnergyInWh (not for modbus:YutakiV2DHWElectricalEnergyConsumptionComponent) + native_unit_of_measurement=POWER_WATT, # core:MeasuredValueType = core:ElectricalEnergyInWh (not for modbus:YutakiV2DHWElectricalEnergyConsumptionComponent) state_class=STATE_CLASS_MEASUREMENT, ), OverkizSensorDescription( key="core:ConsumptionTariff1State", name="Consumption Tariff 1", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -150,7 +150,7 @@ key="core:ConsumptionTariff2State", name="Consumption Tariff 2", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -158,7 +158,7 @@ key="core:ConsumptionTariff3State", name="Consumption Tariff 3", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -166,7 +166,7 @@ key="core:ConsumptionTariff4State", name="Consumption Tariff 4", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -174,7 +174,7 @@ key="core:ConsumptionTariff5State", name="Consumption Tariff 5", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -182,7 +182,7 @@ key="core:ConsumptionTariff6State", name="Consumption Tariff 6", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -190,7 +190,7 @@ key="core:ConsumptionTariff7State", name="Consumption Tariff 7", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -198,7 +198,7 @@ key="core:ConsumptionTariff8State", name="Consumption Tariff 8", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -206,7 +206,7 @@ key="core:ConsumptionTariff9State", name="Consumption Tariff 9", device_class=sensor.DEVICE_CLASS_ENERGY, - unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh + native_unit_of_measurement=ENERGY_WATT_HOUR, # core:MeasuredValueType = core:ElectricalEnergyInWh entity_registry_enabled_default=False, state_class=STATE_CLASS_MEASUREMENT, ), @@ -214,18 +214,18 @@ OverkizSensorDescription( key="core:RelativeHumidityState", name="Relative Humidity", - value=lambda value: round(value, 2), + native_value=lambda value: round(value, 2), device_class=sensor.DEVICE_CLASS_HUMIDITY, - unit_of_measurement=PERCENTAGE, # core:MeasuredValueType = core:RelativeValueInPercentage + native_unit_of_measurement=PERCENTAGE, # core:MeasuredValueType = core:RelativeValueInPercentage state_class=STATE_CLASS_MEASUREMENT, ), # TemperatureSensor/TemperatureSensor OverkizSensorDescription( key="core:TemperatureState", name="Temperature", - value=lambda value: round(value, 2), + native_value=lambda value: round(value, 2), device_class=sensor.DEVICE_CLASS_TEMPERATURE, - unit_of_measurement=TEMP_CELSIUS, # core:MeasuredValueType = core:TemperatureInCelcius + native_unit_of_measurement=TEMP_CELSIUS, # core:MeasuredValueType = core:TemperatureInCelcius state_class=STATE_CLASS_MEASUREMENT, ), # WeatherSensor/WeatherForecastSensor @@ -237,14 +237,14 @@ key="core:MinimumTemperatureState", name="Minimum Temperature", device_class=sensor.DEVICE_CLASS_TEMPERATURE, - unit_of_measurement=TEMP_CELSIUS, + native_unit_of_measurement=TEMP_CELSIUS, state_class=STATE_CLASS_MEASUREMENT, ), OverkizSensorDescription( key="core:MaximumTemperatureState", name="Maximum Temperature", device_class=sensor.DEVICE_CLASS_TEMPERATURE, - unit_of_measurement=TEMP_CELSIUS, + native_unit_of_measurement=TEMP_CELSIUS, state_class=STATE_CLASS_MEASUREMENT, ), # AirSensor/COSensor @@ -252,7 +252,7 @@ key="core:COConcentrationState", name="CO Concentration", device_class=sensor.DEVICE_CLASS_CO, - unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, state_class=STATE_CLASS_MEASUREMENT, ), # AirSensor/CO2Sensor @@ -260,14 +260,14 @@ key="core:CO2ConcentrationState", name="CO2 Concentration", device_class=sensor.DEVICE_CLASS_CO2, - unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, state_class=STATE_CLASS_MEASUREMENT, ), # SunSensor/SunEnergySensor OverkizSensorDescription( key="core:SunEnergyState", name="Sun Energy", - value=lambda value: round(value, 2), + native_value=lambda value: round(value, 2), device_class=sensor.DEVICE_CLASS_ENERGY, icon="mdi:solar-power", state_class=STATE_CLASS_MEASUREMENT, @@ -276,7 +276,7 @@ OverkizSensorDescription( key="core:WindSpeedState", name="Wind Speed", - value=lambda value: round(value, 2), + native_value=lambda value: round(value, 2), icon="mdi:weather-windy", state_class=STATE_CLASS_MEASUREMENT, ), @@ -284,13 +284,13 @@ OverkizSensorDescription( key="io:SensorRoomState", name="Sensor Room", - value=lambda value: str(value).capitalize(), + native_value=lambda value: str(value).capitalize(), entity_registry_enabled_default=False, ), OverkizSensorDescription( key="io:PriorityLockOriginatorState", name="Priority Lock Originator", - value=lambda value: str(value).capitalize(), + native_value=lambda value: str(value).capitalize(), icon="mdi:lock", entity_registry_enabled_default=False, ), @@ -298,7 +298,7 @@ key="core:PriorityLockTimerState", name="Priority Lock Timer", icon="mdi:lock-clock", - unit_of_measurement=TIME_SECONDS, + native_unit_of_measurement=TIME_SECONDS, entity_registry_enabled_default=False, ), ] From 982bd918138db26c3d5a3dcf827f30342c24de83 Mon Sep 17 00:00:00 2001 From: Thibaut Etienne Date: Mon, 6 Sep 2021 07:25:32 +0000 Subject: [PATCH 2/2] Fix sensor state --- custom_components/tahoma/sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/tahoma/sensor.py b/custom_components/tahoma/sensor.py index afa81349d..b51e74987 100644 --- a/custom_components/tahoma/sensor.py +++ b/custom_components/tahoma/sensor.py @@ -345,7 +345,7 @@ def state(self): return None # Transform the value with a lambda function - if hasattr(self.entity_description, "value"): - return self.entity_description.value(state.value) + if hasattr(self.entity_description, "native_value"): + return self.entity_description.native_value(state.value) return state.value