Exclude supported features and attribution from being recorded in the database (#69165)

This commit is contained in:
J. Nick Koston 2022-04-03 05:51:42 -06:00 committed by GitHub
parent 9335b9a52e
commit 522a9bb6b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View File

@ -4,6 +4,7 @@ from functools import partial
import json import json
from typing import Final from typing import Final
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_SUPPORTED_FEATURES
from homeassistant.helpers.json import JSONEncoder from homeassistant.helpers.json import JSONEncoder
DATA_INSTANCE = "recorder_instance" DATA_INSTANCE = "recorder_instance"
@ -25,3 +26,5 @@ MAX_ROWS_TO_PURGE = 998
DB_WORKER_PREFIX = "DbWorker" DB_WORKER_PREFIX = "DbWorker"
JSON_DUMP: Final = partial(json.dumps, cls=JSONEncoder, separators=(",", ":")) JSON_DUMP: Final = partial(json.dumps, cls=JSONEncoder, separators=(",", ":"))
ALL_DOMAIN_EXCLUDE_ATTRS = {ATTR_ATTRIBUTION, ATTR_SUPPORTED_FEATURES}

View File

@ -38,7 +38,7 @@ from homeassistant.core import Context, Event, EventOrigin, State, split_entity_
from homeassistant.helpers.typing import UNDEFINED, UndefinedType from homeassistant.helpers.typing import UNDEFINED, UndefinedType
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import JSON_DUMP from .const import ALL_DOMAIN_EXCLUDE_ATTRS, JSON_DUMP
# SQLAlchemy Schema # SQLAlchemy Schema
# pylint: disable=invalid-name # pylint: disable=invalid-name
@ -269,11 +269,12 @@ class StateAttributes(Base): # type: ignore[misc,valid-type]
if state is None: if state is None:
return "{}" return "{}"
domain = split_entity_id(state.entity_id)[0] domain = split_entity_id(state.entity_id)[0]
if exclude_attrs := exclude_attrs_by_domain.get(domain): exclude_attrs = (
return JSON_DUMP( exclude_attrs_by_domain.get(domain, set()) | ALL_DOMAIN_EXCLUDE_ATTRS
{k: v for k, v in state.attributes.items() if k not in exclude_attrs} )
) return JSON_DUMP(
return JSON_DUMP(state.attributes) {k: v for k, v in state.attributes.items() if k not in exclude_attrs}
)
@staticmethod @staticmethod
def hash_shared_attrs(shared_attrs: str) -> int: def hash_shared_attrs(shared_attrs: str) -> int:

View File

@ -6,6 +6,12 @@ from datetime import timedelta
from homeassistant.components import camera from homeassistant.components import camera
from homeassistant.components.recorder.models import StateAttributes, States from homeassistant.components.recorder.models import StateAttributes, States
from homeassistant.components.recorder.util import session_scope from homeassistant.components.recorder.util import session_scope
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_ENTITY_PICTURE,
ATTR_FRIENDLY_NAME,
ATTR_SUPPORTED_FEATURES,
)
from homeassistant.core import State from homeassistant.core import State
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
@ -38,5 +44,7 @@ async def test_exclude_attributes(hass):
assert len(states) > 1 assert len(states) > 1
for state in states: for state in states:
assert "access_token" not in state.attributes assert "access_token" not in state.attributes
assert "entity_picture" not in state.attributes assert ATTR_ENTITY_PICTURE not in state.attributes
assert "friendly_name" in state.attributes assert ATTR_ATTRIBUTION not in state.attributes
assert ATTR_SUPPORTED_FEATURES not in state.attributes
assert ATTR_FRIENDLY_NAME in state.attributes