From 903ca567c5af8da02d881d8ed0b19088dd7abc31 Mon Sep 17 00:00:00 2001 From: Bob Anderson Date: Tue, 16 Jan 2018 11:48:10 -0800 Subject: [PATCH] History order bugfix and opt-in option (#11686) * make history view re-ordering optional and opt-in, also fix type bug * use python false for default value * whitespace cleanup --- homeassistant/components/history.py | 38 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 8f96d95521d..8f58f5f7e17 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -20,14 +20,19 @@ from homeassistant.components import recorder, script from homeassistant.components.http import HomeAssistantView from homeassistant.const import ATTR_HIDDEN from homeassistant.components.recorder.util import session_scope, execute +import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) DOMAIN = 'history' DEPENDENCIES = ['recorder', 'http'] +CONF_ORDER = 'use_include_order' + CONFIG_SCHEMA = vol.Schema({ - DOMAIN: recorder.FILTER_SCHEMA, + DOMAIN: recorder.FILTER_SCHEMA.extend({ + vol.Optional(CONF_ORDER, default=False): cv.boolean, + }) }, extra=vol.ALLOW_EXTRA) SIGNIFICANT_DOMAINS = ('thermostat', 'climate') @@ -242,8 +247,9 @@ def async_setup(hass, config): if include: filters.included_entities = include[CONF_ENTITIES] filters.included_domains = include[CONF_DOMAINS] + use_include_order = config[DOMAIN].get(CONF_ORDER) - hass.http.register_view(HistoryPeriodView(filters)) + hass.http.register_view(HistoryPeriodView(filters, use_include_order)) yield from hass.components.frontend.async_register_built_in_panel( 'history', 'history', 'mdi:poll-box') @@ -257,9 +263,10 @@ class HistoryPeriodView(HomeAssistantView): name = 'api:history:view-period' extra_urls = ['/api/history/period/{datetime}'] - def __init__(self, filters): + def __init__(self, filters, use_include_order): """Initialize the history period view.""" self.filters = filters + self.use_include_order = use_include_order @asyncio.coroutine def get(self, request, datetime=None): @@ -305,19 +312,22 @@ class HistoryPeriodView(HomeAssistantView): _LOGGER.debug( 'Extracted %d states in %fs', sum(map(len, result)), elapsed) - # Reorder the result to respect the ordering given by any - # entities explicitly included in the configuration. + # Optionally 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) + if self.use_include_order: + result = list(result) + 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) + result = sorted_result - return self.json(sorted_result) + return self.json(result) class Filters(object):