History: ignore scripts that we cannot cancel

This commit is contained in:
Paulus Schoutsen 2016-03-05 10:28:48 -08:00
parent 605a572c86
commit 9701be9e9c
2 changed files with 19 additions and 3 deletions

View File

@ -11,7 +11,7 @@ from collections import defaultdict
from datetime import timedelta from datetime import timedelta
from itertools import groupby from itertools import groupby
import homeassistant.components.recorder as recorder from homeassistant.components import recorder, script
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.const import HTTP_BAD_REQUEST 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 {} " query = ("SELECT * FROM states WHERE {} "
"ORDER BY entity_id, last_updated ASC").format(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) return states_to_json(states, start_time, entity_id)
@ -202,3 +203,13 @@ def _api_history_period(handler, path_match, data):
handler.write_json( handler.write_json(
get_significant_states(start_time, end_time, entity_id).values()) 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))

View File

@ -158,6 +158,8 @@ class TestComponentHistory(unittest.TestCase):
mp = 'media_player.test' mp = 'media_player.test'
therm = 'thermostat.test' therm = 'thermostat.test'
zone = 'zone.home' zone = 'zone.home'
script_nc = 'script.cannot_cancel_this_one'
script_c = 'script.can_cancel_this_one'
def set_state(entity_id, state, **kwargs): def set_state(entity_id, state, **kwargs):
self.hass.states.set(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) three = two + timedelta(seconds=1)
four = three + timedelta(seconds=1) four = three + timedelta(seconds=1)
states = {therm: [], mp: []} states = {therm: [], mp: [], script_c: []}
with patch('homeassistant.components.recorder.dt_util.utcnow', with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=one): return_value=one):
states[mp].append( states[mp].append(
@ -189,6 +191,9 @@ class TestComponentHistory(unittest.TestCase):
attributes={'media_title': str(sentinel.mt3)}) attributes={'media_title': str(sentinel.mt3)})
# this state will be skipped because domain blacklisted # this state will be skipped because domain blacklisted
set_state(zone, 'zoning') 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( states[therm].append(
set_state(therm, 21, attributes={'current_temperature': 19.8})) set_state(therm, 21, attributes={'current_temperature': 19.8}))