mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Clean up logbook component
This commit is contained in:
parent
742479f8bd
commit
30e7f09000
@ -48,7 +48,7 @@ def _handle_get_logbook(handler, path_match, data):
|
|||||||
class Entry(object):
|
class Entry(object):
|
||||||
""" A human readable version of the log. """
|
""" A human readable version of the log. """
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments, too-few-public-methods
|
||||||
|
|
||||||
def __init__(self, when=None, name=None, message=None, domain=None,
|
def __init__(self, when=None, name=None, message=None, domain=None,
|
||||||
entity_id=None):
|
entity_id=None):
|
||||||
@ -58,11 +58,6 @@ class Entry(object):
|
|||||||
self.domain = domain
|
self.domain = domain
|
||||||
self.entity_id = entity_id
|
self.entity_id = entity_id
|
||||||
|
|
||||||
@property
|
|
||||||
def is_valid(self):
|
|
||||||
""" Returns if this entry contains all the needed fields. """
|
|
||||||
return self.when and self.name and self.message
|
|
||||||
|
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
""" Convert Entry to a dict to be used within JSON. """
|
""" Convert Entry to a dict to be used within JSON. """
|
||||||
return {
|
return {
|
||||||
@ -82,7 +77,7 @@ def humanify(events):
|
|||||||
- if 2+ sensor updates in GROUP_BY_MINUTES, show last
|
- if 2+ sensor updates in GROUP_BY_MINUTES, show last
|
||||||
- if home assistant stop and start happen in same minute call it restarted
|
- if home assistant stop and start happen in same minute call it restarted
|
||||||
"""
|
"""
|
||||||
# pylint: disable=too-many-branches, too-many-statements
|
# pylint: disable=too-many-branches
|
||||||
|
|
||||||
# Group events in batches of GROUP_BY_MINUTES
|
# Group events in batches of GROUP_BY_MINUTES
|
||||||
for _, g_events in groupby(
|
for _, g_events in groupby(
|
||||||
@ -138,32 +133,12 @@ def humanify(events):
|
|||||||
event != last_sensor_event[to_state.entity_id]:
|
event != last_sensor_event[to_state.entity_id]:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entry = Entry(
|
yield Entry(
|
||||||
event.time_fired, domain=domain,
|
event.time_fired,
|
||||||
name=to_state.name, entity_id=to_state.entity_id)
|
name=to_state.name,
|
||||||
|
message=_entry_message_from_state(domain, to_state),
|
||||||
if domain == 'device_tracker':
|
domain=domain,
|
||||||
entry.message = '{} home'.format(
|
entity_id=to_state.entity_id)
|
||||||
'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:
|
|
||||||
yield entry
|
|
||||||
|
|
||||||
elif event.event_type == EVENT_HOMEASSISTANT_START:
|
elif event.event_type == EVENT_HOMEASSISTANT_START:
|
||||||
if start_stop_events.get(event.time_fired.minute) == 2:
|
if start_stop_events.get(event.time_fired.minute) == 2:
|
||||||
@ -182,3 +157,27 @@ def humanify(events):
|
|||||||
yield Entry(
|
yield Entry(
|
||||||
event.time_fired, "Home Assistant", action,
|
event.time_fired, "Home Assistant", action,
|
||||||
domain=HA_DOMAIN)
|
domain=HA_DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
if domain == 'device_tracker':
|
||||||
|
return '{} home'.format(
|
||||||
|
'arrived' if state.state == STATE_HOME else 'left')
|
||||||
|
|
||||||
|
elif domain == 'sun':
|
||||||
|
if state.state == sun.STATE_ABOVE_HORIZON:
|
||||||
|
return 'has risen'
|
||||||
|
else:
|
||||||
|
return 'has set'
|
||||||
|
|
||||||
|
elif state.state == STATE_ON:
|
||||||
|
# Future: combine groups and its entity entries ?
|
||||||
|
return "turned on"
|
||||||
|
|
||||||
|
elif state.state == STATE_OFF:
|
||||||
|
return "turned off"
|
||||||
|
|
||||||
|
return "changed to {}".format(state.state)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user