From 6af42b43725322c3dd69b837444e1ab10339cb65 Mon Sep 17 00:00:00 2001 From: Bob Anderson Date: Thu, 11 Jan 2018 15:21:23 -0800 Subject: [PATCH] Control ordering of display in history component (#11340) * Make the order shown in the history component match the ordering given in the configuration of included entities (if any) * return the sorted result * optimize sorting. since entities only appear once, we can break from a search on first find, and no copy of the list is needed --- homeassistant/components/history.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 55858dbe765..8f96d95521d 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -304,7 +304,20 @@ class HistoryPeriodView(HomeAssistantView): elapsed = time.perf_counter() - timer_start _LOGGER.debug( 'Extracted %d states in %fs', sum(map(len, result)), elapsed) - return self.json(result) + + # Reorder the result to respect the ordering given by any + # entities explicitly included in the configuration. + + sorted_result = [] + for order_entity in self.filters.included_entities: + for state_list in result: + if state_list[0].entity_id == order_entity: + sorted_result.append(state_list) + result.remove(state_list) + break + sorted_result.extend(result) + + return self.json(sorted_result) class Filters(object):