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:
Paulus Schoutsen authored and GitHub committed 2016-10-23 23:48:01 -07:00
1 parent 9aa88819a5
commit 519d9f2fd0
45 files changed
+1422 -1009

No files matched your search

+7 -4
View File
@@ -12,6 +12,7 @@ import time
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
@@ -273,8 +274,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
scope=['activity', 'heartrate', 'nutrition', 'profile',
'settings', 'sleep', 'weight'])
hass.wsgi.register_redirect(FITBIT_AUTH_START, fitbit_auth_start_url)
hass.wsgi.register_view(FitbitAuthCallbackView(
hass.http.register_redirect(FITBIT_AUTH_START, fitbit_auth_start_url)
hass.http.register_view(FitbitAuthCallbackView(
hass, config, add_devices, oauth))
request_oauth_completion(hass)
@@ -294,12 +295,13 @@ class FitbitAuthCallbackView(HomeAssistantView):
self.add_devices = add_devices
self.oauth = oauth
@callback
def get(self, request):
"""Finish OAuth callback request."""
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError
from oauthlib.oauth2.rfc6749.errors import MissingTokenError
data = request.args
data = request.GET
response_message = """Fitbit has been successfully authorized!
You can close this window now!"""
@@ -340,7 +342,8 @@ class FitbitAuthCallbackView(HomeAssistantView):
config_contents):
_LOGGER.error("Failed to save config file")
setup_platform(self.hass, self.config, self.add_devices)
self.hass.async_add_job(setup_platform, self.hass, self.config,
self.add_devices)
return html_response
+9 -6
View File
@@ -9,6 +9,7 @@ import re
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_EMAIL, CONF_NAME)
@@ -57,7 +58,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
email = config.get(CONF_EMAIL)
sensors = {}
hass.wsgi.register_view(TorqueReceiveDataView(
hass.http.register_view(TorqueReceiveDataView(
hass, email, vehicle, sensors, add_devices))
return True
@@ -77,9 +78,10 @@ class TorqueReceiveDataView(HomeAssistantView):
self.sensors = sensors
self.add_devices = add_devices
@callback
def get(self, request):
"""Handle Torque data request."""
data = request.args
data = request.GET
if self.email is not None and self.email != data[SENSOR_EMAIL_FIELD]:
return
@@ -100,14 +102,14 @@ class TorqueReceiveDataView(HomeAssistantView):
elif is_value:
pid = convert_pid(is_value.group(1))
if pid in self.sensors:
self.sensors[pid].on_update(data[key])
self.sensors[pid].async_on_update(data[key])
for pid in names:
if pid not in self.sensors:
self.sensors[pid] = TorqueSensor(
ENTITY_NAME_FORMAT.format(self.vehicle, names[pid]),
units.get(pid, None))
self.add_devices([self.sensors[pid]])
self.hass.async_add_job(self.add_devices, [self.sensors[pid]])
return None
@@ -141,7 +143,8 @@ class TorqueSensor(Entity):
"""Return the default icon of the sensor."""
return 'mdi:car'
def on_update(self, value):
@callback
def async_on_update(self, value):
"""Receive an update."""
self._state = value
self.update_ha_state()
self.hass.loop.create_task(self.async_update_ha_state())