Move HomeAssistantView to separate file. Convert http to async syntax. [skip ci] (#12982)
* Move HomeAssistantView to separate file. Convert http to async syntax. * pylint * websocket api * update emulated_hue for async/await * Lint
This commit is contained in:
parent
2ee73ca911
commit
321eb2ec6f
17 changed files with 292 additions and 344 deletions
|
@ -191,8 +191,7 @@ def result_message(iden, result=None):
|
|||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
async def async_setup(hass, config):
|
||||
"""Initialize the websocket API."""
|
||||
hass.http.register_view(WebsocketAPIView)
|
||||
return True
|
||||
|
@ -205,11 +204,10 @@ class WebsocketAPIView(HomeAssistantView):
|
|||
url = URL
|
||||
requires_auth = False
|
||||
|
||||
@asyncio.coroutine
|
||||
def get(self, request):
|
||||
async def get(self, request):
|
||||
"""Handle an incoming websocket connection."""
|
||||
# pylint: disable=no-self-use
|
||||
return ActiveConnection(request.app['hass'], request).handle()
|
||||
return await ActiveConnection(request.app['hass'], request).handle()
|
||||
|
||||
|
||||
class ActiveConnection:
|
||||
|
@ -233,17 +231,16 @@ class ActiveConnection:
|
|||
"""Print an error message."""
|
||||
_LOGGER.error("WS %s: %s %s", id(self.wsock), message1, message2)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _writer(self):
|
||||
async def _writer(self):
|
||||
"""Write outgoing messages."""
|
||||
# Exceptions if Socket disconnected or cancelled by connection handler
|
||||
with suppress(RuntimeError, *CANCELLATION_ERRORS):
|
||||
while not self.wsock.closed:
|
||||
message = yield from self.to_write.get()
|
||||
message = await self.to_write.get()
|
||||
if message is None:
|
||||
break
|
||||
self.debug("Sending", message)
|
||||
yield from self.wsock.send_json(message, dumps=JSON_DUMP)
|
||||
await self.wsock.send_json(message, dumps=JSON_DUMP)
|
||||
|
||||
@callback
|
||||
def send_message_outside(self, message):
|
||||
|
@ -266,12 +263,11 @@ class ActiveConnection:
|
|||
self._handle_task.cancel()
|
||||
self._writer_task.cancel()
|
||||
|
||||
@asyncio.coroutine
|
||||
def handle(self):
|
||||
async def handle(self):
|
||||
"""Handle the websocket connection."""
|
||||
request = self.request
|
||||
wsock = self.wsock = web.WebSocketResponse(heartbeat=55)
|
||||
yield from wsock.prepare(request)
|
||||
await wsock.prepare(request)
|
||||
self.debug("Connected")
|
||||
|
||||
# Get a reference to current task so we can cancel our connection
|
||||
|
@ -294,8 +290,8 @@ class ActiveConnection:
|
|||
authenticated = True
|
||||
|
||||
else:
|
||||
yield from self.wsock.send_json(auth_required_message())
|
||||
msg = yield from wsock.receive_json()
|
||||
await self.wsock.send_json(auth_required_message())
|
||||
msg = await wsock.receive_json()
|
||||
msg = AUTH_MESSAGE_SCHEMA(msg)
|
||||
|
||||
if validate_password(request, msg['api_password']):
|
||||
|
@ -303,18 +299,18 @@ class ActiveConnection:
|
|||
|
||||
else:
|
||||
self.debug("Invalid password")
|
||||
yield from self.wsock.send_json(
|
||||
await self.wsock.send_json(
|
||||
auth_invalid_message('Invalid password'))
|
||||
|
||||
if not authenticated:
|
||||
yield from process_wrong_login(request)
|
||||
await process_wrong_login(request)
|
||||
return wsock
|
||||
|
||||
yield from self.wsock.send_json(auth_ok_message())
|
||||
await self.wsock.send_json(auth_ok_message())
|
||||
|
||||
# ---------- AUTH PHASE OVER ----------
|
||||
|
||||
msg = yield from wsock.receive_json()
|
||||
msg = await wsock.receive_json()
|
||||
last_id = 0
|
||||
|
||||
while msg:
|
||||
|
@ -332,7 +328,7 @@ class ActiveConnection:
|
|||
getattr(self, handler_name)(msg)
|
||||
|
||||
last_id = cur_id
|
||||
msg = yield from wsock.receive_json()
|
||||
msg = await wsock.receive_json()
|
||||
|
||||
except vol.Invalid as err:
|
||||
error_msg = "Message incorrectly formatted: "
|
||||
|
@ -394,11 +390,11 @@ class ActiveConnection:
|
|||
self.to_write.put_nowait(final_message)
|
||||
self.to_write.put_nowait(None)
|
||||
# Make sure all error messages are written before closing
|
||||
yield from self._writer_task
|
||||
await self._writer_task
|
||||
except asyncio.QueueFull:
|
||||
self._writer_task.cancel()
|
||||
|
||||
yield from wsock.close()
|
||||
await wsock.close()
|
||||
self.debug("Closed connection")
|
||||
|
||||
return wsock
|
||||
|
@ -410,8 +406,7 @@ class ActiveConnection:
|
|||
"""
|
||||
msg = SUBSCRIBE_EVENTS_MESSAGE_SCHEMA(msg)
|
||||
|
||||
@asyncio.coroutine
|
||||
def forward_events(event):
|
||||
async def forward_events(event):
|
||||
"""Forward events to websocket."""
|
||||
if event.event_type == EVENT_TIME_CHANGED:
|
||||
return
|
||||
|
@ -447,10 +442,9 @@ class ActiveConnection:
|
|||
"""
|
||||
msg = CALL_SERVICE_MESSAGE_SCHEMA(msg)
|
||||
|
||||
@asyncio.coroutine
|
||||
def call_service_helper(msg):
|
||||
async def call_service_helper(msg):
|
||||
"""Call a service and fire complete message."""
|
||||
yield from self.hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
msg['domain'], msg['service'], msg.get('service_data'), True)
|
||||
self.send_message_outside(result_message(msg['id']))
|
||||
|
||||
|
@ -473,10 +467,9 @@ class ActiveConnection:
|
|||
"""
|
||||
msg = GET_SERVICES_MESSAGE_SCHEMA(msg)
|
||||
|
||||
@asyncio.coroutine
|
||||
def get_services_helper(msg):
|
||||
async def get_services_helper(msg):
|
||||
"""Get available services and fire complete message."""
|
||||
descriptions = yield from async_get_all_descriptions(self.hass)
|
||||
descriptions = await async_get_all_descriptions(self.hass)
|
||||
self.send_message_outside(result_message(msg['id'], descriptions))
|
||||
|
||||
self.hass.async_add_job(get_services_helper(msg))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue