diff --git a/custom_components/localtuya/coordinator.py b/custom_components/localtuya/coordinator.py index f1aaff40..96fc0065 100644 --- a/custom_components/localtuya/coordinator.py +++ b/custom_components/localtuya/coordinator.py @@ -166,6 +166,7 @@ async def _make_connection(self): if not gateway or (not gateway.connected and gateway.is_connecting): return await self.abort_connect() self._interface = gateway._interface + self._interface.enable_debug(self._device_config.enable_debug) else: self._interface = await asyncio.wait_for( pytuya.connect( @@ -248,6 +249,7 @@ def _new_entity_handler(entity_id): self._connect_task = None self.debug(f"Success: connected to {host}", force=True) + if self._sub_devices: for subdevice in self._sub_devices.values(): self._hass.async_create_task(subdevice.async_connect()) diff --git a/custom_components/localtuya/core/pytuya/__init__.py b/custom_components/localtuya/core/pytuya/__init__.py index 1e56fc2d..89ef80f0 100644 --- a/custom_components/localtuya/core/pytuya/__init__.py +++ b/custom_components/localtuya/core/pytuya/__init__.py @@ -572,9 +572,7 @@ class MessageDispatcher(ContextualLogger): RESET_SEQNO = -101 SESS_KEY_SEQNO = -102 - def __init__( - self, dev_id, callback_status_update, protocol_version, local_key, enable_debug - ): + def __init__(self, dev_id, callback_status_update, protocol_version, local_key): """Initialize a new MessageBuffer.""" super().__init__() self.buffer = b"" @@ -582,7 +580,6 @@ def __init__( self.callback_status_update = callback_status_update self.version = protocol_version self.local_key = local_key - self.set_logger(_LOGGER, dev_id, enable_debug) def abort(self): """Abort all waiting clients.""" @@ -758,7 +755,6 @@ def __init__( """ super().__init__() self.loop = asyncio.get_running_loop() - self.set_logger(_LOGGER, dev_id, enable_debug) self.id = dev_id self.local_key = local_key.encode("latin1") self.real_local_key = self.local_key @@ -776,7 +772,7 @@ def __init__( self.seqno = 1 self.transport = None self.listener = weakref.ref(listener) - self.dispatcher = self._setup_dispatcher(enable_debug) + self.dispatcher = self._setup_dispatcher() self.on_connected = on_connected self.heartbeater = None self.dps_cache = {} @@ -785,6 +781,7 @@ def __init__( self.dps_whitelist = UPDATE_DPS_WHITELIST self.dispatched_dps = {} # Store payload so we can trigger an event in HA. self._last_command_sent = 1 + self.enable_debug(enable_debug) def set_version(self, protocol_version): """Set the device version and eventually start available DPs detection.""" @@ -812,7 +809,7 @@ def error_json(self, number=None, payload=None): return json.loads('{ "Error":"%s", "Err":"%s", "Payload":%s }' % vals) - def _setup_dispatcher(self, enable_debug) -> MessageDispatcher: + def _setup_dispatcher(self) -> MessageDispatcher: def _status_update(msg): if msg.seqno > 0: self.seqno = msg.seqno + 1 @@ -840,9 +837,7 @@ def _status_update(msg): listener.status_updated(device) - return MessageDispatcher( - self.id, _status_update, self.version, self.local_key, enable_debug - ) + return MessageDispatcher(self.id, _status_update, self.version, self.local_key) def connection_made(self, transport): """Did connect to the device.""" @@ -1461,6 +1456,13 @@ def deepcopy_dict(_dict: dict): return MessagePayload(command_override, payload) + def enable_debug(self, enable=False): + """Enable the debug logs for the device.""" + if enable: + self.set_logger(_LOGGER, self.id, enable) + self.dispatcher.set_logger(_LOGGER, self.id, enable) + self.info(f"Enabled debug for device: {self.id}") + @property def last_command_sent(self): """Return last command sent by seconds"""