Added include and exclude functionality to history component (#3674)

* added include and exclude functionality to history component

* fixed summary lines in test method doc.

* cleanup of query filter creation

* o improved config validation
o move move IGNORE_DOMAINS to Filter.apply()
o removed config from Last5StatesView
o Filters instance is now created on setup
o config values are processed in setup and set to the Filters instance
o function _set_filters_in_query() moved to Filters class and renamed to apply()

* added unittests for more include/exclude filter combinations

* make pylint happy
This commit is contained in:
wokar authored and Paulus Schoutsen committed 2016-10-13 08:54:45 -07:00
1 parent e031b8078f
commit aa8622f8e8
2 files changed
+371 -32

No files matched your search

+243 -7
View File
@@ -43,7 +43,15 @@ class TestComponentHistory(unittest.TestCase):
def test_setup(self):
"""Test setup method of history."""
mock_http_component(self.hass)
self.assertTrue(setup_component(self.hass, history.DOMAIN, {}))
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_DOMAINS: ['media_player'],
history.CONF_ENTITIES: ['thermostat.test']},
history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['thermostat'],
history.CONF_ENTITIES: ['media_player.test']}}})
self.assertTrue(setup_component(self.hass, history.DOMAIN, config))
def test_last_5_states(self):
"""Test retrieving the last 5 states."""
@@ -145,14 +153,236 @@ class TestComponentHistory(unittest.TestCase):
def test_get_significant_states(self):
"""Test that only significant states are returned.
We inject a bunch of state updates from media player, zone and
thermostat. We should get back every thermostat change that
We should get back every thermostat change that
includes an attribute change, but only the state updates for
media player (attribute changes are not significant and not returned).
"""
zero, four, states = self.record_states()
hist = history.get_significant_states(
zero, four, filters=history.Filters())
assert states == hist
def test_get_significant_states_entity_id(self):
"""Test that only significant states are returned for one entity."""
zero, four, states = self.record_states()
del states['media_player.test2']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
hist = history.get_significant_states(
zero, four, 'media_player.test',
filters=history.Filters())
assert states == hist
def test_get_significant_states_exclude_domain(self):
"""Test if significant states are returned when excluding domains.
We should get back every thermostat change that includes an attribute
change, but no media player changes.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['media_player.test2']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['media_player', ]}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_exclude_entity(self):
"""Test if significant states are returned when excluding entities.
We should get back every thermostat and script changes, but no media
player changes.
"""
zero, four, states = self.record_states()
del states['media_player.test']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_EXCLUDE: {
history.CONF_ENTITIES: ['media_player.test', ]}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_exclude(self):
"""Test significant states when excluding entities and domains.
We should not get back every thermostat and media player test changes.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['thermostat.test']
del states['thermostat.test2']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['thermostat', ],
history.CONF_ENTITIES: ['media_player.test', ]}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_exclude_include_entity(self):
"""Test significant states when excluding domains and include entities.
We should not get back every thermostat and media player test changes.
"""
zero, four, states = self.record_states()
del states['media_player.test2']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {
history.CONF_INCLUDE: {
history.CONF_ENTITIES: ['media_player.test',
'thermostat.test']},
history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['thermostat']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include_domain(self):
"""Test if significant states are returned when including domains.
We should get back every thermostat and script changes, but no media
player changes.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['media_player.test2']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_DOMAINS: ['thermostat', 'script']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include_entity(self):
"""Test if significant states are returned when including entities.
We should only get back changes of the media_player.test entity.
"""
zero, four, states = self.record_states()
del states['media_player.test2']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_ENTITIES: ['media_player.test']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include(self):
"""Test significant states when including domains and entities.
We should only get back changes of the media_player.test entity and the
thermostat domain.
"""
zero, four, states = self.record_states()
del states['media_player.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_DOMAINS: ['thermostat'],
history.CONF_ENTITIES: ['media_player.test']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include_exclude_domain(self):
"""Test if significant states when excluding and including domains.
We should not get back any changes since we include only the
media_player domain but also exclude it.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['media_player.test2']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_DOMAINS: ['media_player']},
history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['media_player']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include_exclude_entity(self):
"""Test if significant states when excluding and including domains.
We should not get back any changes since we include only
media_player.test but also exclude it.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['media_player.test2']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_ENTITIES: ['media_player.test']},
history.CONF_EXCLUDE: {
history.CONF_ENTITIES: ['media_player.test']}}})
self.check_significant_states(zero, four, states, config)
def test_get_significant_states_include_exclude(self):
"""Test if significant states when in/excluding domains and entities.
We should only get back changes of the media_player.test2 entity.
"""
zero, four, states = self.record_states()
del states['media_player.test']
del states['thermostat.test']
del states['thermostat.test2']
del states['script.can_cancel_this_one']
config = history.CONFIG_SCHEMA({
ha.DOMAIN: {},
history.DOMAIN: {history.CONF_INCLUDE: {
history.CONF_DOMAINS: ['media_player'],
history.CONF_ENTITIES: ['thermostat.test']},
history.CONF_EXCLUDE: {
history.CONF_DOMAINS: ['thermostat'],
history.CONF_ENTITIES: ['media_player.test']}}})
self.check_significant_states(zero, four, states, config)
def check_significant_states(self, zero, four, states, config):
"""Check if significant states are retrieved."""
filters = history.Filters()
exclude = config[history.DOMAIN].get(history.CONF_EXCLUDE)
if exclude:
filters.excluded_entities = exclude[history.CONF_ENTITIES]
filters.excluded_domains = exclude[history.CONF_DOMAINS]
include = config[history.DOMAIN].get(history.CONF_INCLUDE)
if include:
filters.included_entities = include[history.CONF_ENTITIES]
filters.included_domains = include[history.CONF_DOMAINS]
hist = history.get_significant_states(zero, four, filters=filters)
assert states == hist
def record_states(self):
"""Record some test states.
We inject a bunch of state updates from media player, zone and
thermostat.
"""
self.init_recorder()
mp = 'media_player.test'
mp2 = 'media_player.test2'
therm = 'thermostat.test'
therm2 = 'thermostat.test2'
zone = 'zone.home'
script_nc = 'script.cannot_cancel_this_one'
script_c = 'script.can_cancel_this_one'
@@ -168,7 +398,7 @@ class TestComponentHistory(unittest.TestCase):
three = two + timedelta(seconds=1)
four = three + timedelta(seconds=1)
states = {therm: [], mp: [], script_c: []}
states = {therm: [], therm2: [], mp: [], mp2: [], script_c: []}
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=one):
states[mp].append(
@@ -177,6 +407,9 @@ class TestComponentHistory(unittest.TestCase):
states[mp].append(
set_state(mp, 'YouTube',
attributes={'media_title': str(sentinel.mt2)}))
states[mp2].append(
set_state(mp2, 'YouTube',
attributes={'media_title': str(sentinel.mt2)}))
states[therm].append(
set_state(therm, 20, attributes={'current_temperature': 19.5}))
@@ -192,6 +425,8 @@ class TestComponentHistory(unittest.TestCase):
set_state(script_c, 'off', attributes={'can_cancel': True}))
states[therm].append(
set_state(therm, 21, attributes={'current_temperature': 19.8}))
states[therm2].append(
set_state(therm2, 20, attributes={'current_temperature': 19}))
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=three):
@@ -201,6 +436,7 @@ class TestComponentHistory(unittest.TestCase):
# Attributes changed even though state is the same
states[therm].append(
set_state(therm, 21, attributes={'current_temperature': 20}))
hist = history.get_significant_states(zero, four)
assert states == hist
# state will be skipped since entity is hidden
set_state(therm, 22, attributes={'current_temperature': 21,
'hidden': True})
return zero, four, states