mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Exclude supported features and attribution from being recorded in the database (#69165)
This commit is contained in:
parent
9335b9a52e
commit
522a9bb6b1
@ -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}
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user