From 236d8aa277354870e59c4496fd85adb488ef40bf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 3 May 2022 13:22:49 -0500 Subject: [PATCH] Avoid recording static attributes for group entities (#71256) --- homeassistant/components/group/__init__.py | 3 ++ homeassistant/components/group/recorder.py | 16 ++++++ tests/components/group/test_recorder.py | 61 ++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 homeassistant/components/group/recorder.py create mode 100644 tests/components/group/test_recorder.py diff --git a/homeassistant/components/group/__init__.py b/homeassistant/components/group/__init__.py index 9627ad86734..e6d7e91f035 100644 --- a/homeassistant/components/group/__init__.py +++ b/homeassistant/components/group/__init__.py @@ -33,6 +33,7 @@ from homeassistant.helpers.entity import Entity, async_generate_entity_id from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.integration_platform import ( + async_process_integration_platform_for_component, async_process_integration_platforms, ) from homeassistant.helpers.reload import async_reload_integration_platforms @@ -265,6 +266,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if DOMAIN not in hass.data: hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass) + await async_process_integration_platform_for_component(hass, DOMAIN) + component: EntityComponent = hass.data[DOMAIN] hass.data[REG_KEY] = GroupIntegrationRegistry() diff --git a/homeassistant/components/group/recorder.py b/homeassistant/components/group/recorder.py new file mode 100644 index 00000000000..9138b4ef348 --- /dev/null +++ b/homeassistant/components/group/recorder.py @@ -0,0 +1,16 @@ +"""Integration platform for recorder.""" +from __future__ import annotations + +from homeassistant.core import HomeAssistant, callback + +from . import ATTR_AUTO, ATTR_ENTITY_ID, ATTR_ORDER + + +@callback +def exclude_attributes(hass: HomeAssistant) -> set[str]: + """Exclude static attributes from being recorded in the database.""" + return { + ATTR_ENTITY_ID, + ATTR_ORDER, + ATTR_AUTO, + } diff --git a/tests/components/group/test_recorder.py b/tests/components/group/test_recorder.py new file mode 100644 index 00000000000..fb68d9d3d43 --- /dev/null +++ b/tests/components/group/test_recorder.py @@ -0,0 +1,61 @@ +"""The tests for group recorder.""" +from __future__ import annotations + +from datetime import timedelta + +from homeassistant.components import group +from homeassistant.components.group import ATTR_AUTO, ATTR_ENTITY_ID, ATTR_ORDER +from homeassistant.components.recorder.models import StateAttributes, States +from homeassistant.components.recorder.util import session_scope +from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_ON +from homeassistant.core import State +from homeassistant.setup import async_setup_component +from homeassistant.util import dt as dt_util + +from tests.common import async_fire_time_changed +from tests.components.recorder.common import async_wait_recording_done + + +async def test_exclude_attributes(hass, recorder_mock): + """Test number registered attributes to be excluded.""" + hass.states.async_set("light.bowl", STATE_ON) + + assert await async_setup_component(hass, "light", {}) + assert await async_setup_component( + hass, + group.DOMAIN, + { + group.DOMAIN: { + "group_zero": {"entities": "light.Bowl", "icon": "mdi:work"}, + "group_one": {"entities": "light.Bowl", "icon": "mdi:work"}, + "group_two": {"entities": "light.Bowl", "icon": "mdi:work"}, + } + }, + ) + await hass.async_block_till_done() + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) + await hass.async_block_till_done() + await async_wait_recording_done(hass) + + def _fetch_states() -> list[State]: + with session_scope(hass=hass) as session: + native_states = [] + attr_ids = {} + for db_state_attributes in session.query(StateAttributes): + attr_ids[ + db_state_attributes.attributes_id + ] = db_state_attributes.to_native() + for db_state, db_state_attributes in session.query(States, StateAttributes): + state = db_state.to_native() + state.attributes = attr_ids[db_state.attributes_id] + native_states.append(state) + return native_states + + states: list[State] = await hass.async_add_executor_job(_fetch_states) + assert len(states) > 1 + for state in states: + if state.domain == group.DOMAIN: + assert ATTR_AUTO not in state.attributes + assert ATTR_ENTITY_ID not in state.attributes + assert ATTR_ORDER not in state.attributes + assert ATTR_FRIENDLY_NAME in state.attributes