Skip to content

Commit 0a00bc8

Browse files
Check if device is connected (#3)
* Check if device is connected * connect
1 parent feec093 commit 0a00bc8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

switchbot/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Switchbot:
2121
def __init__(self, mac) -> None:
2222
self._mac = mac
2323
self._device = None
24-
self._connect()
2524

2625
def _connect(self) -> bool:
2726
if self._device is not None:
@@ -40,6 +39,10 @@ def _connect(self) -> bool:
4039
return True
4140

4241
def _sendpacket(self, key, retry=2) -> bool:
42+
if self._device is None and not self._connect():
43+
_LOGGER.error("Cannot connect to switchbot.")
44+
return False
45+
4346
try:
4447
_LOGGER.debug("Prepare to send")
4548
hand_service = self._device.getServiceByUUID(UUID)

0 commit comments

Comments
 (0)