Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions switchbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,15 +18,21 @@
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"

_LOGGER = logging.getLogger(__name__)


class Switchbot:
"""Representation of a Switchbot."""
class SwitchbotDevice:
# 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
Expand Down Expand Up @@ -104,6 +110,10 @@ def _sendcommand(self, key, retry) -> bool:
time.sleep(DEFAULT_RETRY_TIMEOUT)
return self._sendcommand(key, retry - 1)


class Switchbot(SwitchbotDevice):
"""Representation of a Switchbot."""

def turn_on(self) -> bool:
"""Turn device on."""
return self._sendcommand(ON_KEY, self._retry_count)
Expand All @@ -115,3 +125,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):
"""Representation of a Switchbot Curtain."""

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 (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)