|
@@ -20,23 +20,37 @@ class Switchbot:
|
|
|
|
|
|
def __init__(self, mac) -> None:
|
|
def __init__(self, mac) -> None:
|
|
self._mac = mac
|
|
self._mac = mac
|
|
|
|
+ self._device = None
|
|
|
|
+ self._connect()
|
|
|
|
|
|
- def _sendpacket(self, key, retry=2) -> bool:
|
|
+ def _connect(self) -> bool:
|
|
|
|
+ if self._device is not None:
|
|
|
|
+ _LOGGER.debug("Disconnecting")
|
|
|
|
+ try:
|
|
|
|
+ self._device.disconnect()
|
|
|
|
+ except bluepy.btle.BTLEException:
|
|
|
|
+ pass
|
|
try:
|
|
try:
|
|
_LOGGER.debug("Connecting")
|
|
_LOGGER.debug("Connecting")
|
|
- device = bluepy.btle.Peripheral(self._mac,
|
|
+ self._device = bluepy.btle.Peripheral(self._mac,
|
|
- bluepy.btle.ADDR_TYPE_RANDOM)
|
|
+ bluepy.btle.ADDR_TYPE_RANDOM)
|
|
- hand_service = device.getServiceByUUID(UUID)
|
|
+ except bluepy.btle.BTLEException:
|
|
|
|
+ _LOGGER.error("Failed to connect to switchmate", exc_info=True)
|
|
|
|
+ return False
|
|
|
|
+ return True
|
|
|
|
+
|
|
|
|
+ def _sendpacket(self, key, retry=2) -> bool:
|
|
|
|
+ try:
|
|
|
|
+ _LOGGER.debug("Prepare to send")
|
|
|
|
+ hand_service = self._device.getServiceByUUID(UUID)
|
|
hand = hand_service.getCharacteristics(HANDLE)[0]
|
|
hand = hand_service.getCharacteristics(HANDLE)[0]
|
|
_LOGGER.debug("Sending command, %s", key)
|
|
_LOGGER.debug("Sending command, %s", key)
|
|
hand.write(binascii.a2b_hex(key))
|
|
hand.write(binascii.a2b_hex(key))
|
|
- _LOGGER.debug("Disconnecting")
|
|
|
|
- device.disconnect()
|
|
|
|
except bluepy.btle.BTLEException:
|
|
except bluepy.btle.BTLEException:
|
|
- _LOGGER.error("Cannot connect to switchbot. Retrying", exc_info=True)
|
|
+ if retry < 1 or not self._connect():
|
|
- if retry < 1:
|
|
|
|
_LOGGER.error("Cannot connect to switchbot.", exc_info=True)
|
|
_LOGGER.error("Cannot connect to switchbot.", exc_info=True)
|
|
return False
|
|
return False
|
|
|
|
+ _LOGGER.error("Cannot connect to switchbot. Retrying", exc_info=True)
|
|
return self._sendpacket(key, retry-1)
|
|
return self._sendpacket(key, retry-1)
|
|
return True
|
|
return True
|
|
|
|
|