fixes for pep and delay start

This commit is contained in:
Alex Harvey 2016-05-12 10:33:22 -07:00
parent 67b0365f62
commit 93fd6fa11b
2 changed files with 13 additions and 9 deletions

View File

@ -22,6 +22,7 @@ from homeassistant.const import (
EVENT_TIME_CHANGED, MATCH_ALL) EVENT_TIME_CHANGED, MATCH_ALL)
from homeassistant.core import Event, EventOrigin, State from homeassistant.core import Event, EventOrigin, State
from homeassistant.remote import JSONEncoder from homeassistant.remote import JSONEncoder
from homeassistant.helpers.event import track_point_in_utc_time
DOMAIN = "recorder" DOMAIN = "recorder"
@ -33,8 +34,9 @@ RETURN_ONE_ROW = "one_row"
CONF_PURGE_DAYS = "purge_days" CONF_PURGE_DAYS = "purge_days"
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.All(dict, { DOMAIN: vol.Schema({
CONF_PURGE_DAYS: int vol.Optional(CONF_PURGE_DAYS): vol.All(vol.Coerce(int),
vol.Range(min=1)),
}) })
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
@ -204,7 +206,10 @@ class Recorder(threading.Thread):
"""Start processing events to save.""" """Start processing events to save."""
self._setup_connection() self._setup_connection()
self._setup_run() self._setup_run()
self._purge_old_data() if self.purge_days is not None:
track_point_in_utc_time(self.hass,
lambda now: self._purge_old_data(),
dt_util.utcnow() + timedelta(minutes=5))
while True: while True:
event = self.queue.get() event = self.queue.get()

View File

@ -43,8 +43,9 @@ class TestRecorder(unittest.TestCase):
timestamp = now timestamp = now
state = 'dontpurgeme' state = 'dontpurgeme'
recorder.query("INSERT INTO states (" recorder.query("INSERT INTO states ("
"entity_id, domain, state, attributes, last_changed," "entity_id, domain, state, attributes,"
"last_updated, created, utc_offset, event_id)" "last_changed, last_updated, created,"
"utc_offset, event_id)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
('test.recorder2', 'sensor', state, ('test.recorder2', 'sensor', state,
json.dumps(attributes), timestamp, timestamp, json.dumps(attributes), timestamp, timestamp,
@ -155,7 +156,6 @@ class TestRecorder(unittest.TestCase):
'event_type LIKE "EVENT_TEST%"') 'event_type LIKE "EVENT_TEST%"')
self.assertEqual(len(events), 3) self.assertEqual(len(events), 3)
def test_purge_disabled(self): def test_purge_disabled(self):
"""Tests leaving purge_days disabled.""" """Tests leaving purge_days disabled."""
self._add_test_states() self._add_test_states()
@ -167,7 +167,6 @@ class TestRecorder(unittest.TestCase):
self.assertEqual(len(states), 5) self.assertEqual(len(states), 5)
self.assertEqual(len(events), 5) self.assertEqual(len(events), 5)
# run purge_old_data() # run purge_old_data()
recorder._INSTANCE.purge_days = None recorder._INSTANCE.purge_days = None
recorder._INSTANCE._purge_old_data() recorder._INSTANCE._purge_old_data()