Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions custom_components/wiser/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,13 @@ def __init__(self, coordinator, device_id=0, sensor_type="") -> None:
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
_LOGGER.debug(f"{self._sensor_type} device update requested")
HeatHub = self._data.wiserhub.system.name
HeatHub = HeatHub.replace("WiserHeat","HeatHub")
return f"{HeatHub} {self._sensor_type}"

self._state = getattr(
self._data.wiserhub.system,
self._sensor_type.replace(" ", "_").lower(),
None,
)
self.async_write_ha_state()

@property
def is_on(self):
"""Return the state of the sensor."""
Expand Down
60 changes: 60 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,66 @@ def guard_for(function_name: str) -> ast.If:
self.assertIsNotNone(cleanup_call)


class WiserSystemBinarySensorUpdateTest(unittest.TestCase):
"""Tests for coordinator updates to system binary sensors."""

@classmethod
def setUpClass(cls) -> None:
source = BINARY_SENSOR_SOURCE_PATH.read_text().split(
"class RoomBinarySensor", 1
)[0]
tree = ast.parse(source)
system_class = next(
node
for node in tree.body
if isinstance(node, ast.ClassDef)
and node.name == "SystemBinarySensor"
)
update_method = next(
node
for node in system_class.body
if isinstance(node, ast.FunctionDef)
and node.name == "_handle_coordinator_update"
)
update_method.decorator_list = []
namespace = {"_LOGGER": Mock()}
exec(
compile(
ast.Module(body=[update_method], type_ignores=[]),
BINARY_SENSOR_SOURCE_PATH.name,
"exec",
),
namespace,
)
cls.handle_update = staticmethod(namespace["_handle_coordinator_update"])

def setUp(self) -> None:
self.sensor = SimpleNamespace(
_data=SimpleNamespace(
wiserhub=SimpleNamespace(
system=SimpleNamespace(summer_comfort_available=False)
)
),
_sensor_type="Summer Comfort Available",
_state=True,
async_write_ha_state=Mock(),
)

def test_coordinator_update_refreshes_state(self) -> None:
self.handle_update(self.sensor)

self.assertFalse(self.sensor._state)
self.sensor.async_write_ha_state.assert_called_once_with()

def test_missing_property_sets_unknown_state(self) -> None:
self.sensor._data.wiserhub.system = SimpleNamespace()

self.handle_update(self.sensor)

self.assertIsNone(self.sensor._state)
self.sensor.async_write_ha_state.assert_called_once_with()


class WiserDeviceSignalSensorNameTest(unittest.TestCase):
"""Tests for controller and device signal sensor names."""

Expand Down