Filter entities from logbook (#3426)
* o added ability to exclude entities or domains from logbook o exclude hidden entities * fixed remaned configuration key * - filter the events before they get passed to humanify, to separate concerns - instead of looking at customize, look for the hidden attribute on the state change events - access to configuration defaults to an empty list - no need to check * - filter only events of type EVENT_STATE_CHANGED - improve config handling * added unit tests to cover all filter cases and logbook message creation
This commit is contained in:
1 parent
eb1871dc5b
commit
e891f1a260
2 files changed
+250
-10
No files matched your search
@@ -17,7 +17,8 @@ from homeassistant.components.frontend import register_built_in_panel
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.const import (EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED,
|
||||
STATE_NOT_HOME, STATE_OFF, STATE_ON)
|
||||
STATE_NOT_HOME, STATE_OFF, STATE_ON,
|
||||
ATTR_HIDDEN)
|
||||
from homeassistant.core import State, split_entity_id, DOMAIN as HA_DOMAIN
|
||||
from homeassistant.helpers import template
|
||||
|
||||
@@ -26,6 +27,19 @@ DEPENDENCIES = ['recorder', 'frontend']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_EXCLUDE = 'exclude'
|
||||
CONF_ENTITIES = 'entities'
|
||||
CONF_DOMAINS = 'domains'
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
CONF_EXCLUDE: vol.Schema({
|
||||
vol.Optional(CONF_ENTITIES, default=[]): cv.ensure_list,
|
||||
vol.Optional(CONF_DOMAINS, default=[]): cv.ensure_list
|
||||
}),
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
EVENT_LOGBOOK_ENTRY = 'logbook_entry'
|
||||
|
||||
GROUP_BY_MINUTES = 15
|
||||
@@ -69,7 +83,7 @@ def setup(hass, config):
|
||||
message = template.render(hass, message)
|
||||
log_entry(hass, name, message, domain, entity_id)
|
||||
|
||||
hass.wsgi.register_view(LogbookView)
|
||||
hass.wsgi.register_view(LogbookView(hass, config))
|
||||
|
||||
register_built_in_panel(hass, 'logbook', 'Logbook',
|
||||
'mdi:format-list-bulleted-type')
|
||||
@@ -86,6 +100,11 @@ class LogbookView(HomeAssistantView):
|
||||
name = 'api:logbook'
|
||||
extra_urls = ['/api/logbook/<datetime:datetime>']
|
||||
|
||||
def __init__(self, hass, config):
|
||||
"""Initilalize the logbook view."""
|
||||
super().__init__(hass)
|
||||
self.config = config
|
||||
|
||||
def get(self, request, datetime=None):
|
||||
"""Retrieve logbook entries."""
|
||||
start_day = dt_util.as_utc(datetime or dt_util.start_of_local_day())
|
||||
@@ -96,6 +115,7 @@ class LogbookView(HomeAssistantView):
|
||||
(events.time_fired > start_day) &
|
||||
(events.time_fired < end_day))
|
||||
events = recorder.execute(query)
|
||||
events = _exclude_events(events, self.config)
|
||||
|
||||
return self.json(humanify(events))
|
||||
|
||||
@@ -173,10 +193,6 @@ def humanify(events):
|
||||
for event in events_batch:
|
||||
if event.event_type == EVENT_STATE_CHANGED:
|
||||
|
||||
# Do not report on new entities
|
||||
if 'old_state' not in event.data:
|
||||
continue
|
||||
|
||||
to_state = State.from_dict(event.data.get('new_state'))
|
||||
|
||||
# If last_changed != last_updated only attributes have changed
|
||||
@@ -239,6 +255,39 @@ def humanify(events):
|
||||
entity_id)
|
||||
|
||||
|
||||
def _exclude_events(events, config):
|
||||
"""Get lists of excluded entities and platforms."""
|
||||
excluded_entities = []
|
||||
excluded_domains = []
|
||||
exclude = config[DOMAIN].get(CONF_EXCLUDE)
|
||||
if exclude:
|
||||
excluded_entities = exclude[CONF_ENTITIES]
|
||||
excluded_domains = exclude[CONF_DOMAINS]
|
||||
|
||||
filtered_events = []
|
||||
for event in events:
|
||||
if event.event_type == EVENT_STATE_CHANGED:
|
||||
to_state = State.from_dict(event.data.get('new_state'))
|
||||
# Do not report on new entities
|
||||
if not to_state:
|
||||
continue
|
||||
|
||||
# exclude entities which are customized hidden
|
||||
hidden = to_state.attributes.get(ATTR_HIDDEN, False)
|
||||
if hidden:
|
||||
continue
|
||||
|
||||
domain = to_state.domain
|
||||
# check if logbook entry is excluded for this domain
|
||||
if domain in excluded_domains:
|
||||
continue
|
||||
# check if logbook entry is excluded for this entity
|
||||
if to_state.entity_id in excluded_entities:
|
||||
continue
|
||||
filtered_events.append(event)
|
||||
return filtered_events
|
||||
|
||||
|
||||
def _entry_message_from_state(domain, state):
|
||||
"""Convert a state to a message for the logbook."""
|
||||
# We pass domain in so we don't have to split entity_id again
|
||||
|
||||
Reference in new issue
Block a user