Filter out empty results in history API (#25633)

pull/25636/head
Paulus Schoutsen 2019-08-01 11:52:57 -07:00 committed by GitHub
parent c2556d90ea
commit 3649a1b5e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -234,6 +234,7 @@ def states_to_json(
axis correctly.
"""
result = defaultdict(list)
# Set all entity IDs to empty lists in result set to maintain the order
if entity_ids is not None:
for ent_id in entity_ids:
result[ent_id] = []
@ -253,7 +254,9 @@ def states_to_json(
# Append all changes to it
for ent_id, group in groupby(states, lambda state: state.entity_id):
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):
@ -348,7 +351,6 @@ class HistoryPeriodView(HomeAssistantView):
# Optionally reorder the result to respect the ordering given
# by any entities explicitly included in the configuration.
if self.use_include_order:
sorted_result = []
for order_entity in self.filters.included_entities:

View File

@ -628,3 +628,25 @@ async def test_fetch_period_api(hass, hass_client):
"/api/history/period/{}".format(dt_util.utcnow().isoformat())
)
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