Use entity attribute enums in logbook (#178254)

This commit is contained in:
epenet
2026-08-05 17:33:39 +02:00
committed by GitHub
parent 82578a362d
commit 03b6c98f5a
3 changed files with 26 additions and 15 deletions
+20 -11
View File
@@ -3,17 +3,19 @@
from collections.abc import Callable, Collection, Mapping
from typing import Any
from homeassistant.components.sensor import ATTR_STATE_CLASS, NON_NUMERIC_DEVICE_CLASSES
from homeassistant.components.sensor import (
NON_NUMERIC_DEVICE_CLASSES,
SensorEntityCapabilityAttribute,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_DEVICE_ID,
ATTR_DOMAIN,
ATTR_ENTITY_ID,
ATTR_SERVICE_DATA,
ATTR_UNIT_OF_MEASUREMENT,
EVENT_CALL_SERVICE,
EVENT_LOGBOOK_ENTRY,
EVENT_STATE_CHANGED,
EntityStateAttribute,
)
from homeassistant.core import (
CALLBACK_TYPE,
@@ -254,7 +256,7 @@ def is_sensor_continuous(
will filter out any sensors with a unit_of_measurement.
If the state still exists in the state machine, this function still
checks for ATTR_UNIT_OF_MEASUREMENT since the live mode is not filtered
checks for EntityStateAttribute.UNIT_OF_MEASUREMENT since the live mode is not filtered
by the SQL query.
"""
# If it is in the state machine we can quick check if it
@@ -262,9 +264,11 @@ def is_sensor_continuous(
# it does
if (state := hass.states.get(entity_id)) and (attributes := state.attributes):
return (
ATTR_UNIT_OF_MEASUREMENT in attributes
or ATTR_STATE_CLASS in attributes
or _device_class_is_numeric(attributes.get(ATTR_DEVICE_CLASS))
EntityStateAttribute.UNIT_OF_MEASUREMENT in attributes
or SensorEntityCapabilityAttribute.STATE_CLASS in attributes
or _device_class_is_numeric(
attributes.get(EntityStateAttribute.DEVICE_CLASS)
)
)
# If its not in the state machine, we need to check
# the entity registry to see if its a sensor
@@ -276,7 +280,10 @@ def is_sensor_continuous(
return bool(
(entry := ent_reg.async_get(entity_id))
and (
(entry.capabilities and entry.capabilities.get(ATTR_STATE_CLASS))
(
entry.capabilities
and entry.capabilities.get(SensorEntityCapabilityAttribute.STATE_CLASS)
)
or _device_class_is_numeric(entry.device_class)
)
)
@@ -295,9 +302,11 @@ def _is_state_filtered(new_state: State, old_state: State) -> bool:
or (
new_state.domain == SENSOR_DOMAIN
and (
ATTR_UNIT_OF_MEASUREMENT in new_state.attributes
or ATTR_STATE_CLASS in new_state.attributes
or _device_class_is_numeric(new_state.attributes.get(ATTR_DEVICE_CLASS))
EntityStateAttribute.UNIT_OF_MEASUREMENT in new_state.attributes
or SensorEntityCapabilityAttribute.STATE_CLASS in new_state.attributes
or _device_class_is_numeric(
new_state.attributes.get(EntityStateAttribute.DEVICE_CLASS)
)
)
)
)
+2 -2
View File
@@ -14,7 +14,7 @@ from homeassistant.components.recorder.models import (
ulid_to_bytes_or_none,
uuid_hex_to_bytes_or_none,
)
from homeassistant.const import ATTR_ICON, EVENT_STATE_CHANGED
from homeassistant.const import EVENT_STATE_CHANGED, EntityStateAttribute
from homeassistant.core import Context, Event, State, callback
from homeassistant.util.event_type import EventType
from homeassistant.util.json import json_loads
@@ -177,7 +177,7 @@ def async_event_to_row(event: Event) -> EventAsRow:
context_parent_id_bin=ulid_to_bytes_or_none(context.parent_id),
state=new_state.state,
entity_id=new_state.entity_id,
icon=new_state.attributes.get(ATTR_ICON),
icon=new_state.attributes.get(EntityStateAttribute.ICON),
attributes=new_state.attributes,
context_only=None,
data=event.data,
@@ -29,11 +29,11 @@ from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import (
ATTR_DOMAIN,
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_NAME,
ATTR_SERVICE,
EVENT_CALL_SERVICE,
EVENT_LOGBOOK_ENTRY,
EntityStateAttribute,
)
from homeassistant.core import HomeAssistant, split_entity_id
from homeassistant.helpers import entity_registry as er
@@ -544,7 +544,9 @@ class EntityNameCache:
if entity_id in self._names:
return self._names[entity_id]
if (current_state := self._hass.states.get(entity_id)) and (
friendly_name := current_state.attributes.get(ATTR_FRIENDLY_NAME)
friendly_name := current_state.attributes.get(
EntityStateAttribute.FRIENDLY_NAME
)
):
self._names[entity_id] = friendly_name
else: