Return history for entities in the order they were requested (#25560)

* Return history for entities in the order they were requested

* Fix problems. Add tests

* Update __init__.py
This commit is contained in:
Thomas Lovén 2019-07-31 20:59:25 +02:00 committed by Paulus Schoutsen
parent fcdd66b33b
commit 671cb0d092
2 changed files with 19 additions and 0 deletions

View File

@ -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()

View File

@ -428,6 +428,22 @@ class TestComponentHistory(unittest.TestCase):
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()