Exclude filter for event types (#7627)

* add exclude filter for event types to recorder component

* corrected long line (279)

* change source code structure
add test for exclude event types

* code cleanup

* change source code structure

* Update __init__.py

* Update test_init.py
This commit is contained in:
amigian74 2017-05-25 00:23:52 +02:00 committed by Paulus Schoutsen
parent e7d783ca2a
commit 775d45ae5a
2 changed files with 27 additions and 0 deletions

View File

@ -44,6 +44,7 @@ DEFAULT_DB_FILE = 'home-assistant_v2.db'
CONF_DB_URL = 'db_url'
CONF_PURGE_DAYS = 'purge_days'
CONF_EVENT_TYPES = 'event_types'
CONNECT_RETRY_WAIT = 3
@ -51,6 +52,8 @@ FILTER_SCHEMA = vol.Schema({
vol.Optional(CONF_EXCLUDE, default={}): vol.Schema({
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
vol.Optional(CONF_DOMAINS, default=[]):
vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_EVENT_TYPES, default=[]):
vol.All(cv.ensure_list, [cv.string])
}),
vol.Optional(CONF_INCLUDE, default={}): vol.Schema({
@ -142,6 +145,7 @@ class Recorder(threading.Thread):
self.include_d = include.get(CONF_DOMAINS, [])
self.exclude = exclude.get(CONF_ENTITIES, []) + \
exclude.get(CONF_DOMAINS, [])
self.exclude_t = exclude.get(CONF_EVENT_TYPES, [])
self.get_session = None
@ -245,6 +249,9 @@ class Recorder(threading.Thread):
elif event.event_type == EVENT_TIME_CHANGED:
self.queue.task_done()
continue
elif event.event_type in self.exclude_t:
self.queue.task_done()
continue
entity_id = event.data.get(ATTR_ENTITY_ID)
if entity_id is not None:

View File

@ -114,6 +114,18 @@ def _add_entities(hass, entity_ids):
return [st.to_native() for st in session.query(States)]
def _add_events(hass, events):
with session_scope(hass=hass) as session:
session.query(Events).delete(synchronize_session=False)
for event_type in events:
hass.bus.fire(event_type)
hass.block_till_done()
hass.data[DATA_INSTANCE].block_till_done()
with session_scope(hass=hass) as session:
return [ev.to_native() for ev in session.query(Events)]
# pylint: disable=redefined-outer-name,invalid-name
def test_saving_state_include_domains(hass_recorder):
"""Test saving and restoring a state."""
@ -131,6 +143,14 @@ def test_saving_state_incl_entities(hass_recorder):
assert hass.states.get('test2.recorder') == states[0]
def test_saving_event_exclude_event_type(hass_recorder):
"""Test saving and restoring an event."""
hass = hass_recorder({'exclude': {'event_types': 'test'}})
events = _add_events(hass, ['test', 'test2'])
assert len(events) == 1
assert events[0].event_type == 'test2'
def test_saving_state_exclude_domains(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({'exclude': {'domains': 'test'}})