diff --git a/homeassistant/components/recorder/const.py b/homeassistant/components/recorder/const.py index 13f833627a4..0fce9657624 100644 --- a/homeassistant/components/recorder/const.py +++ b/homeassistant/components/recorder/const.py @@ -4,6 +4,7 @@ from functools import partial import json from typing import Final +from homeassistant.const import ATTR_ATTRIBUTION, ATTR_SUPPORTED_FEATURES from homeassistant.helpers.json import JSONEncoder DATA_INSTANCE = "recorder_instance" @@ -25,3 +26,5 @@ MAX_ROWS_TO_PURGE = 998 DB_WORKER_PREFIX = "DbWorker" JSON_DUMP: Final = partial(json.dumps, cls=JSONEncoder, separators=(",", ":")) + +ALL_DOMAIN_EXCLUDE_ATTRS = {ATTR_ATTRIBUTION, ATTR_SUPPORTED_FEATURES} diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index afadde560a0..384bf4b22e2 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -38,7 +38,7 @@ from homeassistant.core import Context, Event, EventOrigin, State, split_entity_ from homeassistant.helpers.typing import UNDEFINED, UndefinedType import homeassistant.util.dt as dt_util -from .const import JSON_DUMP +from .const import ALL_DOMAIN_EXCLUDE_ATTRS, JSON_DUMP # SQLAlchemy Schema # pylint: disable=invalid-name @@ -269,11 +269,12 @@ class StateAttributes(Base): # type: ignore[misc,valid-type] if state is None: return "{}" domain = split_entity_id(state.entity_id)[0] - if exclude_attrs := exclude_attrs_by_domain.get(domain): - return JSON_DUMP( - {k: v for k, v in state.attributes.items() if k not in exclude_attrs} - ) - return JSON_DUMP(state.attributes) + exclude_attrs = ( + exclude_attrs_by_domain.get(domain, set()) | ALL_DOMAIN_EXCLUDE_ATTRS + ) + return JSON_DUMP( + {k: v for k, v in state.attributes.items() if k not in exclude_attrs} + ) @staticmethod def hash_shared_attrs(shared_attrs: str) -> int: diff --git a/tests/components/camera/test_recorder.py b/tests/components/camera/test_recorder.py index 1496c834e3e..76654a2dd9c 100644 --- a/tests/components/camera/test_recorder.py +++ b/tests/components/camera/test_recorder.py @@ -6,6 +6,12 @@ from datetime import timedelta from homeassistant.components import camera from homeassistant.components.recorder.models import StateAttributes, States 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.setup import async_setup_component from homeassistant.util import dt as dt_util @@ -38,5 +44,7 @@ async def test_exclude_attributes(hass): assert len(states) > 1 for state in states: assert "access_token" not in state.attributes - assert "entity_picture" not in state.attributes - assert "friendly_name" in state.attributes + assert ATTR_ENTITY_PICTURE not 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