Avoid recording static attributes for group entities (#71256)

pull/66454/head
J. Nick Koston 2022-05-03 13:22:49 -05:00 committed by GitHub
parent e1be6dd34f
commit 236d8aa277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 0 deletions

View File

@ -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()

View File

@ -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,
}

View File

@ -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