mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Filter out empty results in history API (#25633)
This commit is contained in:
parent
c2556d90ea
commit
3649a1b5e9
@ -234,6 +234,7 @@ def states_to_json(
|
|||||||
axis correctly.
|
axis correctly.
|
||||||
"""
|
"""
|
||||||
result = defaultdict(list)
|
result = defaultdict(list)
|
||||||
|
# Set all entity IDs to empty lists in result set to maintain the order
|
||||||
if entity_ids is not None:
|
if entity_ids is not None:
|
||||||
for ent_id in entity_ids:
|
for ent_id in entity_ids:
|
||||||
result[ent_id] = []
|
result[ent_id] = []
|
||||||
@ -253,7 +254,9 @@ def states_to_json(
|
|||||||
# Append all changes to it
|
# Append all changes to it
|
||||||
for ent_id, group in groupby(states, lambda state: state.entity_id):
|
for ent_id, group in groupby(states, lambda state: state.entity_id):
|
||||||
result[ent_id].extend(group)
|
result[ent_id].extend(group)
|
||||||
return result
|
|
||||||
|
# Filter out the empty lists if some states had 0 results.
|
||||||
|
return {key: val for key, val in result.items() if val}
|
||||||
|
|
||||||
|
|
||||||
def get_state(hass, utc_point_in_time, entity_id, run=None):
|
def get_state(hass, utc_point_in_time, entity_id, run=None):
|
||||||
@ -348,7 +351,6 @@ class HistoryPeriodView(HomeAssistantView):
|
|||||||
|
|
||||||
# Optionally reorder the result to respect the ordering given
|
# Optionally reorder the result to respect the ordering given
|
||||||
# by any entities explicitly included in the configuration.
|
# by any entities explicitly included in the configuration.
|
||||||
|
|
||||||
if self.use_include_order:
|
if self.use_include_order:
|
||||||
sorted_result = []
|
sorted_result = []
|
||||||
for order_entity in self.filters.included_entities:
|
for order_entity in self.filters.included_entities:
|
||||||
|
@ -628,3 +628,25 @@ async def test_fetch_period_api(hass, hass_client):
|
|||||||
"/api/history/period/{}".format(dt_util.utcnow().isoformat())
|
"/api/history/period/{}".format(dt_util.utcnow().isoformat())
|
||||||
)
|
)
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
||||||
|
async def test_fetch_period_api_with_include_order(hass, hass_client):
|
||||||
|
"""Test the fetch period view for history."""
|
||||||
|
await hass.async_add_job(init_recorder_component, hass)
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
"history",
|
||||||
|
{
|
||||||
|
"history": {
|
||||||
|
"use_include_order": True,
|
||||||
|
"include": {"entities": ["light.kitchen"]},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
||||||
|
client = await hass_client()
|
||||||
|
response = await client.get(
|
||||||
|
"/api/history/period/{}".format(dt_util.utcnow().isoformat()),
|
||||||
|
params={"filter_entity_id": "non.existing,something.else"},
|
||||||
|
)
|
||||||
|
assert response.status == 200
|
||||||
|
Loading…
x
Reference in New Issue
Block a user