Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions homeassistant/components/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand All @@ -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):
Expand Down Expand Up @@ -348,7 +351,6 @@ async def get(self, request, datetime=None):

# 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:
Expand Down
22 changes: 22 additions & 0 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm - this is a test for something else, right? not the introduced functionality? I don't see it validating the results, other than HTTP status.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request failed before my fix, because it assumed each entry for an entity had at least 1 state. It does indeed not validate the results.

"""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