|
1 | 1 | """Library to handle connection with Switchbot"""
|
| 2 | +import time |
2 | 3 |
|
3 | 4 | import binascii
|
4 | 5 | import logging
|
5 | 6 |
|
6 | 7 | import bluepy
|
7 | 8 |
|
| 9 | +DEFAULT_RETRY_COUNT = 3 |
| 10 | +DEFAULT_RETRY_TIMEOUT = .2 |
| 11 | + |
8 | 12 | UUID = "cba20d00-224d-11e6-9fb8-0002a5d5c51b"
|
9 | 13 | HANDLE = "cba20002-224d-11e6-9fb8-0002a5d5c51b"
|
10 | 14 |
|
|
18 | 22 | class Switchbot:
|
19 | 23 | """Representation of a Switchbot."""
|
20 | 24 |
|
21 |
| - def __init__(self, mac) -> None: |
| 25 | + def __init__(self, mac, retry_count=DEFAULT_RETRY_COUNT) -> None: |
22 | 26 | self._mac = mac
|
23 | 27 | self._device = None
|
| 28 | + self._retry_count = retry_count |
24 | 29 |
|
25 |
| - def _connect(self) -> bool: |
| 30 | + def _connect(self) -> None: |
26 | 31 | if self._device is not None:
|
27 |
| - _LOGGER.debug("Disconnecting") |
28 |
| - try: |
29 |
| - self._device.disconnect() |
30 |
| - except bluepy.btle.BTLEException: |
31 |
| - pass |
| 32 | + return |
32 | 33 | try:
|
33 |
| - _LOGGER.debug("Connecting") |
| 34 | + _LOGGER.debug("Connecting to Switchbot...") |
34 | 35 | self._device = bluepy.btle.Peripheral(self._mac,
|
35 | 36 | bluepy.btle.ADDR_TYPE_RANDOM)
|
| 37 | + _LOGGER.debug("Connected to Switchbot.") |
36 | 38 | except bluepy.btle.BTLEException:
|
37 |
| - _LOGGER.warning("Failed to connect to Switchbot", exc_info=True) |
38 |
| - return False |
39 |
| - return True |
| 39 | + _LOGGER.debug("Failed connecting to Switchbot.", exc_info=True) |
| 40 | + self._device = None |
| 41 | + raise |
40 | 42 |
|
41 |
| - def _sendpacket(self, key, retry=2) -> bool: |
42 |
| - if self._device is None and not self._connect(): |
43 |
| - _LOGGER.error("Can not connect to switchbot.") |
44 |
| - return False |
| 43 | + def _disconnect(self) -> None: |
| 44 | + if self._device is None: |
| 45 | + return |
| 46 | + _LOGGER.debug("Disconnecting") |
| 47 | + try: |
| 48 | + self._device.disconnect() |
| 49 | + except bluepy.btle.BTLEException: |
| 50 | + _LOGGER.warning("Error disconnecting from Switchbot.", exc_info=True) |
| 51 | + finally: |
| 52 | + self._device = None |
| 53 | + |
| 54 | + def _writekey(self, key) -> bool: |
| 55 | + _LOGGER.debug("Prepare to send") |
| 56 | + hand_service = self._device.getServiceByUUID(UUID) |
| 57 | + hand = hand_service.getCharacteristics(HANDLE)[0] |
| 58 | + _LOGGER.debug("Sending command, %s", key) |
| 59 | + write_result = hand.write(binascii.a2b_hex(key), withResponse=True) |
| 60 | + if not write_result: |
| 61 | + _LOGGER.error("Sent command but didn't get a response from Switchbot confirming command was sent. " |
| 62 | + "Please check the Switchbot.") |
| 63 | + else: |
| 64 | + _LOGGER.info("Successfully sent command to Switchbot (MAC: %s).", self._mac) |
| 65 | + return write_result |
45 | 66 |
|
| 67 | + def _sendcommand(self, key, retry) -> bool: |
| 68 | + send_success = False |
46 | 69 | try:
|
47 |
| - _LOGGER.debug("Prepare to send") |
48 |
| - hand_service = self._device.getServiceByUUID(UUID) |
49 |
| - hand = hand_service.getCharacteristics(HANDLE)[0] |
50 |
| - _LOGGER.debug("Sending command, %s", key) |
51 |
| - hand.write(binascii.a2b_hex(key)) |
| 70 | + self._connect() |
| 71 | + send_success = self._writekey(key) |
52 | 72 | except bluepy.btle.BTLEException:
|
53 |
| - if retry < 1 or not self._connect(): |
54 |
| - _LOGGER.error("Can not connect to switchbot.", exc_info=True) |
55 |
| - return False |
56 |
| - _LOGGER.warning("Can not connect to switchbot. Retrying") |
57 |
| - return self._sendpacket(key, retry-1) |
58 |
| - return True |
59 |
| - |
60 |
| - def turn_on(self) -> None: |
| 73 | + _LOGGER.warning("Error talking to Switchbot.", exc_info=True) |
| 74 | + finally: |
| 75 | + self._disconnect() |
| 76 | + if send_success: |
| 77 | + return send_success |
| 78 | + if retry < 1: |
| 79 | + _LOGGER.error("Switchbot communication failed. Stopping trying.", exc_info=True) |
| 80 | + return False |
| 81 | + _LOGGER.warning("Cannot connect to Switchbot. Retrying (remaining: %d)...", retry) |
| 82 | + time.sleep(DEFAULT_RETRY_TIMEOUT) |
| 83 | + return self._sendcommand(key, retry - 1) |
| 84 | + |
| 85 | + def turn_on(self) -> bool: |
61 | 86 | """Turn device on."""
|
62 |
| - return self._sendpacket(ON_KEY) |
| 87 | + return self._sendcommand(ON_KEY, self._retry_count) |
63 | 88 |
|
64 |
| - def turn_off(self) -> None: |
| 89 | + def turn_off(self) -> bool: |
65 | 90 | """Turn device off."""
|
66 |
| - return self._sendpacket(OFF_KEY) |
| 91 | + return self._sendcommand(OFF_KEY, self._retry_count) |
67 | 92 |
|
68 |
| - def press(self) -> None: |
| 93 | + def press(self) -> bool: |
69 | 94 | """Press command to device."""
|
70 |
| - return self._sendpacket(PRESS_KEY) |
| 95 | + return self._sendcommand(PRESS_KEY, self._retry_count) |
0 commit comments