Browse Source

Implement SwitchbotRelaySwitch get_basic_info method (#283)

Co-authored-by: J. Nick Koston <nick@koston.org>
greyeee 3 months ago
parent
commit
db02c29f92
2 changed files with 25 additions and 1 deletions
  1. 10 0
      switchbot/devices/relay_switch.py
  2. 15 1
      tests/test_relay_switch.py

+ 10 - 0
switchbot/devices/relay_switch.py

@@ -17,6 +17,7 @@ COMMAND_TURN_OFF = f"{COMMAND_HEADER}0f70010000"
 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,15 @@ class SwitchbotRelaySwitch(SwitchbotEncryptedDevice):
             }
         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)
+        if self._check_command_result(result, 0, {1}):
+            return {
+                "is_on": result[1] & 0x01 != 0,
+            }
+        return None
+
     def poll_needed(self, seconds_since_last_poll: float | None) -> bool:
         """Return if device needs polling."""
         if self._force_next_update:

+ 15 - 1
tests/test_relay_switch.py

@@ -53,8 +53,22 @@ 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()
     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
+    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