mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Reduce overhead to see if an entity is recorded (#87912)
A significant chunk of list_statistic_ids was checking if the entity was recorded because it had to get the recorder instance over and over
This commit is contained in:
parent
e899754919
commit
85649ec589
@ -185,6 +185,9 @@ class Recorder(threading.Thread):
|
|||||||
self.engine: Engine | None = None
|
self.engine: Engine | None = None
|
||||||
self.run_history = RunHistory()
|
self.run_history = RunHistory()
|
||||||
|
|
||||||
|
# The entity_filter is exposed on the recorder instance so that
|
||||||
|
# it can be used to see if an entity is being recorded and is called
|
||||||
|
# by is_entity_recorder and the sensor recorder.
|
||||||
self.entity_filter = entity_filter
|
self.entity_filter = entity_filter
|
||||||
self.exclude_t = set(exclude_t)
|
self.exclude_t = set(exclude_t)
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ from sqlalchemy.orm.session import Session
|
|||||||
|
|
||||||
from homeassistant.components.recorder import (
|
from homeassistant.components.recorder import (
|
||||||
DOMAIN as RECORDER_DOMAIN,
|
DOMAIN as RECORDER_DOMAIN,
|
||||||
|
get_instance,
|
||||||
history,
|
history,
|
||||||
is_entity_recorded,
|
|
||||||
statistics,
|
statistics,
|
||||||
util as recorder_util,
|
util as recorder_util,
|
||||||
)
|
)
|
||||||
@ -76,9 +76,10 @@ def _get_sensor_states(hass: HomeAssistant) -> list[State]:
|
|||||||
"""Get the current state of all sensors for which to compile statistics."""
|
"""Get the current state of all sensors for which to compile statistics."""
|
||||||
all_sensors = hass.states.all(DOMAIN)
|
all_sensors = hass.states.all(DOMAIN)
|
||||||
statistics_sensors = []
|
statistics_sensors = []
|
||||||
|
instance = get_instance(hass)
|
||||||
|
|
||||||
for state in all_sensors:
|
for state in all_sensors:
|
||||||
if not is_entity_recorded(hass, state.entity_id):
|
if not instance.entity_filter(state.entity_id):
|
||||||
continue
|
continue
|
||||||
if not try_parse_enum(SensorStateClass, state.attributes.get(ATTR_STATE_CLASS)):
|
if not try_parse_enum(SensorStateClass, state.attributes.get(ATTR_STATE_CLASS)):
|
||||||
continue
|
continue
|
||||||
@ -680,6 +681,7 @@ def validate_statistics(
|
|||||||
metadatas = statistics.get_metadata(hass, statistic_source=RECORDER_DOMAIN)
|
metadatas = statistics.get_metadata(hass, statistic_source=RECORDER_DOMAIN)
|
||||||
sensor_entity_ids = {i.entity_id for i in sensor_states}
|
sensor_entity_ids = {i.entity_id for i in sensor_states}
|
||||||
sensor_statistic_ids = set(metadatas)
|
sensor_statistic_ids = set(metadatas)
|
||||||
|
instance = get_instance(hass)
|
||||||
|
|
||||||
for state in sensor_states:
|
for state in sensor_states:
|
||||||
entity_id = state.entity_id
|
entity_id = state.entity_id
|
||||||
@ -689,7 +691,7 @@ def validate_statistics(
|
|||||||
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
if metadata := metadatas.get(entity_id):
|
if metadata := metadatas.get(entity_id):
|
||||||
if not is_entity_recorded(hass, state.entity_id):
|
if not instance.entity_filter(state.entity_id):
|
||||||
# Sensor was previously recorded, but no longer is
|
# Sensor was previously recorded, but no longer is
|
||||||
validation_result[entity_id].append(
|
validation_result[entity_id].append(
|
||||||
statistics.ValidationIssue(
|
statistics.ValidationIssue(
|
||||||
@ -739,7 +741,7 @@ def validate_statistics(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif state_class is not None:
|
elif state_class is not None:
|
||||||
if not is_entity_recorded(hass, state.entity_id):
|
if not instance.entity_filter(state.entity_id):
|
||||||
# Sensor is not recorded
|
# Sensor is not recorded
|
||||||
validation_result[entity_id].append(
|
validation_result[entity_id].append(
|
||||||
statistics.ValidationIssue(
|
statistics.ValidationIssue(
|
||||||
|
@ -3894,8 +3894,10 @@ async def test_validate_statistics_sensor_no_longer_recorded(
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
with patch(
|
instance = get_instance(hass)
|
||||||
"homeassistant.components.sensor.recorder.is_entity_recorded",
|
with patch.object(
|
||||||
|
instance,
|
||||||
|
"entity_filter",
|
||||||
return_value=False,
|
return_value=False,
|
||||||
):
|
):
|
||||||
await assert_validation_result(client, expected)
|
await assert_validation_result(client, expected)
|
||||||
@ -3945,8 +3947,10 @@ async def test_validate_statistics_sensor_not_recorded(
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
with patch(
|
instance = get_instance(hass)
|
||||||
"homeassistant.components.sensor.recorder.is_entity_recorded",
|
with patch.object(
|
||||||
|
instance,
|
||||||
|
"entity_filter",
|
||||||
return_value=False,
|
return_value=False,
|
||||||
):
|
):
|
||||||
hass.states.async_set("sensor.test", 10, attributes=attributes)
|
hass.states.async_set("sensor.test", 10, attributes=attributes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user