Skip to content

Commit 44ae30b

Browse files
Change garage door opener type to CoverDeviceClass.GARAGE in HA (#373)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 472eb22 commit 44ae30b

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

switchbot/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
from .devices.light_strip import SwitchbotLightStrip, SwitchbotStripLight3
4444
from .devices.lock import SwitchbotLock
4545
from .devices.plug import SwitchbotPlugMini
46-
from .devices.relay_switch import SwitchbotRelaySwitch, SwitchbotRelaySwitch2PM
46+
from .devices.relay_switch import (
47+
SwitchbotGarageDoorOpener,
48+
SwitchbotRelaySwitch,
49+
SwitchbotRelaySwitch2PM,
50+
)
4751
from .devices.roller_shade import SwitchbotRollerShade
4852
from .devices.vacuum import SwitchbotVacuum
4953
from .discovery import GetSwitchbotDevices
@@ -77,6 +81,7 @@
7781
"SwitchbotEncryptedDevice",
7882
"SwitchbotEvaporativeHumidifier",
7983
"SwitchbotFan",
84+
"SwitchbotGarageDoorOpener",
8085
"SwitchbotHumidifier",
8186
"SwitchbotLightStrip",
8287
"SwitchbotLock",

switchbot/devices/device.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class SwitchbotBaseDevice:
127127

128128
_turn_on_command: str | None = None
129129
_turn_off_command: str | None = None
130+
_open_command: str | None = None
131+
_close_command: str | None = None
130132
_press_command: str | None = None
131133

132134
def __init__(
@@ -719,6 +721,20 @@ async def turn_off(self) -> bool:
719721
result = await self._send_command(self._turn_off_command)
720722
return self._check_command_result(result, 0, {1})
721723

724+
@update_after_operation
725+
async def open(self) -> bool:
726+
"""Open the device."""
727+
self._check_function_support(self._open_command)
728+
result = await self._send_command(self._open_command)
729+
return self._check_command_result(result, 0, {1})
730+
731+
@update_after_operation
732+
async def close(self) -> bool:
733+
"""Close the device."""
734+
self._check_function_support(self._close_command)
735+
result = await self._send_command(self._close_command)
736+
return self._check_command_result(result, 0, {1})
737+
722738
@update_after_operation
723739
async def press(self) -> bool:
724740
"""Press the device."""

switchbot/devices/relay_switch.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class SwitchbotRelaySwitch(SwitchbotSequenceDevice, SwitchbotEncryptedDevice):
6161

6262
_turn_on_command = f"{COMMAND_CONTROL}010100"
6363
_turn_off_command = f"{COMMAND_CONTROL}010000"
64-
_press_command = f"{COMMAND_CONTROL}110329" # for garage door opener toggle
6564

6665
def __init__(
6766
self,
@@ -205,6 +204,29 @@ def is_on(self) -> bool | None:
205204
"""Return switch state from cache."""
206205
return self._get_adv_value("isOn")
207206

207+
def door_open(self) -> bool | None:
208+
"""Return garage door state from cache."""
209+
return self._get_adv_value("door_open")
210+
211+
212+
class SwitchbotGarageDoorOpener(SwitchbotRelaySwitch):
213+
"""Representation of a Switchbot garage door opener."""
214+
215+
_open_command = f"{COMMAND_CONTROL}110129"
216+
_close_command = f"{COMMAND_CONTROL}110229"
217+
_press_command = f"{COMMAND_CONTROL}110329" # for garage door opener toggle
218+
219+
def __init__(
220+
self,
221+
device: BLEDevice,
222+
key_id: str,
223+
encryption_key: str,
224+
interface: int = 0,
225+
model: SwitchbotModel = SwitchbotModel.GARAGE_DOOR_OPENER,
226+
**kwargs: Any,
227+
) -> None:
228+
super().__init__(device, key_id, encryption_key, interface, model, **kwargs)
229+
208230

209231
class SwitchbotRelaySwitch2PM(SwitchbotRelaySwitch):
210232
"""Representation of a Switchbot relay switch 2pm."""

tests/test_relay_switch.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ def create_device_for_command_testing(
3030
):
3131
"""Create a device for command testing."""
3232
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
33-
device_class = (
34-
relay_switch.SwitchbotRelaySwitch2PM
35-
if model == SwitchbotModel.RELAY_SWITCH_2PM
36-
else relay_switch.SwitchbotRelaySwitch
37-
)
33+
if model == SwitchbotModel.GARAGE_DOOR_OPENER:
34+
device_class = relay_switch.SwitchbotGarageDoorOpener
35+
elif model == SwitchbotModel.RELAY_SWITCH_2PM:
36+
device_class = relay_switch.SwitchbotRelaySwitch2PM
37+
else:
38+
device_class = relay_switch.SwitchbotRelaySwitch
3839
device = device_class(
3940
ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model
4041
)
@@ -453,12 +454,49 @@ def test_merge_data(old_data, new_data, expected_result):
453454
assert result == expected_result
454455

455456

457+
@pytest.mark.asyncio
458+
async def test_garage_door_opener_open():
459+
"""Test open the garage door."""
460+
device = create_device_for_command_testing(
461+
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
462+
)
463+
464+
await device.open()
465+
device._send_command.assert_awaited_once_with(device._open_command)
466+
467+
468+
@pytest.mark.asyncio
469+
async def test_garage_door_opener_close():
470+
"""Test close the garage door."""
471+
device = create_device_for_command_testing(
472+
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
473+
)
474+
475+
await device.close()
476+
device._send_command.assert_awaited_once_with(device._close_command)
477+
478+
479+
@pytest.mark.parametrize(
480+
"door_open",
481+
[
482+
True,
483+
False,
484+
],
485+
)
486+
@pytest.mark.asyncio
487+
async def test_garage_door_opener_door_open(door_open):
488+
"""Test get garage door state."""
489+
device = create_device_for_command_testing(
490+
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER, {"door_open": door_open}
491+
)
492+
assert device.door_open() is door_open
493+
494+
456495
@pytest.mark.asyncio
457496
async def test_press():
458497
"""Test the press command for garage door opener."""
459498
device = create_device_for_command_testing(
460499
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
461500
)
462-
463501
await device.press()
464502
device._send_command.assert_awaited_once_with(device._press_command)

0 commit comments

Comments
 (0)