Skip to content

Commit db02c29

Browse files
greyeeebdraco
andauthored
Implement SwitchbotRelaySwitch get_basic_info method (#283)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 785377c commit db02c29

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

switchbot/devices/relay_switch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
COMMAND_TURN_ON = f"{COMMAND_HEADER}0f70010100"
1818
COMMAND_TOGGLE = f"{COMMAND_HEADER}0f70010200"
1919
COMMAND_GET_VOLTAGE_AND_CURRENT = f"{COMMAND_HEADER}0f7106000000"
20+
COMMAND_GET_SWITCH_STATE = f"{COMMAND_HEADER}0f7101000000"
2021
PASSIVE_POLL_INTERVAL = 10 * 60
2122

2223

@@ -87,6 +88,15 @@ async def get_voltage_and_current(self) -> dict[str, Any] | None:
8788
}
8889
return None
8990

91+
async def get_basic_info(self) -> dict[str, Any] | None:
92+
"""Get the current state of the switch."""
93+
result = await self._send_command(COMMAND_GET_SWITCH_STATE)
94+
if self._check_command_result(result, 0, {1}):
95+
return {
96+
"is_on": result[1] & 0x01 != 0,
97+
}
98+
return None
99+
90100
def poll_needed(self, seconds_since_last_poll: float | None) -> bool:
91101
"""Return if device needs polling."""
92102
if self._force_next_update:

tests/test_relay_switch.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,22 @@ async def test_turn_on():
5353

5454

5555
@pytest.mark.asyncio
56-
async def test_trun_off():
56+
async def test_turn_off():
5757
relay_switch_device = create_device_for_command_testing()
5858
relay_switch_device._send_command = AsyncMock(return_value=b"\x01")
5959
await relay_switch_device.turn_off()
6060
assert relay_switch_device.is_on() is False
61+
62+
63+
@pytest.mark.asyncio
64+
async def test_get_basic_info():
65+
relay_switch_device = create_device_for_command_testing()
66+
relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x01")
67+
info = await relay_switch_device.get_basic_info()
68+
assert info["is_on"] is True
69+
relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x00")
70+
info = await relay_switch_device.get_basic_info()
71+
assert info["is_on"] is False
72+
relay_switch_device._send_command = AsyncMock(return_value=b"\x00\x00")
73+
info = await relay_switch_device.get_basic_info()
74+
assert info is None

0 commit comments

Comments
 (0)