Skip to content

Commit

Permalink
Return history for entities in the order they were requested (home-as…
Browse files Browse the repository at this point in the history
…sistant#25560)

* Return history for entities in the order they were requested

* Fix problems. Add tests

* Update __init__.py
  • Loading branch information
thomasloven authored and balloob committed Jul 31, 2019
1 parent fcdd66b commit 671cb0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def states_to_json(
axis correctly.
"""
result = defaultdict(list)
if entity_ids is not None:
for ent_id in entity_ids:
result[ent_id] = []

# Get the states at the start time
timer_start = time.perf_counter()
Expand Down
16 changes: 16 additions & 0 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ def test_get_significant_states_include_exclude(self):
history.CONF_ENTITIES: ['media_player.test']}}})
self.check_significant_states(zero, four, states, config)

def test_get_significant_states_are_ordered(self):
"""Test order of results from get_significant_states
When entity ids are given, the results should be returned with the data
in the same order.
"""
zero, four, states = self.record_states()
entity_ids = ['media_player.test', 'media_player.test2']
hist = history.get_significant_states(
self.hass, zero, four, entity_ids, filters=history.Filters())
assert list(hist.keys()) == entity_ids
entity_ids = ['media_player.test2', 'media_player.test']
hist = history.get_significant_states(
self.hass, zero, four, entity_ids, filters=history.Filters())
assert list(hist.keys()) == entity_ids

def check_significant_states(self, zero, four, states, config):
"""Check if significant states are retrieved."""
filters = history.Filters()
Expand Down

0 comments on commit 671cb0d

Please sign in to comment.