async HTTP component (#3914)
* Migrate WSGI to asyncio * Rename wsgi -> http * Python 3.4 compat * Move linting to Python 3.4 * lint * Lint * Fix Python 3.4 mock_open + binary data * Surpress logging aiohttp.access * Spelling * Sending files is a coroutine * More callback annotations and naming fixes * Fix ios
This commit is contained in:
1 parent
9aa88819a5
commit
519d9f2fd0
45 files changed
+1422
-1009
No files matched your search
@@ -11,6 +11,7 @@ from itertools import groupby
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components import recorder, sun
|
||||
@@ -19,7 +20,7 @@ from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.const import (EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED,
|
||||
STATE_NOT_HOME, STATE_OFF, STATE_ON,
|
||||
ATTR_HIDDEN)
|
||||
ATTR_HIDDEN, HTTP_BAD_REQUEST)
|
||||
from homeassistant.core import State, split_entity_id, DOMAIN as HA_DOMAIN
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
|
||||
@@ -88,7 +89,7 @@ def async_log_entry(hass, name, message, domain=None, entity_id=None):
|
||||
|
||||
def setup(hass, config):
|
||||
"""Listen for download events to download files."""
|
||||
@asyncio.coroutine
|
||||
@callback
|
||||
def log_message(service):
|
||||
"""Handle sending notification message service calls."""
|
||||
message = service.data[ATTR_MESSAGE]
|
||||
@@ -100,7 +101,7 @@ def setup(hass, config):
|
||||
message = message.async_render()
|
||||
async_log_entry(hass, name, message, domain, entity_id)
|
||||
|
||||
hass.wsgi.register_view(LogbookView(hass, config))
|
||||
hass.http.register_view(LogbookView(hass, config))
|
||||
|
||||
register_built_in_panel(hass, 'logbook', 'Logbook',
|
||||
'mdi:format-list-bulleted-type')
|
||||
@@ -115,24 +116,37 @@ class LogbookView(HomeAssistantView):
|
||||
|
||||
url = '/api/logbook'
|
||||
name = 'api:logbook'
|
||||
extra_urls = ['/api/logbook/<datetime:datetime>']
|
||||
extra_urls = ['/api/logbook/{datetime}']
|
||||
|
||||
def __init__(self, hass, config):
|
||||
"""Initilalize the logbook view."""
|
||||
super().__init__(hass)
|
||||
self.config = config
|
||||
|
||||
@asyncio.coroutine
|
||||
def get(self, request, datetime=None):
|
||||
"""Retrieve logbook entries."""
|
||||
start_day = dt_util.as_utc(datetime or dt_util.start_of_local_day())
|
||||
if datetime:
|
||||
datetime = dt_util.parse_datetime(datetime)
|
||||
|
||||
if datetime is None:
|
||||
return self.json_message('Invalid datetime', HTTP_BAD_REQUEST)
|
||||
else:
|
||||
datetime = dt_util.start_of_local_day()
|
||||
|
||||
start_day = dt_util.as_utc(datetime)
|
||||
end_day = start_day + timedelta(days=1)
|
||||
|
||||
events = recorder.get_model('Events')
|
||||
query = recorder.query('Events').filter(
|
||||
(events.time_fired > start_day) &
|
||||
(events.time_fired < end_day))
|
||||
events = recorder.execute(query)
|
||||
events = _exclude_events(events, self.config)
|
||||
def get_results():
|
||||
"""Query DB for results."""
|
||||
events = recorder.get_model('Events')
|
||||
query = recorder.query('Events').filter(
|
||||
(events.time_fired > start_day) &
|
||||
(events.time_fired < end_day))
|
||||
events = recorder.execute(query)
|
||||
return _exclude_events(events, self.config)
|
||||
|
||||
events = yield from self.hass.loop.run_in_executor(None, get_results)
|
||||
|
||||
return self.json(humanify(events))
|
||||
|
||||
|
||||
Reference in new issue
Block a user