Have logbook only report each sensor every 15 minutes
This commit is contained in:
parent
9fb634ed3a
commit
6455f1388a
1 changed files with 74 additions and 41 deletions
|
@ -5,6 +5,7 @@ homeassistant.components.logbook
|
|||
Parses events and generates a human log
|
||||
"""
|
||||
from datetime import datetime
|
||||
from itertools import groupby
|
||||
|
||||
from homeassistant import State, DOMAIN as HA_DOMAIN
|
||||
from homeassistant.const import (
|
||||
|
@ -25,6 +26,8 @@ QUERY_EVENTS_BETWEEN = """
|
|||
ORDER BY time_fired
|
||||
"""
|
||||
|
||||
GROUP_BY_MINUTES = 15
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
""" Listens for download events to download files. """
|
||||
|
@ -72,9 +75,34 @@ class Entry(object):
|
|||
|
||||
|
||||
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
|
||||
for event in events:
|
||||
|
||||
# Group events in batches of GROUP_BY_MINUTES
|
||||
for _, g_events in groupby(
|
||||
events,
|
||||
lambda event: event.time_fired.minute // GROUP_BY_MINUTES):
|
||||
|
||||
events_batch = list(g_events)
|
||||
|
||||
# Keep track of last sensor states
|
||||
last_sensor_event = {}
|
||||
|
||||
# Process events
|
||||
for event in events_batch:
|
||||
if event.event_type == EVENT_STATE_CHANGED:
|
||||
entity_id = event.data['entity_id']
|
||||
|
||||
if entity_id.startswith('sensor.'):
|
||||
last_sensor_event[entity_id] = event
|
||||
|
||||
# Yield entries
|
||||
for event in events_batch:
|
||||
if event.event_type == EVENT_STATE_CHANGED:
|
||||
|
||||
# Do not report on new entities
|
||||
|
@ -88,6 +116,11 @@ def humanify(events):
|
|||
|
||||
domain = to_state.domain
|
||||
|
||||
# Skip all but the last sensor state
|
||||
if domain == 'sensor' and \
|
||||
event != last_sensor_event[to_state.entity_id]:
|
||||
continue
|
||||
|
||||
entry = Entry(
|
||||
event.time_fired, domain=domain,
|
||||
name=to_state.name, entity_id=to_state.entity_id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue