diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index 13cea036ed7..210590ea58c 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -11,7 +11,7 @@ from collections import defaultdict from datetime import timedelta from itertools import groupby -import homeassistant.components.recorder as recorder +from homeassistant.components import recorder, script import homeassistant.util.dt as dt_util from homeassistant.const import HTTP_BAD_REQUEST @@ -65,7 +65,8 @@ def get_significant_states(start_time, end_time=None, entity_id=None): query = ("SELECT * FROM states WHERE {} " "ORDER BY entity_id, last_updated ASC").format(where) - states = recorder.query_states(query, data) + states = (state for state in recorder.query_states(query, data) + if _is_significant(state)) return states_to_json(states, start_time, entity_id) @@ -202,3 +203,13 @@ def _api_history_period(handler, path_match, data): handler.write_json( get_significant_states(start_time, end_time, entity_id).values()) + + +def _is_significant(state): + """Test if state is significant for history charts. + + Will only test for things that are not filtered out in SQL. + """ + # scripts that are not cancellable will never change state + return (state.domain != 'script' or + state.attributes.get(script.ATTR_CAN_CANCEL)) diff --git a/tests/components/test_history.py b/tests/components/test_history.py index 3d8211553b3..3f781b9d30c 100644 --- a/tests/components/test_history.py +++ b/tests/components/test_history.py @@ -158,6 +158,8 @@ class TestComponentHistory(unittest.TestCase): mp = 'media_player.test' therm = 'thermostat.test' zone = 'zone.home' + script_nc = 'script.cannot_cancel_this_one' + script_c = 'script.can_cancel_this_one' def set_state(entity_id, state, **kwargs): self.hass.states.set(entity_id, state, **kwargs) @@ -170,7 +172,7 @@ class TestComponentHistory(unittest.TestCase): three = two + timedelta(seconds=1) four = three + timedelta(seconds=1) - states = {therm: [], mp: []} + states = {therm: [], mp: [], script_c: []} with patch('homeassistant.components.recorder.dt_util.utcnow', return_value=one): states[mp].append( @@ -189,6 +191,9 @@ class TestComponentHistory(unittest.TestCase): attributes={'media_title': str(sentinel.mt3)}) # this state will be skipped because domain blacklisted set_state(zone, 'zoning') + set_state(script_nc, 'off') + states[script_c].append( + set_state(script_c, 'off', attributes={'can_cancel': True})) states[therm].append( set_state(therm, 21, attributes={'current_temperature': 19.8}))