Skip to content

Commit 3dd4a7b

Browse files
fix: add extra logging
1 parent c251501 commit 3dd4a7b

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

custom_components/robovac/tuyalocalapi.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ async def async_send(self):
527527
await self.device._async_send(self)
528528

529529
@classmethod
530-
def from_bytes(cls, data, cipher=None):
530+
def from_bytes(cls, device, data, cipher=None):
531531
try:
532532
prefix, sequence, command, payload_size = struct.unpack_from(
533533
MESSAGE_PREFIX_FORMAT, data
@@ -584,15 +584,15 @@ def from_bytes(cls, data, cipher=None):
584584
try:
585585
payload_text = payload_data.decode("utf8")
586586
except UnicodeDecodeError as e:
587-
_LOGGER.debug(payload_data.hex())
588-
_LOGGER.error(e)
587+
device._LOGGER.debug(payload_data.hex())
588+
device._LOGGER.error(e)
589589
raise MessageDecodeFailed() from e
590590
try:
591591
payload = json.loads(payload_text)
592592
except json.decoder.JSONDecodeError as e:
593593
# data may be encrypted
594-
_LOGGER.debug(payload_data.hex())
595-
_LOGGER.error(e)
594+
device._LOGGER.debug(payload_data.hex())
595+
device._LOGGER.error(e)
596596
raise MessageDecodeFailed() from e
597597

598598
return cls(command, payload, sequence)
@@ -614,6 +614,7 @@ def __init__(
614614
version=(3, 3),
615615
):
616616
"""Initialize the device."""
617+
self._LOGGER = _LOGGER.getChild(device_id)
617618
self.device_id = device_id
618619
self.host = host
619620
self.port = port
@@ -668,7 +669,7 @@ async def process_queue(self):
668669
self.clean_queue()
669670

670671
if len(self._queue) > 0:
671-
_LOGGER.debug(
672+
self._LOGGER.debug(
672673
"Processing queue. Current length: {}".format(len(self._queue))
673674
)
674675
try:
@@ -679,14 +680,16 @@ async def process_queue(self):
679680
self._backoff = False
680681
except Exception as e:
681682
self._failures += 1
682-
_LOGGER.debug("{} failures. Most recent: {}".format(self._failures, e))
683+
self._LOGGER.debug(
684+
"{} failures. Most recent: {}".format(self._failures, e)
685+
)
683686
if self._failures > 3:
684687
self._backoff = True
685688
self._queue_interval = min(
686689
INITIAL_BACKOFF * (BACKOFF_MULTIPLIER ** (self._failures - 4)),
687690
600,
688691
)
689-
_LOGGER.warn(
692+
self._LOGGER.warn(
690693
"{} failures, backing off for {} seconds".format(
691694
self._failures, self._queue_interval
692695
)
@@ -709,7 +712,7 @@ async def async_connect(self):
709712

710713
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
711714
sock.settimeout(self.timeout)
712-
_LOGGER.debug("Connecting to {}".format(self))
715+
self._LOGGER.debug("Connecting to {}".format(self))
713716
try:
714717
sock.connect((self.host, self.port))
715718
except (socket.timeout, TimeoutError) as e:
@@ -734,7 +737,7 @@ async def async_disconnect(self):
734737
if self._connected is False:
735738
return
736739

737-
_LOGGER.debug("Disconnected from {}".format(self))
740+
self._LOGGER.debug("Disconnected from {}".format(self))
738741
self._connected = False
739742
self.last_pong = 0
740743

@@ -769,7 +772,7 @@ async def async_ping(self, ping_interval):
769772
return
770773

771774
if self._backoff is True:
772-
_LOGGER.debug("Currently in backoff, not adding ping to queue")
775+
self._LOGGER.debug("Currently in backoff, not adding ping to queue")
773776
else:
774777
self.last_ping = time.time()
775778
encrypt = False if self.version < (3, 3) else True
@@ -801,7 +804,7 @@ async def async_update_state(self, state_message, _=None):
801804
and state_message.payload["dps"]
802805
):
803806
self._dps.update(state_message.payload["dps"])
804-
_LOGGER.debug("Received updated state {}: {}".format(self, self._dps))
807+
self._LOGGER.debug("Received updated state {}: {}".format(self, self._dps))
805808

806809
@property
807810
def state(self):
@@ -821,21 +824,23 @@ async def _async_handle_message(self):
821824
)
822825
await self._response_task
823826
response_data = self._response_task.result()
824-
message = Message.from_bytes(response_data, self.cipher)
827+
message = Message.from_bytes(self, response_data, self.cipher)
825828
except Exception as e:
826829
if isinstance(e, InvalidMessage):
827-
_LOGGER.debug("Invalid message from {}: {}".format(self, e))
830+
self._LOGGER.debug("Invalid message from {}: {}".format(self, e))
828831
elif isinstance(e, MessageDecodeFailed):
829-
_LOGGER.debug("Failed to decrypt message from {}".format(self))
832+
self._LOGGER.debug("Failed to decrypt message from {}".format(self))
830833
elif isinstance(e, asyncio.IncompleteReadError):
831834
if self._connected:
832-
_LOGGER.debug("Incomplete read")
835+
self._LOGGER.debug("Incomplete read")
833836
elif isinstance(e, ConnectionResetError):
834-
_LOGGER.debug("Connection reset: {}".format(e))
837+
self._LOGGER.debug(
838+
"Connection reset: {}\n{}".format(e, e.__traceback__)
839+
)
835840
await self.async_disconnect()
836841

837842
else:
838-
_LOGGER.debug("Received message from {}: {}".format(self, message))
843+
self._LOGGER.debug("Received message from {}: {}".format(self, message))
839844
if message.sequence in self._listeners:
840845
sem = self._listeners[message.sequence]
841846
if isinstance(sem, asyncio.Semaphore):
@@ -850,7 +855,7 @@ async def _async_handle_message(self):
850855
asyncio.create_task(self._async_handle_message())
851856

852857
async def _async_send(self, message, retries=2):
853-
_LOGGER.debug("Sending to {}: {}".format(self, message))
858+
self._LOGGER.debug("Sending to {}: {}".format(self, message))
854859
try:
855860
await self.async_connect()
856861
self.writer.write(message.bytes())
@@ -871,19 +876,19 @@ async def _async_send(self, message, retries=2):
871876
raise TuyaException("Failed to send data to {}".format(self))
872877

873878
if isinstance(e, socket.error):
874-
_LOGGER.debug(
879+
self._LOGGER.debug(
875880
"Retrying send due to error. Connection to {} failed: {}".format(
876881
self, e
877882
)
878883
)
879884
elif isinstance(e, asyncio.IncompleteReadError):
880-
_LOGGER.debug(
885+
self._LOGGER.debug(
881886
"Retrying send due to error. Incomplete read from: {} : {}. Partial data recieved: {}".format(
882887
self, e, e.partial
883888
)
884889
)
885890
else:
886-
_LOGGER.debug(
891+
self._LOGGER.debug(
887892
"Retrying send due to error. Failed to send data to {}".format(self)
888893
)
889894
await asyncio.sleep(0.25)

0 commit comments

Comments
 (0)