From 69b4c42cf38bf9fe77da25556e14f0d83172d2d2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 3 Jul 2023 19:54:22 -0500 Subject: [PATCH] Add checks for debug logging in the write path (#458) --- aioesphomeapi/_frame_helper.py | 3 ++- aioesphomeapi/connection.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/aioesphomeapi/_frame_helper.py b/aioesphomeapi/_frame_helper.py index c6d37c5..ed2040f 100644 --- a/aioesphomeapi/_frame_helper.py +++ b/aioesphomeapi/_frame_helper.py @@ -139,7 +139,8 @@ class APIPlaintextFrameHelper(APIFrameHelper): """ assert self._transport is not None, "Transport should be set" data = b"\0" + varuint_to_bytes(len(data)) + varuint_to_bytes(type_) + data - _LOGGER.debug("Sending plaintext frame %s", data.hex()) + if _LOGGER.isEnabledFor(logging.DEBUG): + _LOGGER.debug("Sending plaintext frame %s", data.hex()) try: self._transport.write(data) diff --git a/aioesphomeapi/connection.py b/aioesphomeapi/connection.py index b48b7d3..365de5a 100644 --- a/aioesphomeapi/connection.py +++ b/aioesphomeapi/connection.py @@ -531,7 +531,11 @@ class APIConnection: if not message_type: raise ValueError(f"Message type id not found for type {type(msg)}") encoded = msg.SerializeToString() - _LOGGER.debug("%s: Sending %s: %s", self._params.address, type(msg), str(msg)) + + if _LOGGER.isEnabledFor(logging.DEBUG): + _LOGGER.debug( + "%s: Sending %s: %s", self._params.address, type(msg), str(msg) + ) try: frame_helper.write_packet(message_type, encoded)