Add subscription info endpoint (#16727)
* Add subscription info endpoint * Lint * Lint * Make decorator * Lint
This commit is contained in:
parent
874225dd67
commit
e58836f99f
6 changed files with 237 additions and 88 deletions
|
@ -480,6 +480,26 @@ class ActiveConnection:
|
|||
return wsock
|
||||
|
||||
|
||||
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_outside(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))
|
||||
|
||||
return schedule_handler
|
||||
|
||||
|
||||
@callback
|
||||
def handle_subscribe_events(hass, connection, msg):
|
||||
"""Handle subscribe events command.
|
||||
|
@ -515,24 +535,20 @@ def handle_unsubscribe_events(hass, connection, msg):
|
|||
msg['id'], ERR_NOT_FOUND, 'Subscription not found.'))
|
||||
|
||||
|
||||
@callback
|
||||
def handle_call_service(hass, connection, msg):
|
||||
@async_response
|
||||
async def handle_call_service(hass, connection, msg):
|
||||
"""Handle call service command.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
async def call_service_helper(msg):
|
||||
"""Call a service and fire complete message."""
|
||||
blocking = True
|
||||
if (msg['domain'] == 'homeassistant' and
|
||||
msg['service'] in ['restart', 'stop']):
|
||||
blocking = False
|
||||
await hass.services.async_call(
|
||||
msg['domain'], msg['service'], msg.get('service_data'), blocking,
|
||||
connection.context(msg))
|
||||
connection.send_message_outside(result_message(msg['id']))
|
||||
|
||||
hass.async_add_job(call_service_helper(msg))
|
||||
blocking = True
|
||||
if (msg['domain'] == 'homeassistant' and
|
||||
msg['service'] in ['restart', 'stop']):
|
||||
blocking = False
|
||||
await hass.services.async_call(
|
||||
msg['domain'], msg['service'], msg.get('service_data'), blocking,
|
||||
connection.context(msg))
|
||||
connection.send_message_outside(result_message(msg['id']))
|
||||
|
||||
|
||||
@callback
|
||||
|
@ -545,19 +561,15 @@ def handle_get_states(hass, connection, msg):
|
|||
msg['id'], hass.states.async_all()))
|
||||
|
||||
|
||||
@callback
|
||||
def handle_get_services(hass, connection, msg):
|
||||
@async_response
|
||||
async def handle_get_services(hass, connection, msg):
|
||||
"""Handle get services command.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
async def get_services_helper(msg):
|
||||
"""Get available services and fire complete message."""
|
||||
descriptions = await async_get_all_descriptions(hass)
|
||||
connection.send_message_outside(
|
||||
result_message(msg['id'], descriptions))
|
||||
|
||||
hass.async_add_job(get_services_helper(msg))
|
||||
descriptions = await async_get_all_descriptions(hass)
|
||||
connection.send_message_outside(
|
||||
result_message(msg['id'], descriptions))
|
||||
|
||||
|
||||
@callback
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue