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
This commit is contained in:
Bob Anderson 2018-01-16 11:48:10 -08:00 committed by Paulus Schoutsen
parent 3a00077305
commit 94950cccc8

View File

@ -20,14 +20,19 @@ from homeassistant.components import recorder, script
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.const import ATTR_HIDDEN from homeassistant.const import ATTR_HIDDEN
from homeassistant.components.recorder.util import session_scope, execute from homeassistant.components.recorder.util import session_scope, execute
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = 'history' DOMAIN = 'history'
DEPENDENCIES = ['recorder', 'http'] DEPENDENCIES = ['recorder', 'http']
CONF_ORDER = 'use_include_order'
CONFIG_SCHEMA = vol.Schema({ 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) }, extra=vol.ALLOW_EXTRA)
SIGNIFICANT_DOMAINS = ('thermostat', 'climate') SIGNIFICANT_DOMAINS = ('thermostat', 'climate')
@ -242,8 +247,9 @@ def async_setup(hass, config):
if include: if include:
filters.included_entities = include[CONF_ENTITIES] filters.included_entities = include[CONF_ENTITIES]
filters.included_domains = include[CONF_DOMAINS] 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( yield from hass.components.frontend.async_register_built_in_panel(
'history', 'history', 'mdi:poll-box') 'history', 'history', 'mdi:poll-box')
@ -257,9 +263,10 @@ class HistoryPeriodView(HomeAssistantView):
name = 'api:history:view-period' name = 'api:history:view-period'
extra_urls = ['/api/history/period/{datetime}'] extra_urls = ['/api/history/period/{datetime}']
def __init__(self, filters): def __init__(self, filters, use_include_order):
"""Initialize the history period view.""" """Initialize the history period view."""
self.filters = filters self.filters = filters
self.use_include_order = use_include_order
@asyncio.coroutine @asyncio.coroutine
def get(self, request, datetime=None): def get(self, request, datetime=None):
@ -305,19 +312,22 @@ class HistoryPeriodView(HomeAssistantView):
_LOGGER.debug( _LOGGER.debug(
'Extracted %d states in %fs', sum(map(len, result)), elapsed) 'Extracted %d states in %fs', sum(map(len, result)), elapsed)
# Reorder the result to respect the ordering given by any # Optionally reorder the result to respect the ordering given
# entities explicitly included in the configuration. # by any entities explicitly included in the configuration.
sorted_result = [] if self.use_include_order:
for order_entity in self.filters.included_entities: result = list(result)
for state_list in result: sorted_result = []
if state_list[0].entity_id == order_entity: for order_entity in self.filters.included_entities:
sorted_result.append(state_list) for state_list in result:
result.remove(state_list) if state_list[0].entity_id == order_entity:
break sorted_result.append(state_list)
sorted_result.extend(result) result.remove(state_list)
break
sorted_result.extend(result)
result = sorted_result
return self.json(sorted_result) return self.json(result)
class Filters(object): class Filters(object):