diff --git a/homeassistant/components/recorder/filters.py b/homeassistant/components/recorder/filters.py index 0ceb013d8c5..3077f7f57f3 100644 --- a/homeassistant/components/recorder/filters.py +++ b/homeassistant/components/recorder/filters.py @@ -36,7 +36,7 @@ def extract_include_exclude_filter_conf(conf: ConfigType) -> dict[str, Any]: """ return { filter_type: { - matcher: set(conf.get(filter_type, {}).get(matcher, [])) + matcher: set(conf.get(filter_type, {}).get(matcher) or []) for matcher in FITLER_MATCHERS } for filter_type in FILTER_TYPES diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index 2903f29f5dc..d33bbd5b8ac 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -11,7 +11,7 @@ from unittest.mock import Mock, patch import pytest import voluptuous as vol -from homeassistant.components import logbook +from homeassistant.components import logbook, recorder from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME from homeassistant.components.automation import EVENT_AUTOMATION_TRIGGERED from homeassistant.components.logbook.models import LazyEventPartialState @@ -2796,3 +2796,39 @@ async def test_get_events_with_context_state(hass, hass_ws_client, recorder_mock assert results[3]["context_state"] == "off" assert results[3]["context_user_id"] == "b400facee45711eaa9308bfd3d19e474" assert "context_event_type" not in results[3] + + +async def test_logbook_with_empty_config(hass, recorder_mock): + """Test we handle a empty configuration.""" + assert await async_setup_component( + hass, + logbook.DOMAIN, + { + logbook.DOMAIN: {}, + recorder.DOMAIN: {}, + }, + ) + await hass.async_block_till_done() + + +async def test_logbook_with_non_iterable_entity_filter(hass, recorder_mock): + """Test we handle a non-iterable entity filter.""" + assert await async_setup_component( + hass, + logbook.DOMAIN, + { + logbook.DOMAIN: { + CONF_EXCLUDE: { + CONF_ENTITIES: ["light.additional_excluded"], + } + }, + recorder.DOMAIN: { + CONF_EXCLUDE: { + CONF_ENTITIES: None, + CONF_DOMAINS: None, + CONF_ENTITY_GLOBS: None, + } + }, + }, + ) + await hass.async_block_till_done() diff --git a/tests/components/recorder/test_filters.py b/tests/components/recorder/test_filters.py index fa80df6e345..5c0afa10f9d 100644 --- a/tests/components/recorder/test_filters.py +++ b/tests/components/recorder/test_filters.py @@ -12,6 +12,13 @@ from homeassistant.helpers.entityfilter import ( CONF_INCLUDE, ) +EMPTY_INCLUDE_FILTER = { + CONF_INCLUDE: { + CONF_DOMAINS: None, + CONF_ENTITIES: None, + CONF_ENTITY_GLOBS: None, + } +} SIMPLE_INCLUDE_FILTER = { CONF_INCLUDE: { CONF_DOMAINS: ["homeassistant"], @@ -87,6 +94,19 @@ def test_extract_include_exclude_filter_conf(): assert SIMPLE_INCLUDE_EXCLUDE_FILTER[CONF_EXCLUDE][CONF_ENTITIES] != { "cover.altered" } + empty_include_filter = extract_include_exclude_filter_conf(EMPTY_INCLUDE_FILTER) + assert empty_include_filter == { + CONF_EXCLUDE: { + CONF_DOMAINS: set(), + CONF_ENTITIES: set(), + CONF_ENTITY_GLOBS: set(), + }, + CONF_INCLUDE: { + CONF_DOMAINS: set(), + CONF_ENTITIES: set(), + CONF_ENTITY_GLOBS: set(), + }, + } def test_merge_include_exclude_filters():