diff --git a/homeassistant/components/history/__init__.py b/homeassistant/components/history/__init__.py index d0dd098638f..2032843e761 100644 --- a/homeassistant/components/history/__init__.py +++ b/homeassistant/components/history/__init__.py @@ -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() diff --git a/tests/components/history/test_init.py b/tests/components/history/test_init.py index 0c9062414e7..b25fd12ccf4 100644 --- a/tests/components/history/test_init.py +++ b/tests/components/history/test_init.py @@ -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()