diff --git a/custom_components/wundasmart/sensor.py b/custom_components/wundasmart/sensor.py index f022e06..d7240e3 100644 --- a/custom_components/wundasmart/sensor.py +++ b/custom_components/wundasmart/sensor.py @@ -1,5 +1,6 @@ """Support for WundaSmart sensors.""" from __future__ import annotations +from dataclasses import dataclass import itertools from homeassistant.core import HomeAssistant, callback @@ -29,8 +30,15 @@ def _number_or_none(x): return None -ROOM_SENSORS: list[SensorEntityDescription] = [ - SensorEntityDescription( +@dataclass +class WundaSensorDescription(SensorEntityDescription): + available: bool | callable = True + + + + +ROOM_SENSORS: list[WundaSensorDescription] = [ + WundaSensorDescription( key="temp", name="Temperature", icon="mdi:thermometer", @@ -38,7 +46,7 @@ def _number_or_none(x): device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="rh", name="Humidity", icon="mdi:water-percent", @@ -46,7 +54,16 @@ def _number_or_none(x): device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( + key="temp_ext", + name="External Probe Temperature", + icon="mdi:thermometer", + available=lambda state: bool(int(state.get("ext", 0))), + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + ), + WundaSensorDescription( key="bat", name="Battery Level", icon=lambda x: icon_for_battery_level(_number_or_none(x)), @@ -54,7 +71,7 @@ def _number_or_none(x): device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="sig", name="Signal Level", icon=lambda x: icon_for_signal_level(_number_or_none(x)), @@ -64,8 +81,8 @@ def _number_or_none(x): ) ] -TRV_SENSORS: list[SensorEntityDescription] = [ - SensorEntityDescription( +TRV_SENSORS: list[WundaSensorDescription] = [ + WundaSensorDescription( key="vtemp", name="Temperature", icon="mdi:thermometer", @@ -73,7 +90,7 @@ def _number_or_none(x): device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="bat", name="Battery Level", icon=lambda x: icon_for_battery_level(_number_or_none(x)), @@ -81,7 +98,7 @@ def _number_or_none(x): device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="sig", name="Signal Level", icon=lambda x: icon_for_signal_level(_number_or_none(x)), @@ -89,27 +106,27 @@ def _number_or_none(x): device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="vpos", name="Position", state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="vpos_min", name="Position Min", state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="vpos_range", name="Position Range", state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="downforce", name="Downforce", state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + WundaSensorDescription( key="trv_range", name="TRV Range", state_class=SensorStateClass.MEASUREMENT, @@ -202,10 +219,17 @@ def __init__( # Update with initial state self.__update_state() + @property + def available(self): + if callable(self.entity_description.available): + device = self.coordinator.data.get(self._wunda_id, {}) + state = device.get("state", {}) + return self.entity_description.available(state) + return self.entity_description.available + def __update_state(self): device = self.coordinator.data.get(self._wunda_id, {}) state = device.get("state", {}) - self._attr_available = True self._attr_native_value = state.get(self.entity_description.key) @callback diff --git a/tests/fixtures/test_get_devices1.json b/tests/fixtures/test_get_devices1.json index 4923652..c89c694 100644 --- a/tests/fixtures/test_get_devices1.json +++ b/tests/fixtures/test_get_devices1.json @@ -23,8 +23,8 @@ "v": "1.8", "temp": "17.8", "rh": "66.57", - "temp_ext": "", - "ext": "0", + "temp_ext": "18.0", + "ext": "1", "bat": "100", "sig": "88", "alarm": "0" @@ -55,7 +55,7 @@ "t_norm": "19.00", "t_hi": "21.00", "heat": "4", - "temp_pre": "0", + "temp_pre": "4", "prg": "1", "lock": "0", "temp": "0.00", diff --git a/tests/test_sensor.py b/tests/test_sensor.py index abefff4..ca07441 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -37,6 +37,10 @@ async def test_sensors(hass: HomeAssistant, config): assert trv_signal_state.state == "88" assert trv_signal_state.attributes["icon"] == "mdi:signal-cellular-3" + ext_temp_state = hass.states.get("sensor.test_room_external_probe_temperature") + assert ext_temp_state + assert ext_temp_state.state == "18.0" + coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] assert coordinator