Browse Source

Check if device is connected (#3)

* Check if device is connected

* connect
Daniel Høyer Iversen 5 years ago
parent
commit
0a00bc84dd
1 changed files with 4 additions and 1 deletions
  1. 4 1
      switchbot/__init__.py

+ 4 - 1
switchbot/__init__.py

@@ -21,7 +21,6 @@ class Switchbot:
     def __init__(self, mac) -> None:
         self._mac = mac
         self._device = None
-        self._connect()
 
     def _connect(self) -> bool:
         if self._device is not None:
@@ -40,6 +39,10 @@ class Switchbot:
         return True
 
     def _sendpacket(self, key, retry=2) -> bool:
+        if self._device is None and not self._connect():
+            _LOGGER.error("Cannot connect to switchbot.")
+            return False
+
         try:
             _LOGGER.debug("Prepare to send")
             hand_service = self._device.getServiceByUUID(UUID)