Enable RFDEBUG on RFLink "Enable debug logging" (#138571)

* Enable RFDEBUG on "Enable debug logging"

* fix checks

* fix checks

* one more lap

* fix test

* wait to init rflink 

In my dev env this is not needed

* use hass.async_create_task(handle_logging_changed())

instead async_at_started(hass, handle_logging_changed)

* revert unneeded async_block_till_done

* Remove the startup management

There's a race condition at startup that can't be managed nicely
This commit is contained in:
javicalle 2025-05-18 22:51:42 +02:00 committed by GitHub
parent 3ecde49dca
commit 3d83c6299b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View File

@ -16,8 +16,16 @@ from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_PORT, CONF_PORT,
EVENT_HOMEASSISTANT_STOP, 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 import config_validation as cv
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
@ -41,6 +49,7 @@ from .entity import RflinkCommand
from .utils import identify_event_type from .utils import identify_event_type
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
LIB_LOGGER = logging.getLogger("rflink")
CONF_IGNORE_DEVICES = "ignore_devices" CONF_IGNORE_DEVICES = "ignore_devices"
CONF_RECONNECT_INTERVAL = "reconnect_interval" 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) hass.async_create_task(connect(), eager_start=False)
async_dispatcher_connect(hass, SIGNAL_EVENT, event_callback) 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 return True

View File

@ -1,5 +1,6 @@
"""Common functions for RFLink component tests and generic platform tests.""" """Common functions for RFLink component tests and generic platform tests."""
import logging
from unittest.mock import Mock from unittest.mock import Mock
import pytest import pytest
@ -21,6 +22,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
CONF_HOST, CONF_HOST,
CONF_PORT, CONF_PORT,
EVENT_LOGGING_CHANGED,
SERVICE_STOP_COVER, SERVICE_STOP_COVER,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
) )
@ -556,3 +558,30 @@ async def test_unique_id(
temperature_entry = entity_registry.async_get("sensor.temperature_device") temperature_entry = entity_registry.async_get("sensor.temperature_device")
assert temperature_entry assert temperature_entry
assert temperature_entry.unique_id == "my_temperature_device_unique_id" 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