77import bluepy
88
99DEFAULT_RETRY_COUNT = 3
10- DEFAULT_RETRY_TIMEOUT = .2
10+ DEFAULT_RETRY_TIMEOUT = 0 .2
1111
1212UUID = "cba20d00-224d-11e6-9fb8-0002a5d5c51b"
1313HANDLE = "cba20002-224d-11e6-9fb8-0002a5d5c51b"
1818ON_KEY = "570101"
1919OFF_KEY = "570102"
2020
21+ OPEN_KEY = "570f450105ff00" # 570F4501010100
22+ CLOSE_KEY = "570f450105ff64" # 570F4501010164
23+ POSITION_KEY = "570F450105ff" # +actual_position ex: 570F450105ff32 for 50%
24+ STOP_KEY = "570F45010001"
25+
2126ON_KEY_SUFFIX = "01"
2227OFF_KEY_SUFFIX = "02"
2328PRESS_KEY_SUFFIX = "00"
2429
2530_LOGGER = logging .getLogger (__name__ )
2631
2732
28- class Switchbot :
29- """Representation of a Switchbot."""
33+ class SwitchbotDevice :
34+ # pylint: disable=too-few-public-methods
35+ """Base Representation of a Switchbot Device."""
3036
3137 def __init__ (self , mac , retry_count = DEFAULT_RETRY_COUNT , password = None ) -> None :
3238 self ._mac = mac
@@ -104,6 +110,10 @@ def _sendcommand(self, key, retry) -> bool:
104110 time .sleep (DEFAULT_RETRY_TIMEOUT )
105111 return self ._sendcommand (key , retry - 1 )
106112
113+
114+ class Switchbot (SwitchbotDevice ):
115+ """Representation of a Switchbot."""
116+
107117 def turn_on (self ) -> bool :
108118 """Turn device on."""
109119 return self ._sendcommand (ON_KEY , self ._retry_count )
@@ -115,3 +125,24 @@ def turn_off(self) -> bool:
115125 def press (self ) -> bool :
116126 """Press command to device."""
117127 return self ._sendcommand (PRESS_KEY , self ._retry_count )
128+
129+
130+ class SwitchbotCurtain (SwitchbotDevice ):
131+ """Representation of a Switchbot Curtain."""
132+
133+ def open (self ) -> bool :
134+ """Send open command."""
135+ return self ._sendcommand (OPEN_KEY , self ._retry_count )
136+
137+ def close (self ) -> bool :
138+ """Send close command."""
139+ return self ._sendcommand (CLOSE_KEY , self ._retry_count )
140+
141+ def stop (self ) -> bool :
142+ """Send stop command to device."""
143+ return self ._sendcommand (STOP_KEY , self ._retry_count )
144+
145+ def set_position (self , position : int ) -> bool :
146+ """Send position command (0-100) to device."""
147+ hex_position = "%0.2X" % (100 - position ) # curtain position in reverse mode
148+ return self ._sendcommand (POSITION_KEY + hex_position , self ._retry_count )
0 commit comments