diff --git a/switchbot/__init__.py b/switchbot/__init__.py index 99de4b6a..ccd44651 100644 --- a/switchbot/__init__.py +++ b/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 @@ def _connect(self) -> bool: 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)