Async response all the things (#17073)
* Use async_response * Update device_registry.py
This commit is contained in:
1 parent
15a160a630
commit
4210835dcd
4 files changed
+59
-78
No files matched your search
@@ -20,6 +20,7 @@ BASE_COMMAND_MESSAGE_SCHEMA = messages.BASE_COMMAND_MESSAGE_SCHEMA
|
||||
error_message = messages.error_message
|
||||
result_message = messages.result_message
|
||||
async_response = decorators.async_response
|
||||
require_owner = decorators.require_owner
|
||||
ws_require_user = decorators.ws_require_user
|
||||
# pylint: enable=invalid-name
|
||||
|
||||
|
||||
@@ -10,22 +10,24 @@ from . import messages
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _handle_async_response(func, hass, connection, msg):
|
||||
"""Create a response and handle exception."""
|
||||
try:
|
||||
await func(hass, connection, msg)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
connection.send_message(messages.error_message(
|
||||
msg['id'], 'unknown', 'Unexpected error occurred'))
|
||||
|
||||
|
||||
def async_response(func):
|
||||
"""Decorate an async function to handle WebSocket API messages."""
|
||||
async def handle_msg_response(hass, connection, msg):
|
||||
"""Create a response and handle exception."""
|
||||
try:
|
||||
await func(hass, connection, msg)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
connection.send_message(messages.error_message(
|
||||
msg['id'], 'unknown', 'Unexpected error occurred'))
|
||||
|
||||
@callback
|
||||
@wraps(func)
|
||||
def schedule_handler(hass, connection, msg):
|
||||
"""Schedule the handler."""
|
||||
hass.async_create_task(handle_msg_response(hass, connection, msg))
|
||||
hass.async_create_task(
|
||||
_handle_async_response(func, hass, connection, msg))
|
||||
|
||||
return schedule_handler
|
||||
|
||||
|
||||
Reference in new issue
Block a user