1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-23 04:19:34 +00:00

Use new style for built-in ws commmands (#21694)

* Use new style for built-in ws commmands

* Lint
This commit is contained in:
Paulus Schoutsen
2019-03-05 19:31:26 -08:00
committed by GitHub
parent c9b173405b
commit fc1ee9be43
4 changed files with 55 additions and 82 deletions

View File

@@ -10,77 +10,25 @@ from homeassistant.helpers.service import async_get_all_descriptions
from . import const, decorators, messages
TYPE_CALL_SERVICE = 'call_service'
TYPE_EVENT = 'event'
TYPE_GET_CONFIG = 'get_config'
TYPE_GET_SERVICES = 'get_services'
TYPE_GET_STATES = 'get_states'
TYPE_PING = 'ping'
TYPE_PONG = 'pong'
TYPE_SUBSCRIBE_EVENTS = 'subscribe_events'
TYPE_UNSUBSCRIBE_EVENTS = 'unsubscribe_events'
@callback
def async_register_commands(hass):
"""Register commands."""
async_reg = hass.components.websocket_api.async_register_command
async_reg(TYPE_SUBSCRIBE_EVENTS, handle_subscribe_events,
SCHEMA_SUBSCRIBE_EVENTS)
async_reg(TYPE_UNSUBSCRIBE_EVENTS, handle_unsubscribe_events,
SCHEMA_UNSUBSCRIBE_EVENTS)
async_reg(TYPE_CALL_SERVICE, handle_call_service, SCHEMA_CALL_SERVICE)
async_reg(TYPE_GET_STATES, handle_get_states, SCHEMA_GET_STATES)
async_reg(TYPE_GET_SERVICES, handle_get_services, SCHEMA_GET_SERVICES)
async_reg(TYPE_GET_CONFIG, handle_get_config, SCHEMA_GET_CONFIG)
async_reg(TYPE_PING, handle_ping, SCHEMA_PING)
SCHEMA_SUBSCRIBE_EVENTS = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_SUBSCRIBE_EVENTS,
vol.Optional('event_type', default=MATCH_ALL): str,
})
SCHEMA_UNSUBSCRIBE_EVENTS = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_UNSUBSCRIBE_EVENTS,
vol.Required('subscription'): cv.positive_int,
})
SCHEMA_CALL_SERVICE = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_CALL_SERVICE,
vol.Required('domain'): str,
vol.Required('service'): str,
vol.Optional('service_data'): dict
})
SCHEMA_GET_STATES = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_GET_STATES,
})
SCHEMA_GET_SERVICES = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_GET_SERVICES,
})
SCHEMA_GET_CONFIG = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_GET_CONFIG,
})
SCHEMA_PING = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): TYPE_PING,
})
async_reg(handle_subscribe_events)
async_reg(handle_unsubscribe_events)
async_reg(handle_call_service)
async_reg(handle_get_states)
async_reg(handle_get_services)
async_reg(handle_get_config)
async_reg(handle_ping)
def event_message(iden, event):
"""Return an event message."""
return {
'id': iden,
'type': TYPE_EVENT,
'type': 'event',
'event': event.as_dict(),
}
@@ -89,11 +37,15 @@ def pong_message(iden):
"""Return a pong message."""
return {
'id': iden,
'type': TYPE_PONG,
'type': 'pong',
}
@callback
@decorators.websocket_command({
vol.Required('type'): 'subscribe_events',
vol.Optional('event_type', default=MATCH_ALL): str,
})
def handle_subscribe_events(hass, connection, msg):
"""Handle subscribe events command.
@@ -116,6 +68,10 @@ def handle_subscribe_events(hass, connection, msg):
@callback
@decorators.websocket_command({
vol.Required('type'): 'unsubscribe_events',
vol.Required('subscription'): cv.positive_int,
})
def handle_unsubscribe_events(hass, connection, msg):
"""Handle unsubscribe events command.
@@ -132,6 +88,12 @@ def handle_unsubscribe_events(hass, connection, msg):
@decorators.async_response
@decorators.websocket_command({
vol.Required('type'): 'call_service',
vol.Required('domain'): str,
vol.Required('service'): str,
vol.Optional('service_data'): dict
})
async def handle_call_service(hass, connection, msg):
"""Handle call service command.
@@ -161,6 +123,9 @@ async def handle_call_service(hass, connection, msg):
@callback
@decorators.websocket_command({
vol.Required('type'): 'get_states',
})
def handle_get_states(hass, connection, msg):
"""Handle get states command.
@@ -177,6 +142,9 @@ def handle_get_states(hass, connection, msg):
@decorators.async_response
@decorators.websocket_command({
vol.Required('type'): 'get_services',
})
async def handle_get_services(hass, connection, msg):
"""Handle get services command.
@@ -188,6 +156,9 @@ async def handle_get_services(hass, connection, msg):
@callback
@decorators.websocket_command({
vol.Required('type'): 'get_config',
})
def handle_get_config(hass, connection, msg):
"""Handle get config command.
@@ -198,6 +169,9 @@ def handle_get_config(hass, connection, msg):
@callback
@decorators.websocket_command({
vol.Required('type'): 'ping',
})
def handle_ping(hass, connection, msg):
"""Handle ping command.