Skip to content

Commit

Permalink
Allow sub-devices to enable debug for gateway if enabled.
Browse files Browse the repository at this point in the history
* Before only possible to enable debug only if the devices is an actual gateway.
  • Loading branch information
xZetsubou committed Apr 22, 2024
1 parent 07fa10a commit 4a4d67a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions custom_components/localtuya/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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())
Expand Down
22 changes: 12 additions & 10 deletions custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,17 +572,14 @@ 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""
self.listeners: dict[str, asyncio.Semaphore] = {}
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."""
Expand Down Expand Up @@ -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
Expand All @@ -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 = {}
Expand All @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit 4a4d67a

Please sign in to comment.