From a9917e7a56e4056d3bef59d810e637637a1317d5 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 14 Mar 2018 14:29:51 -0700 Subject: [PATCH] Fix history API (#13214) --- homeassistant/components/history.py | 10 +++++----- tests/components/test_history.py | 12 +++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index dd14bbf6811..8ab91b08a3d 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -239,15 +239,16 @@ def get_state(hass, utc_point_in_time, entity_id, run=None): def async_setup(hass, config): """Set up the history hooks.""" filters = Filters() - exclude = config[DOMAIN].get(CONF_EXCLUDE) + conf = config.get(DOMAIN, {}) + exclude = conf.get(CONF_EXCLUDE) if exclude: filters.excluded_entities = exclude.get(CONF_ENTITIES, []) filters.excluded_domains = exclude.get(CONF_DOMAINS, []) - include = config[DOMAIN].get(CONF_INCLUDE) + include = conf.get(CONF_INCLUDE) if include: filters.included_entities = include.get(CONF_ENTITIES, []) filters.included_domains = include.get(CONF_DOMAINS, []) - use_include_order = config[DOMAIN].get(CONF_ORDER) + use_include_order = conf.get(CONF_ORDER) hass.http.register_view(HistoryPeriodView(filters, use_include_order)) yield from hass.components.frontend.async_register_built_in_panel( @@ -308,7 +309,7 @@ class HistoryPeriodView(HomeAssistantView): result = yield from hass.async_add_job( get_significant_states, hass, start_time, end_time, entity_ids, self.filters, include_start_time_state) - result = result.values() + result = list(result.values()) if _LOGGER.isEnabledFor(logging.DEBUG): elapsed = time.perf_counter() - timer_start _LOGGER.debug( @@ -318,7 +319,6 @@ class HistoryPeriodView(HomeAssistantView): # 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: diff --git a/tests/components/test_history.py b/tests/components/test_history.py index 4a759e7e0ac..be768f5ec69 100644 --- a/tests/components/test_history.py +++ b/tests/components/test_history.py @@ -4,7 +4,7 @@ from datetime import timedelta import unittest from unittest.mock import patch, sentinel -from homeassistant.setup import setup_component +from homeassistant.setup import setup_component, async_setup_component import homeassistant.core as ha import homeassistant.util.dt as dt_util from homeassistant.components import history, recorder @@ -481,3 +481,13 @@ class TestComponentHistory(unittest.TestCase): set_state(therm, 22, attributes={'current_temperature': 21, 'hidden': True}) return zero, four, states + + +async def test_fetch_period_api(hass, test_client): + """Test the fetch period view for history.""" + await hass.async_add_job(init_recorder_component, hass) + await async_setup_component(hass, 'history', {}) + client = await test_client(hass.http.app) + response = await client.get( + '/api/history/period/{}'.format(dt_util.utcnow().isoformat())) + assert response.status == 200