From c77d98e47b6af0b1b8fdca8fbe349f5f97fd9dc4 Mon Sep 17 00:00:00 2001 From: Bob Anderson Date: Mon, 15 Jan 2018 17:16:52 -0800 Subject: [PATCH 1/3] make history view re-ordering optional and opt-in, also fix type bug --- homeassistant/components/history.py | 44 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 8f96d95521d152..9a581f73c69062 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -20,14 +20,19 @@ 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] - - hass.http.register_view(HistoryPeriodView(filters)) + use_include_order = config[DOMAIN].get(CONF_ORDER) + + 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 @@ def get(self, request, datetime=None): _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. - - 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) + # Optionally reorder the result to respect the ordering given + # by any entities explicitly included in the configuration. + + 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(result) class Filters(object): From a3ea7273679c02d87462c50200ef64cfa0987b69 Mon Sep 17 00:00:00 2001 From: Bob Anderson Date: Mon, 15 Jan 2018 17:52:40 -0800 Subject: [PATCH 2/3] use python false for default value --- homeassistant/components/history.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 9a581f73c69062..3928242b22e184 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -31,7 +31,7 @@ CONFIG_SCHEMA = vol.Schema({ DOMAIN: recorder.FILTER_SCHEMA.extend({ - vol.Optional(CONF_ORDER, default='false'): cv.boolean, + vol.Optional(CONF_ORDER, default=False): cv.boolean, }) }, extra=vol.ALLOW_EXTRA) From afbf62ee6e70de16035be5f62abcab0e13849ca9 Mon Sep 17 00:00:00 2001 From: Bob Anderson Date: Mon, 15 Jan 2018 18:23:27 -0800 Subject: [PATCH 3/3] whitespace cleanup --- homeassistant/components/history.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 3928242b22e184..8f58f5f7e174b4 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -248,7 +248,7 @@ def async_setup(hass, config): 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, use_include_order)) yield from hass.components.frontend.async_register_built_in_panel( 'history', 'history', 'mdi:poll-box') @@ -326,7 +326,7 @@ def get(self, request, datetime=None): break sorted_result.extend(result) result = sorted_result - + return self.json(result)