Add time_fired to Event class
This commit is contained in:
parent
a21673069b
commit
a2f8fa7b05
2 changed files with 36 additions and 9 deletions
|
@ -22,7 +22,7 @@ from homeassistant.const import (
|
||||||
SERVICE_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED,
|
SERVICE_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED,
|
||||||
EVENT_CALL_SERVICE, ATTR_NOW, ATTR_DOMAIN, ATTR_SERVICE, MATCH_ALL,
|
EVENT_CALL_SERVICE, ATTR_NOW, ATTR_DOMAIN, ATTR_SERVICE, MATCH_ALL,
|
||||||
EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID, EVENT_SERVICE_REGISTERED,
|
EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID, EVENT_SERVICE_REGISTERED,
|
||||||
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME)
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
|
|
||||||
DOMAIN = "homeassistant"
|
DOMAIN = "homeassistant"
|
||||||
|
@ -325,19 +325,23 @@ class EventOrigin(enum.Enum):
|
||||||
class Event(object):
|
class Event(object):
|
||||||
""" Represents an event within the Bus. """
|
""" Represents an event within the Bus. """
|
||||||
|
|
||||||
__slots__ = ['event_type', 'data', 'origin']
|
__slots__ = ['event_type', 'data', 'origin', 'time_fired']
|
||||||
|
|
||||||
def __init__(self, event_type, data=None, origin=EventOrigin.local):
|
def __init__(self, event_type, data=None, origin=EventOrigin.local,
|
||||||
|
time_fired=None):
|
||||||
self.event_type = event_type
|
self.event_type = event_type
|
||||||
self.data = data or {}
|
self.data = data or {}
|
||||||
self.origin = origin
|
self.origin = origin
|
||||||
|
self.time_fired = util.strip_microseconds(
|
||||||
|
time_fired or dt.datetime.now())
|
||||||
|
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
""" Returns a dict representation of this Event. """
|
""" Returns a dict representation of this Event. """
|
||||||
return {
|
return {
|
||||||
'event_type': self.event_type,
|
'event_type': self.event_type,
|
||||||
'data': dict(self.data),
|
'data': dict(self.data),
|
||||||
'origin': str(self.origin)
|
'origin': str(self.origin),
|
||||||
|
'time_fired': util.datetime_to_str(self.time_fired),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -482,6 +486,18 @@ class State(object):
|
||||||
""" Returns domain of this state. """
|
""" Returns domain of this state. """
|
||||||
return util.split_entity_id(self.entity_id)[0]
|
return util.split_entity_id(self.entity_id)[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def object_id(self):
|
||||||
|
""" Returns object_id of this state. """
|
||||||
|
return util.split_entity_id(self.entity_id)[1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
""" Name to represent this state. """
|
||||||
|
return (
|
||||||
|
self.attributes.get(ATTR_FRIENDLY_NAME) or
|
||||||
|
self.object_id.replace('_', ' '))
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
""" Creates a copy of itself. """
|
""" Creates a copy of itself. """
|
||||||
return State(self.entity_id, self.state,
|
return State(self.entity_id, self.state,
|
||||||
|
|
|
@ -70,9 +70,10 @@ def row_to_state(row):
|
||||||
def row_to_event(row):
|
def row_to_event(row):
|
||||||
""" Convert a databse row to an event. """
|
""" Convert a databse row to an event. """
|
||||||
try:
|
try:
|
||||||
return Event(row[1], json.loads(row[2]), EventOrigin[row[3].lower()])
|
return Event(row[1], json.loads(row[2]), EventOrigin[row[3].lower()],
|
||||||
|
datetime.fromtimestamp(row[5]))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# When json.oads fails
|
# When json.loads fails
|
||||||
_LOGGER.exception("Error converting row to event: %s", row)
|
_LOGGER.exception("Error converting row to event: %s", row)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -225,13 +226,13 @@ class Recorder(threading.Thread):
|
||||||
""" Save an event to the database. """
|
""" Save an event to the database. """
|
||||||
info = (
|
info = (
|
||||||
event.event_type, json.dumps(event.data, cls=JSONEncoder),
|
event.event_type, json.dumps(event.data, cls=JSONEncoder),
|
||||||
str(event.origin), datetime.now()
|
str(event.origin), datetime.now(), event.time_fired,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.query(
|
self.query(
|
||||||
"INSERT INTO events ("
|
"INSERT INTO events ("
|
||||||
"event_type, event_data, origin, created"
|
"event_type, event_data, origin, created, time_fired"
|
||||||
") VALUES (?, ?, ?, ?)", info)
|
") VALUES (?, ?, ?, ?, ?)", info)
|
||||||
|
|
||||||
def query(self, sql_query, data=None, return_value=None):
|
def query(self, sql_query, data=None, return_value=None):
|
||||||
""" Query the database. """
|
""" Query the database. """
|
||||||
|
@ -328,6 +329,16 @@ class Recorder(threading.Thread):
|
||||||
|
|
||||||
save_migration(1)
|
save_migration(1)
|
||||||
|
|
||||||
|
if migration_id < 2:
|
||||||
|
cur.execute("""
|
||||||
|
ALTER TABLE events
|
||||||
|
ADD COLUMN time_fired integer
|
||||||
|
""")
|
||||||
|
|
||||||
|
cur.execute('UPDATE events SET time_fired=created')
|
||||||
|
|
||||||
|
save_migration(2)
|
||||||
|
|
||||||
def _close_connection(self):
|
def _close_connection(self):
|
||||||
""" Close connection to the database. """
|
""" Close connection to the database. """
|
||||||
_LOGGER.info("Closing database")
|
_LOGGER.info("Closing database")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue