Skip to content
Snippets Groups Projects
Commit 6455f138 authored by Paulus Schoutsen's avatar Paulus Schoutsen
Browse files

Have logbook only report each sensor every 15 minutes

parent 9fb634ed
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ homeassistant.components.logbook ...@@ -5,6 +5,7 @@ homeassistant.components.logbook
Parses events and generates a human log Parses events and generates a human log
""" """
from datetime import datetime from datetime import datetime
from itertools import groupby
from homeassistant import State, DOMAIN as HA_DOMAIN from homeassistant import State, DOMAIN as HA_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
...@@ -25,6 +26,8 @@ QUERY_EVENTS_BETWEEN = """ ...@@ -25,6 +26,8 @@ QUERY_EVENTS_BETWEEN = """
ORDER BY time_fired ORDER BY time_fired
""" """
GROUP_BY_MINUTES = 15
def setup(hass, config): def setup(hass, config):
""" Listens for download events to download files. """ """ Listens for download events to download files. """
...@@ -72,56 +75,86 @@ class Entry(object): ...@@ -72,56 +75,86 @@ class Entry(object):
def humanify(events): def humanify(events):
""" Generator that converts a list of events into Entry objects. """ """
Generator that converts a list of events into Entry objects.
Will try to group events if possible:
- if 2+ sensor updates in GROUP_BY_MINUTES, show last
"""
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
for event in events:
if event.event_type == EVENT_STATE_CHANGED:
# Do not report on new entities # Group events in batches of GROUP_BY_MINUTES
if 'old_state' not in event.data: for _, g_events in groupby(
continue events,
lambda event: event.time_fired.minute // GROUP_BY_MINUTES):
to_state = State.from_dict(event.data.get('new_state')) events_batch = list(g_events)
if not to_state: # Keep track of last sensor states
continue last_sensor_event = {}
domain = to_state.domain # Process events
for event in events_batch:
if event.event_type == EVENT_STATE_CHANGED:
entity_id = event.data['entity_id']
entry = Entry( if entity_id.startswith('sensor.'):
event.time_fired, domain=domain, last_sensor_event[entity_id] = event
name=to_state.name, entity_id=to_state.entity_id)
if domain == 'device_tracker': # Yield entries
entry.message = '{} home'.format( for event in events_batch:
'arrived' if to_state.state == STATE_HOME else 'left') if event.event_type == EVENT_STATE_CHANGED:
elif domain == 'sun': # Do not report on new entities
if to_state.state == sun.STATE_ABOVE_HORIZON: if 'old_state' not in event.data:
entry.message = 'has risen' continue
else:
entry.message = 'has set' to_state = State.from_dict(event.data.get('new_state'))
if not to_state:
continue
domain = to_state.domain
elif to_state.state == STATE_ON: # Skip all but the last sensor state
# Future: combine groups and its entity entries ? if domain == 'sensor' and \
entry.message = "turned on" event != last_sensor_event[to_state.entity_id]:
continue
elif to_state.state == STATE_OFF: entry = Entry(
entry.message = "turned off" event.time_fired, domain=domain,
name=to_state.name, entity_id=to_state.entity_id)
else: if domain == 'device_tracker':
entry.message = "changed to {}".format(to_state.state) entry.message = '{} home'.format(
'arrived' if to_state.state == STATE_HOME else 'left')
elif domain == 'sun':
if to_state.state == sun.STATE_ABOVE_HORIZON:
entry.message = 'has risen'
else:
entry.message = 'has set'
elif to_state.state == STATE_ON:
# Future: combine groups and its entity entries ?
entry.message = "turned on"
elif to_state.state == STATE_OFF:
entry.message = "turned off"
else:
entry.message = "changed to {}".format(to_state.state)
if entry.is_valid: if entry.is_valid:
yield entry yield entry
elif event.event_type == EVENT_HOMEASSISTANT_START: elif event.event_type == EVENT_HOMEASSISTANT_START:
# Future: look for sequence stop/start and rewrite as restarted # Future: look for sequence stop/start and rewrite as restarted
yield Entry( yield Entry(
event.time_fired, "Home Assistant", "started", event.time_fired, "Home Assistant", "started",
domain=HA_DOMAIN) domain=HA_DOMAIN)
elif event.event_type == EVENT_HOMEASSISTANT_STOP: elif event.event_type == EVENT_HOMEASSISTANT_STOP:
yield Entry( yield Entry(
event.time_fired, "Home Assistant", "stopped", event.time_fired, "Home Assistant", "stopped",
domain=HA_DOMAIN) domain=HA_DOMAIN)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment