From e532da87123fbb8210e187eeb91965e6d688b2dc Mon Sep 17 00:00:00 2001 From: Daniel Claes Date: Fri, 18 Dec 2020 14:22:29 +0100 Subject: [PATCH 1/2] feature: curtain support --- switchbot/__init__.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/switchbot/__init__.py b/switchbot/__init__.py index a2d7e228..5f914a80 100644 --- a/switchbot/__init__.py +++ b/switchbot/__init__.py @@ -18,6 +18,11 @@ ON_KEY = "570101" OFF_KEY = "570102" +OPEN_KEY = '570f450105ff00' #570F4501010100 +CLOSE_KEY = '570f450105ff64' #570F4501010164 +POSITION_KEY = '570F450105ff' # +actual_position ex: 570F450105ff32 for 50% +STOP_KEY = '570F45010001' + ON_KEY_SUFFIX = "01" OFF_KEY_SUFFIX = "02" PRESS_KEY_SUFFIX = "00" @@ -25,7 +30,7 @@ _LOGGER = logging.getLogger(__name__) -class Switchbot: +class SwitchbotDevice: """Representation of a Switchbot.""" def __init__(self, mac, retry_count=DEFAULT_RETRY_COUNT, password=None) -> None: @@ -104,6 +109,9 @@ def _sendcommand(self, key, retry) -> bool: time.sleep(DEFAULT_RETRY_TIMEOUT) return self._sendcommand(key, retry - 1) + +class Switchbot(SwitchbotDevice): + def turn_on(self) -> bool: """Turn device on.""" return self._sendcommand(ON_KEY, self._retry_count) @@ -115,3 +123,24 @@ def turn_off(self) -> bool: def press(self) -> bool: """Press command to device.""" return self._sendcommand(PRESS_KEY, self._retry_count) + + +class SwitchbotCurtain(SwitchbotDevice): + + def open(self) -> bool: + """Send open command.""" + return self._sendcommand(OPEN_KEY, self._retry_count) + + def close(self) -> bool: + """Send close command.""" + return self._sendcommand(CLOSE_KEY, self._retry_count) + + def stop(self) -> bool: + """Send stop command to device.""" + return self._sendcommand(STOP_KEY, self._retry_count) + + def set_position(self, position: int) -> bool: + """Send position command to device.""" + hex_position = "%0.2X" % (100 - position) # curtain position in reverse mode + return self._sendcommand(POSITION_KEY + hex_position, self._retry_count) + From 7469d206c865f7e7bb930481f84850a75536c081 Mon Sep 17 00:00:00 2001 From: Daniel Claes Date: Fri, 18 Dec 2020 17:47:36 +0100 Subject: [PATCH 2/2] fix: pylint and docsstrings --- switchbot/__init__.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/switchbot/__init__.py b/switchbot/__init__.py index 5f914a80..ff1e01dd 100644 --- a/switchbot/__init__.py +++ b/switchbot/__init__.py @@ -7,7 +7,7 @@ import bluepy DEFAULT_RETRY_COUNT = 3 -DEFAULT_RETRY_TIMEOUT = .2 +DEFAULT_RETRY_TIMEOUT = 0.2 UUID = "cba20d00-224d-11e6-9fb8-0002a5d5c51b" HANDLE = "cba20002-224d-11e6-9fb8-0002a5d5c51b" @@ -18,10 +18,10 @@ ON_KEY = "570101" OFF_KEY = "570102" -OPEN_KEY = '570f450105ff00' #570F4501010100 -CLOSE_KEY = '570f450105ff64' #570F4501010164 -POSITION_KEY = '570F450105ff' # +actual_position ex: 570F450105ff32 for 50% -STOP_KEY = '570F45010001' +OPEN_KEY = "570f450105ff00" # 570F4501010100 +CLOSE_KEY = "570f450105ff64" # 570F4501010164 +POSITION_KEY = "570F450105ff" # +actual_position ex: 570F450105ff32 for 50% +STOP_KEY = "570F45010001" ON_KEY_SUFFIX = "01" OFF_KEY_SUFFIX = "02" @@ -31,7 +31,8 @@ class SwitchbotDevice: - """Representation of a Switchbot.""" + # pylint: disable=too-few-public-methods + """Base Representation of a Switchbot Device.""" def __init__(self, mac, retry_count=DEFAULT_RETRY_COUNT, password=None) -> None: self._mac = mac @@ -111,6 +112,7 @@ def _sendcommand(self, key, retry) -> bool: class Switchbot(SwitchbotDevice): + """Representation of a Switchbot.""" def turn_on(self) -> bool: """Turn device on.""" @@ -126,6 +128,7 @@ def press(self) -> bool: class SwitchbotCurtain(SwitchbotDevice): + """Representation of a Switchbot Curtain.""" def open(self) -> bool: """Send open command.""" @@ -140,7 +143,6 @@ def stop(self) -> bool: return self._sendcommand(STOP_KEY, self._retry_count) def set_position(self, position: int) -> bool: - """Send position command to device.""" - hex_position = "%0.2X" % (100 - position) # curtain position in reverse mode + """Send position command (0-100) to device.""" + hex_position = "%0.2X" % (100 - position) # curtain position in reverse mode return self._sendcommand(POSITION_KEY + hex_position, self._retry_count) -