Async response all the things (#17073)

* Use async_response

* Update device_registry.py
This commit is contained in:
Paulus Schoutsen authored and Pascal Vizeli committed 2018-10-03 07:53:54 +02:00
1 parent 15a160a630
commit 4210835dcd
4 files changed
+59 -78

No files matched your search

+30 -44
View File
@@ -1,9 +1,7 @@
"""Offer API to configure Home Assistant auth."""
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components import websocket_api
from homeassistant.components.websocket_api.decorators import require_owner
WS_TYPE_LIST = 'config/auth/list'
@@ -41,61 +39,49 @@ async def async_setup(hass):
return True
@callback
@require_owner
def websocket_list(hass, connection, msg):
@websocket_api.require_owner
@websocket_api.async_response
async def websocket_list(hass, connection, msg):
"""Return a list of users."""
async def send_users():
"""Send users."""
result = [_user_info(u) for u in await hass.auth.async_get_users()]
result = [_user_info(u) for u in await hass.auth.async_get_users()]
connection.send_message(
websocket_api.result_message(msg['id'], result))
hass.async_create_task(send_users())
connection.send_message(
websocket_api.result_message(msg['id'], result))
@callback
@require_owner
def websocket_delete(hass, connection, msg):
@websocket_api.require_owner
@websocket_api.async_response
async def websocket_delete(hass, connection, msg):
"""Delete a user."""
async def delete_user():
"""Delete user."""
if msg['user_id'] == connection.user.id:
connection.send_message(websocket_api.error_message(
msg['id'], 'no_delete_self',
'Unable to delete your own account'))
return
if msg['user_id'] == connection.user.id:
connection.send_message(websocket_api.error_message(
msg['id'], 'no_delete_self',
'Unable to delete your own account'))
return
user = await hass.auth.async_get_user(msg['user_id'])
user = await hass.auth.async_get_user(msg['user_id'])
if not user:
connection.send_message(websocket_api.error_message(
msg['id'], 'not_found', 'User not found'))
return
if not user:
connection.send_message(websocket_api.error_message(
msg['id'], 'not_found', 'User not found'))
return
await hass.auth.async_remove_user(user)
await hass.auth.async_remove_user(user)
connection.send_message(
websocket_api.result_message(msg['id']))
hass.async_create_task(delete_user())
connection.send_message(
websocket_api.result_message(msg['id']))
@callback
@require_owner
def websocket_create(hass, connection, msg):
@websocket_api.require_owner
@websocket_api.async_response
async def websocket_create(hass, connection, msg):
"""Create a user."""
async def create_user():
"""Create a user."""
user = await hass.auth.async_create_user(msg['name'])
user = await hass.auth.async_create_user(msg['name'])
connection.send_message(
websocket_api.result_message(msg['id'], {
'user': _user_info(user)
}))
hass.async_create_task(create_user())
connection.send_message(
websocket_api.result_message(msg['id'], {
'user': _user_info(user)
}))
def _user_info(user):