From 7c45318c00d2b73db7584f9dbca3af3c92e3922a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 28 Jan 2015 00:22:09 -0800 Subject: [PATCH] Recorder component: proof of concept --- homeassistant/__init__.py | 11 +- homeassistant/components/http/__init__.py | 25 ++ .../polymer/components/recent-states.html | 31 +++ .../polymer/more-infos/more-info-default.html | 6 +- homeassistant/components/recorder.py | 230 ++++++++++++++++++ homeassistant/remote.py | 2 +- 6 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 homeassistant/components/http/www_static/polymer/components/recent-states.html create mode 100644 homeassistant/components/recorder.py diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index 96d24cb62cf..b1a518c18bd 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -314,6 +314,14 @@ class Event(object): self.data = data or {} self.origin = origin + def as_dict(self): + """ Returns a dict representation of this Event. """ + return { + 'event_type': self.event_type, + 'data': dict(self.data), + 'origin': str(self.origin) + } + def __repr__(self): # pylint: disable=maybe-no-member if self.data: @@ -355,7 +363,8 @@ class EventBus(object): event = Event(event_type, event_data, origin) - _LOGGER.info("Bus:Handling %s", event) + if event_type != EVENT_TIME_CHANGED: + _LOGGER.info("Bus:Handling %s", event) if not listeners: return diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 67030407f5f..5076b18ee30 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -89,6 +89,7 @@ from homeassistant.const import ( from homeassistant.helpers import TrackStates import homeassistant.remote as rem import homeassistant.util as util +import homeassistant.components.recorder as recorder from . import frontend DOMAIN = "http" @@ -235,6 +236,14 @@ class RequestHandler(SimpleHTTPRequestHandler): ('DELETE', URL_API_EVENT_FORWARD, '_handle_delete_api_event_forward'), + # Query the recorder + ('GET', + re.compile('/api/component/recorder/(?P[a-zA-Z\._0-9]+)/last_5_states'), + '_handle_component_recorder_5_states'), + ('GET', + re.compile('/api/component/recorder/last_5_events'), + '_handle_component_recorder_5_events'), + # Static files ('GET', re.compile(r'/static/(?P[a-zA-Z\._\-0-9/]+)'), '_handle_get_static'), @@ -495,6 +504,22 @@ class RequestHandler(SimpleHTTPRequestHandler): self._write_json(changed_states) + # pylint: disable=invalid-name + def _handle_component_recorder_5_states(self, path_match, data): + if recorder.DOMAIN not in self.server.hass.components: + return self._write_json([]) + + entity_id = path_match.group('entity_id') + + self._write_json(recorder.last_5_states(entity_id)) + + # pylint: disable=invalid-name + def _handle_component_recorder_5_events(self, path_match, data): + if recorder.DOMAIN not in self.server.hass.components: + return self._write_json([]) + + self._write_json(recorder.last_5_events()) + # pylint: disable=invalid-name def _handle_post_api_event_forward(self, path_match, data): """ Handles adding an event forwarding target. """ diff --git a/homeassistant/components/http/www_static/polymer/components/recent-states.html b/homeassistant/components/http/www_static/polymer/components/recent-states.html new file mode 100644 index 00000000000..60b6757b73b --- /dev/null +++ b/homeassistant/components/http/www_static/polymer/components/recent-states.html @@ -0,0 +1,31 @@ + + + + + + diff --git a/homeassistant/components/http/www_static/polymer/more-infos/more-info-default.html b/homeassistant/components/http/www_static/polymer/more-infos/more-info-default.html index e232beb666e..abc642a92e8 100644 --- a/homeassistant/components/http/www_static/polymer/more-infos/more-info-default.html +++ b/homeassistant/components/http/www_static/polymer/more-infos/more-info-default.html @@ -1,5 +1,7 @@ + +