Prechádzať zdrojové kódy

Add press function for garage door opener (#365)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Retha Runolfsson 1 deň pred
rodič
commit
02174ed12c

+ 6 - 1
switchbot/__init__.py

@@ -32,7 +32,11 @@ from .devices.bot import Switchbot
 from .devices.bulb import SwitchbotBulb
 from .devices.ceiling_light import SwitchbotCeilingLight
 from .devices.curtain import SwitchbotCurtain
-from .devices.device import SwitchbotDevice, SwitchbotEncryptedDevice
+from .devices.device import (
+    SwitchbotDevice,
+    SwitchbotEncryptedDevice,
+    SwitchbotOperationError,
+)
 from .devices.evaporative_humidifier import SwitchbotEvaporativeHumidifier
 from .devices.fan import SwitchbotFan
 from .devices.humidifier import SwitchbotHumidifier
@@ -78,6 +82,7 @@ __all__ = [
     "SwitchbotLock",
     "SwitchbotModel",
     "SwitchbotModel",
+    "SwitchbotOperationError",
     "SwitchbotPlugMini",
     "SwitchbotPlugMini",
     "SwitchbotRelaySwitch",

+ 8 - 0
switchbot/devices/device.py

@@ -127,6 +127,7 @@ class SwitchbotBaseDevice:
 
     _turn_on_command: str | None = None
     _turn_off_command: str | None = None
+    _press_command: str | None = None
 
     def __init__(
         self,
@@ -718,6 +719,13 @@ class SwitchbotBaseDevice:
         result = await self._send_command(self._turn_off_command)
         return self._check_command_result(result, 0, {1})
 
+    @update_after_operation
+    async def press(self) -> bool:
+        """Press the device."""
+        self._check_function_support(self._press_command)
+        result = await self._send_command(self._press_command)
+        return self._check_command_result(result, 0, {1})
+
 
 class SwitchbotDevice(SwitchbotBaseDevice):
     """

+ 1 - 0
switchbot/devices/relay_switch.py

@@ -61,6 +61,7 @@ class SwitchbotRelaySwitch(SwitchbotSequenceDevice, SwitchbotEncryptedDevice):
 
     _turn_on_command = f"{COMMAND_CONTROL}010100"
     _turn_off_command = f"{COMMAND_CONTROL}010000"
+    _press_command = f"{COMMAND_CONTROL}110329"  # for garage door opener toggle
 
     def __init__(
         self,

+ 11 - 0
tests/test_relay_switch.py

@@ -451,3 +451,14 @@ def test_merge_data(old_data, new_data, expected_result):
     """Test merging of data dictionaries."""
     result = merge_data(old_data, new_data)
     assert result == expected_result
+
+
+@pytest.mark.asyncio
+async def test_press():
+    """Test the press command for garage door opener."""
+    device = create_device_for_command_testing(
+        b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
+    )
+
+    await device.press()
+    device._send_command.assert_awaited_once_with(device._press_command)