Only log loop client subscription log if log level is DEBUG (#117008)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Jan Bouwhuis 2024-05-07 18:38:59 +02:00 committed by GitHub
parent 15618a8a97
commit 7148c849d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -844,8 +844,9 @@ class MQTT:
subscription_list = list(subscriptions.items()) subscription_list = list(subscriptions.items())
result, mid = self._mqttc.subscribe(subscription_list) result, mid = self._mqttc.subscribe(subscription_list)
for topic, qos in subscriptions.items(): if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug("Subscribing to %s, mid: %s, qos: %s", topic, mid, qos) for topic, qos in subscriptions.items():
_LOGGER.debug("Subscribing to %s, mid: %s, qos: %s", topic, mid, qos)
self._last_subscribe = time.monotonic() self._last_subscribe = time.monotonic()
if result == 0: if result == 0:
@ -863,8 +864,9 @@ class MQTT:
result, mid = self._mqttc.unsubscribe(topics) result, mid = self._mqttc.unsubscribe(topics)
_raise_on_error(result) _raise_on_error(result)
for topic in topics: if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug("Unsubscribing from %s, mid: %s", topic, mid) for topic in topics:
_LOGGER.debug("Unsubscribing from %s, mid: %s", topic, mid)
await self._async_wait_for_mid(mid) await self._async_wait_for_mid(mid)

View file

@ -1,9 +1,11 @@
"""The tests for the MQTT component.""" """The tests for the MQTT component."""
import asyncio import asyncio
from collections.abc import Generator
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timedelta from datetime import datetime, timedelta
import json import json
import logging
import socket import socket
import ssl import ssl
from typing import Any, TypedDict from typing import Any, TypedDict
@ -17,6 +19,7 @@ import voluptuous as vol
from homeassistant.components import mqtt from homeassistant.components import mqtt
from homeassistant.components.mqtt import debug_info from homeassistant.components.mqtt import debug_info
from homeassistant.components.mqtt.client import ( from homeassistant.components.mqtt.client import (
_LOGGER as CLIENT_LOGGER,
RECONNECT_INTERVAL_SECONDS, RECONNECT_INTERVAL_SECONDS,
EnsureJobAfterCooldown, EnsureJobAfterCooldown,
) )
@ -112,6 +115,15 @@ def record_calls(calls: list[ReceiveMessage]) -> MessageCallbackType:
return record_calls return record_calls
@pytest.fixture
def client_debug_log() -> Generator[None, None]:
"""Set the mqtt client log level to DEBUG."""
logger = logging.getLogger("mqtt_client_tests_debug")
logger.setLevel(logging.DEBUG)
with patch.object(CLIENT_LOGGER, "parent", logger):
yield
def help_assert_message( def help_assert_message(
msg: ReceiveMessage, msg: ReceiveMessage,
topic: str | None = None, topic: str | None = None,
@ -1000,6 +1012,7 @@ async def test_subscribe_topic_not_initialize(
@patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.2) @patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.2)
async def test_subscribe_and_resubscribe( async def test_subscribe_and_resubscribe(
hass: HomeAssistant, hass: HomeAssistant,
client_debug_log: None,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
mqtt_client_mock: MqttMockPahoClient, mqtt_client_mock: MqttMockPahoClient,
calls: list[ReceiveMessage], calls: list[ReceiveMessage],