Skip to content

Commit

Permalink
Add sensor for external temperature probes
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
tonyroberts committed Nov 14, 2023
1 parent b5c2b72 commit 568d7ea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
54 changes: 39 additions & 15 deletions custom_components/wundasmart/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for WundaSmart sensors."""
from __future__ import annotations
from dataclasses import dataclass
import itertools

from homeassistant.core import HomeAssistant, callback
Expand Down Expand Up @@ -29,32 +30,48 @@ 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",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
WundaSensorDescription(
key="rh",
name="Humidity",
icon="mdi:water-percent",
native_unit_of_measurement=PERCENTAGE,
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)),
native_unit_of_measurement=PERCENTAGE,
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)),
Expand All @@ -64,52 +81,52 @@ def _number_or_none(x):
)
]

TRV_SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription(
TRV_SENSORS: list[WundaSensorDescription] = [
WundaSensorDescription(
key="vtemp",
name="Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
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)),
native_unit_of_measurement=PERCENTAGE,
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)),
native_unit_of_measurement=PERCENTAGE,
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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/test_get_devices1.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 568d7ea

Please sign in to comment.