diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index 85195fb1581..d83a242ac71 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -16,8 +16,16 @@ from homeassistant.const import ( CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP, + EVENT_LOGGING_CHANGED, +) +from homeassistant.core import ( + CoreState, + Event, + HassJob, + HomeAssistant, + ServiceCall, + callback, ) -from homeassistant.core import CoreState, HassJob, HomeAssistant, ServiceCall, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, @@ -41,6 +49,7 @@ from .entity import RflinkCommand from .utils import identify_event_type _LOGGER = logging.getLogger(__name__) +LIB_LOGGER = logging.getLogger("rflink") CONF_IGNORE_DEVICES = "ignore_devices" CONF_RECONNECT_INTERVAL = "reconnect_interval" @@ -277,4 +286,17 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.async_create_task(connect(), eager_start=False) async_dispatcher_connect(hass, SIGNAL_EVENT, event_callback) + + async def handle_logging_changed(_: Event) -> None: + """Handle logging changed event.""" + if LIB_LOGGER.isEnabledFor(logging.DEBUG): + await RflinkCommand.send_command("rfdebug", "on") + _LOGGER.info("RFDEBUG enabled") + else: + await RflinkCommand.send_command("rfdebug", "off") + _LOGGER.info("RFDEBUG disabled") + + # Listen to EVENT_LOGGING_CHANGED to manage the RFDEBUG + hass.bus.async_listen(EVENT_LOGGING_CHANGED, handle_logging_changed) + return True diff --git a/tests/components/rflink/test_init.py b/tests/components/rflink/test_init.py index 1caae302748..d702cd44718 100644 --- a/tests/components/rflink/test_init.py +++ b/tests/components/rflink/test_init.py @@ -1,5 +1,6 @@ """Common functions for RFLink component tests and generic platform tests.""" +import logging from unittest.mock import Mock import pytest @@ -21,6 +22,7 @@ from homeassistant.const import ( ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, + EVENT_LOGGING_CHANGED, SERVICE_STOP_COVER, SERVICE_TURN_OFF, ) @@ -556,3 +558,30 @@ async def test_unique_id( temperature_entry = entity_registry.async_get("sensor.temperature_device") assert temperature_entry assert temperature_entry.unique_id == "my_temperature_device_unique_id" + + +async def test_enable_debug_logs( + hass: HomeAssistant, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test that changing debug level enables RFDEBUG.""" + + domain = RFLINK_DOMAIN + config = {RFLINK_DOMAIN: {CONF_HOST: "10.10.0.1", CONF_PORT: 1234}} + + # setup mocking rflink module + _, mock_create, _, _ = await mock_rflink(hass, config, domain, monkeypatch) + + logging.getLogger("rflink").setLevel(logging.DEBUG) + hass.bus.async_fire(EVENT_LOGGING_CHANGED) + await hass.async_block_till_done() + + assert "RFDEBUG enabled" in caplog.text + assert "RFDEBUG disabled" not in caplog.text + + logging.getLogger("rflink").setLevel(logging.INFO) + hass.bus.async_fire(EVENT_LOGGING_CHANGED) + await hass.async_block_till_done() + + assert "RFDEBUG disabled" in caplog.text