Mark counter domain as continuous to exclude it from logbook (#73101)
This commit is contained in:
parent
457c7a4ddc
commit
0b62944148
6 changed files with 66 additions and 20 deletions
|
@ -10,7 +10,7 @@ from sqlalchemy.sql.elements import ClauseList
|
|||
from sqlalchemy.sql.expression import literal
|
||||
from sqlalchemy.sql.selectable import Select
|
||||
|
||||
from homeassistant.components.proximity import DOMAIN as PROXIMITY_DOMAIN
|
||||
from homeassistant.components.recorder.filters import like_domain_matchers
|
||||
from homeassistant.components.recorder.models import (
|
||||
EVENTS_CONTEXT_ID_INDEX,
|
||||
OLD_FORMAT_ATTRS_JSON,
|
||||
|
@ -22,15 +22,19 @@ from homeassistant.components.recorder.models import (
|
|||
StateAttributes,
|
||||
States,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
|
||||
CONTINUOUS_DOMAINS = {PROXIMITY_DOMAIN, SENSOR_DOMAIN}
|
||||
CONTINUOUS_ENTITY_ID_LIKE = [f"{domain}.%" for domain in CONTINUOUS_DOMAINS]
|
||||
from ..const import ALWAYS_CONTINUOUS_DOMAINS, CONDITIONALLY_CONTINUOUS_DOMAINS
|
||||
|
||||
# Domains that are continuous if there is a UOM set on the entity
|
||||
CONDITIONALLY_CONTINUOUS_ENTITY_ID_LIKE = like_domain_matchers(
|
||||
CONDITIONALLY_CONTINUOUS_DOMAINS
|
||||
)
|
||||
# Domains that are always continuous
|
||||
ALWAYS_CONTINUOUS_ENTITY_ID_LIKE = like_domain_matchers(ALWAYS_CONTINUOUS_DOMAINS)
|
||||
|
||||
UNIT_OF_MEASUREMENT_JSON = '"unit_of_measurement":'
|
||||
UNIT_OF_MEASUREMENT_JSON_LIKE = f"%{UNIT_OF_MEASUREMENT_JSON}%"
|
||||
|
||||
|
||||
PSUEDO_EVENT_STATE_CHANGED = None
|
||||
# Since we don't store event_types and None
|
||||
# and we don't store state_changed in events
|
||||
|
@ -220,29 +224,44 @@ def _missing_state_matcher() -> sqlalchemy.and_:
|
|||
def _not_continuous_entity_matcher() -> sqlalchemy.or_:
|
||||
"""Match non continuous entities."""
|
||||
return sqlalchemy.or_(
|
||||
_not_continuous_domain_matcher(),
|
||||
# First exclude domains that may be continuous
|
||||
_not_possible_continuous_domain_matcher(),
|
||||
# But let in the entities in the possible continuous domains
|
||||
# that are not actually continuous sensors because they lack a UOM
|
||||
sqlalchemy.and_(
|
||||
_continuous_domain_matcher, _not_uom_attributes_matcher()
|
||||
_conditionally_continuous_domain_matcher, _not_uom_attributes_matcher()
|
||||
).self_group(),
|
||||
)
|
||||
|
||||
|
||||
def _not_continuous_domain_matcher() -> sqlalchemy.and_:
|
||||
"""Match not continuous domains."""
|
||||
def _not_possible_continuous_domain_matcher() -> sqlalchemy.and_:
|
||||
"""Match not continuous domains.
|
||||
|
||||
This matches domain that are always considered continuous
|
||||
and domains that are conditionally (if they have a UOM)
|
||||
continuous domains.
|
||||
"""
|
||||
return sqlalchemy.and_(
|
||||
*[
|
||||
~States.entity_id.like(entity_domain)
|
||||
for entity_domain in CONTINUOUS_ENTITY_ID_LIKE
|
||||
for entity_domain in (
|
||||
*ALWAYS_CONTINUOUS_ENTITY_ID_LIKE,
|
||||
*CONDITIONALLY_CONTINUOUS_ENTITY_ID_LIKE,
|
||||
)
|
||||
],
|
||||
).self_group()
|
||||
|
||||
|
||||
def _continuous_domain_matcher() -> sqlalchemy.or_:
|
||||
"""Match continuous domains."""
|
||||
def _conditionally_continuous_domain_matcher() -> sqlalchemy.or_:
|
||||
"""Match conditionally continuous domains.
|
||||
|
||||
This matches domain that are only considered
|
||||
continuous if a UOM is set.
|
||||
"""
|
||||
return sqlalchemy.or_(
|
||||
*[
|
||||
States.entity_id.like(entity_domain)
|
||||
for entity_domain in CONTINUOUS_ENTITY_ID_LIKE
|
||||
for entity_domain in CONDITIONALLY_CONTINUOUS_ENTITY_ID_LIKE
|
||||
],
|
||||
).self_group()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue