1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add contextmanager to reset logger after set_level call in tests (#143295)

This commit is contained in:
Marc Mueller
2025-04-20 02:13:01 +02:00
committed by GitHub
parent 961f8afe53
commit 5843e63878
8 changed files with 304 additions and 318 deletions

View File

@@ -46,6 +46,7 @@ from homeassistant.components import device_automation, persistent_notification
from homeassistant.components.device_automation import ( # noqa: F401
_async_get_device_automation_capabilities as async_get_device_automation_capabilities,
)
from homeassistant.components.logger import DOMAIN as LOGGER_DOMAIN, SERVICE_SET_LEVEL
from homeassistant.config import IntegrationConfigInfo, async_process_component_config
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import (
@@ -1688,6 +1689,28 @@ def async_mock_cloud_connection_status(hass: HomeAssistant, connected: bool) ->
async_dispatcher_send(hass, SIGNAL_CLOUD_CONNECTION_STATE, state)
@asynccontextmanager
async def async_call_logger_set_level(
logger: str,
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "FATAL", "CRITICAL"],
*,
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> AsyncGenerator[None]:
"""Context manager to reset loggers after logger.set_level call."""
assert LOGGER_DOMAIN in hass.data, "'logger' integration not setup"
with caplog.at_level(logging.NOTSET, logger):
await hass.services.async_call(
LOGGER_DOMAIN,
SERVICE_SET_LEVEL,
{logger: level},
blocking=True,
)
await hass.async_block_till_done()
yield
hass.data[LOGGER_DOMAIN].overrides.clear()
def import_and_test_deprecated_constant_enum(
caplog: pytest.LogCaptureFixture,
module: ModuleType,