From 6c42d36279a5438269f428a9be52f2135097a988 Mon Sep 17 00:00:00 2001 From: greyeee Date: Mon, 23 Dec 2024 03:14:11 +0000 Subject: [PATCH 1/4] Add get_basic_info implementation --- switchbot/devices/relay_switch.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/switchbot/devices/relay_switch.py b/switchbot/devices/relay_switch.py index 2d5845e..efd3445 100644 --- a/switchbot/devices/relay_switch.py +++ b/switchbot/devices/relay_switch.py @@ -17,6 +17,7 @@ COMMAND_TURN_ON = f"{COMMAND_HEADER}0f70010100" COMMAND_TOGGLE = f"{COMMAND_HEADER}0f70010200" COMMAND_GET_VOLTAGE_AND_CURRENT = f"{COMMAND_HEADER}0f7106000000" +COMMAND_GET_SWITCH_STATE = f"{COMMAND_HEADER}0f7101000000" PASSIVE_POLL_INTERVAL = 10 * 60 @@ -87,6 +88,16 @@ async def get_voltage_and_current(self) -> dict[str, Any] | None: } return None + async def get_basic_info(self) -> dict[str, Any] | None: + """Get the current state of the switch.""" + result = await self._send_command(COMMAND_GET_SWITCH_STATE) + ok = self._check_command_result(result, 0, {1}) + if ok: + return { + "is_on": result[9] & 0x01 == 0x01, + } + return None + def poll_needed(self, seconds_since_last_poll: float | None) -> bool: """Return if device needs polling.""" if self._force_next_update: From 6f181a5f2b457b158c812a26cd81305a2c5c3011 Mon Sep 17 00:00:00 2001 From: greyeee Date: Mon, 23 Dec 2024 04:03:13 +0000 Subject: [PATCH 2/4] Use another way --- switchbot/devices/relay_switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/switchbot/devices/relay_switch.py b/switchbot/devices/relay_switch.py index efd3445..9542164 100644 --- a/switchbot/devices/relay_switch.py +++ b/switchbot/devices/relay_switch.py @@ -94,7 +94,7 @@ async def get_basic_info(self) -> dict[str, Any] | None: ok = self._check_command_result(result, 0, {1}) if ok: return { - "is_on": result[9] & 0x01 == 0x01, + "is_on": result[1] & 0x01 != 0, } return None From 212c694acd4060d332c8eb356a5ba701c41b5ab3 Mon Sep 17 00:00:00 2001 From: greyeee Date: Mon, 23 Dec 2024 04:27:56 +0000 Subject: [PATCH 3/4] Add test --- tests/test_relay_switch.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_relay_switch.py b/tests/test_relay_switch.py index e3d2c89..973b31e 100644 --- a/tests/test_relay_switch.py +++ b/tests/test_relay_switch.py @@ -58,3 +58,11 @@ async def test_trun_off(): relay_switch_device._send_command = AsyncMock(return_value=b"\x01") await relay_switch_device.turn_off() assert relay_switch_device.is_on() is False + + +@pytest.mark.asyncio +async def test_get_basic_info(): + relay_switch_device = create_device_for_command_testing() + relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x01") + info = await relay_switch_device.get_basic_info() + assert info["is_on"] is True From bed8fc68dff331bf5bf21ad5b87649d5f1b6e231 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Dec 2024 19:30:34 -1000 Subject: [PATCH 4/4] tweaks --- switchbot/devices/relay_switch.py | 3 +-- tests/test_relay_switch.py | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/switchbot/devices/relay_switch.py b/switchbot/devices/relay_switch.py index 9542164..bf666d1 100644 --- a/switchbot/devices/relay_switch.py +++ b/switchbot/devices/relay_switch.py @@ -91,8 +91,7 @@ async def get_voltage_and_current(self) -> dict[str, Any] | None: async def get_basic_info(self) -> dict[str, Any] | None: """Get the current state of the switch.""" result = await self._send_command(COMMAND_GET_SWITCH_STATE) - ok = self._check_command_result(result, 0, {1}) - if ok: + if self._check_command_result(result, 0, {1}): return { "is_on": result[1] & 0x01 != 0, } diff --git a/tests/test_relay_switch.py b/tests/test_relay_switch.py index 973b31e..da9c884 100644 --- a/tests/test_relay_switch.py +++ b/tests/test_relay_switch.py @@ -53,7 +53,7 @@ async def test_turn_on(): @pytest.mark.asyncio -async def test_trun_off(): +async def test_turn_off(): relay_switch_device = create_device_for_command_testing() relay_switch_device._send_command = AsyncMock(return_value=b"\x01") await relay_switch_device.turn_off() @@ -66,3 +66,9 @@ async def test_get_basic_info(): relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x01") info = await relay_switch_device.get_basic_info() assert info["is_on"] is True + relay_switch_device._send_command = AsyncMock(return_value=b"\x01\x00") + info = await relay_switch_device.get_basic_info() + assert info["is_on"] is False + relay_switch_device._send_command = AsyncMock(return_value=b"\x00\x00") + info = await relay_switch_device.get_basic_info() + assert info is None