Skip to content

Commit dbd221a

Browse files
committed
Use water_sense_mode device API for smart program on/off
Smart programs are controlled via the device's water_sense_mode setting ("auto" for on, "off" for off) rather than the program update API. Non-smart programs continue using update_program as before. Also adds update_device method to the BHyve API client.
1 parent 3e36d76 commit dbd221a

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

custom_components/bhyve/pybhyve/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ async def update_program(self, program_id: str, program: BHyveTimerProgram) -> N
249249
json = {"sprinkler_timer_program": program}
250250
await self._request("put", path, json=json)
251251

252+
async def update_device(self, device: dict) -> None:
253+
"""Update device settings."""
254+
device_id = device.get("id")
255+
path = f"{DEVICES_PATH}/{device_id}"
256+
json = {"device": device}
257+
await self._request("put", path, json=json)
258+
252259
async def send_message(self, payload: Any) -> None:
253260
"""Send a message via the websocket."""
254261
if self._websocket is not None:

custom_components/bhyve/switch.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,35 @@ def is_on(self) -> bool:
217217

218218
async def async_turn_on(self, **_kwargs: Any) -> None:
219219
"""Turn the switch on."""
220-
program = BHyveTimerProgram(self.program_data)
221-
program["enabled"] = True
222-
await self.coordinator.client.update_program(self._program_id, program)
223-
# Coordinator updates via WebSocket event
220+
if self.program_data.get("is_smart_program"):
221+
await self.coordinator.client.update_device(
222+
{
223+
"id": self._device_id,
224+
"type": self._device_type,
225+
"mac_address": self._mac_address,
226+
"water_sense_mode": "auto",
227+
}
228+
)
229+
else:
230+
program = BHyveTimerProgram(self.program_data)
231+
program["enabled"] = True
232+
await self.coordinator.client.update_program(self._program_id, program)
224233

225234
async def async_turn_off(self, **_kwargs: Any) -> None:
226235
"""Turn the switch off."""
227-
program = BHyveTimerProgram(self.program_data)
228-
program["enabled"] = False
229-
await self.coordinator.client.update_program(self._program_id, program)
230-
# Coordinator updates via WebSocket event
236+
if self.program_data.get("is_smart_program"):
237+
await self.coordinator.client.update_device(
238+
{
239+
"id": self._device_id,
240+
"type": self._device_type,
241+
"mac_address": self._mac_address,
242+
"water_sense_mode": "off",
243+
}
244+
)
245+
else:
246+
program = BHyveTimerProgram(self.program_data)
247+
program["enabled"] = False
248+
await self.coordinator.client.update_program(self._program_id, program)
231249

232250
async def start_program(self) -> None:
233251
"""Begins running a program."""

tests/test_switch.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def create_mock_coordinator(devices: dict, programs: dict) -> MagicMock:
3737
coordinator.client = MagicMock()
3838
coordinator.client.send_message = AsyncMock()
3939
coordinator.client.update_program = AsyncMock()
40+
coordinator.client.update_device = AsyncMock()
4041
return coordinator
4142

4243

@@ -412,14 +413,14 @@ async def test_program_switch_turn_on(
412413
description=description,
413414
)
414415

415-
# Turn on the switch
416+
# Turn on the switch (smart program uses update_device)
416417
await switch.async_turn_on()
417418

418-
# Verify update_program was called with enabled=True
419-
coordinator.client.update_program.assert_called_once()
420-
call_args = coordinator.client.update_program.call_args[0]
421-
assert call_args[0] == TEST_PROGRAM_ID
422-
assert call_args[1]["enabled"] is True
419+
# Verify update_device was called with water_sense_mode=auto
420+
coordinator.client.update_device.assert_called_once()
421+
call_args = coordinator.client.update_device.call_args[0][0]
422+
assert call_args["id"] == TEST_DEVICE_ID
423+
assert call_args["water_sense_mode"] == "auto"
423424

424425
async def test_program_switch_turn_off(
425426
self,

0 commit comments

Comments
 (0)