2018-10-01 09:21:00 +00:00
|
|
|
"""Commands part of Websocket API."""
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-18 02:13:06 +00:00
|
|
|
from homeassistant.auth.permissions.const import POLICY_READ
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import MATCH_ALL, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED
|
2018-10-30 15:38:09 +00:00
|
|
|
from homeassistant.core import callback, DOMAIN as HASS_DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.exceptions import Unauthorized, ServiceNotFound, HomeAssistantError
|
2018-10-01 09:21:00 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.service import async_get_all_descriptions
|
|
|
|
|
|
|
|
from . import const, decorators, messages
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-04-13 20:17:01 +00:00
|
|
|
def async_register_commands(hass, async_reg):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Register commands."""
|
2019-04-13 20:17:01 +00:00
|
|
|
async_reg(hass, handle_subscribe_events)
|
|
|
|
async_reg(hass, handle_unsubscribe_events)
|
|
|
|
async_reg(hass, handle_call_service)
|
|
|
|
async_reg(hass, handle_get_states)
|
|
|
|
async_reg(hass, handle_get_services)
|
|
|
|
async_reg(hass, handle_get_config)
|
|
|
|
async_reg(hass, handle_ping)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pong_message(iden):
|
|
|
|
"""Return a pong message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"id": iden, "type": "pong"}
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command(
|
|
|
|
{
|
|
|
|
vol.Required("type"): "subscribe_events",
|
|
|
|
vol.Optional("event_type", default=MATCH_ALL): str,
|
|
|
|
}
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
def handle_subscribe_events(hass, connection, msg):
|
|
|
|
"""Handle subscribe events command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2019-03-18 02:13:06 +00:00
|
|
|
from .permissions import SUBSCRIBE_WHITELIST
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event_type = msg["event_type"]
|
2019-03-18 02:13:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if event_type not in SUBSCRIBE_WHITELIST and not connection.user.is_admin:
|
2018-11-27 09:12:31 +00:00
|
|
|
raise Unauthorized
|
|
|
|
|
2019-03-18 02:13:06 +00:00
|
|
|
if event_type == EVENT_STATE_CHANGED:
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-18 02:13:06 +00:00
|
|
|
@callback
|
|
|
|
def forward_events(event):
|
|
|
|
"""Forward state changed events to websocket."""
|
|
|
|
if not connection.user.permissions.check_entity(
|
2019-07-31 19:25:30 +00:00
|
|
|
event.data["entity_id"], POLICY_READ
|
|
|
|
):
|
2019-03-18 02:13:06 +00:00
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.event_message(msg["id"], event))
|
2019-03-18 02:13:06 +00:00
|
|
|
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-18 02:13:06 +00:00
|
|
|
@callback
|
|
|
|
def forward_events(event):
|
|
|
|
"""Forward events to websocket."""
|
|
|
|
if event.event_type == EVENT_TIME_CHANGED:
|
|
|
|
return
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.event_message(msg["id"], event.as_dict()))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.subscriptions[msg["id"]] = hass.bus.async_listen(
|
|
|
|
event_type, forward_events
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.result_message(msg["id"]))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command(
|
|
|
|
{
|
|
|
|
vol.Required("type"): "unsubscribe_events",
|
|
|
|
vol.Required("subscription"): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
def handle_unsubscribe_events(hass, connection, msg):
|
|
|
|
"""Handle unsubscribe events command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
subscription = msg["subscription"]
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-03-11 03:07:09 +00:00
|
|
|
if subscription in connection.subscriptions:
|
|
|
|
connection.subscriptions.pop(subscription)()
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.result_message(msg["id"]))
|
2018-10-01 09:21:00 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(
|
|
|
|
messages.error_message(
|
|
|
|
msg["id"], const.ERR_NOT_FOUND, "Subscription not found."
|
|
|
|
)
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@decorators.async_response
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command(
|
|
|
|
{
|
|
|
|
vol.Required("type"): "call_service",
|
|
|
|
vol.Required("domain"): str,
|
|
|
|
vol.Required("service"): str,
|
|
|
|
vol.Optional("service_data"): dict,
|
|
|
|
}
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
async def handle_call_service(hass, connection, msg):
|
|
|
|
"""Handle call service command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
blocking = True
|
2019-07-31 19:25:30 +00:00
|
|
|
if msg["domain"] == HASS_DOMAIN and msg["service"] in ["restart", "stop"]:
|
2018-10-01 09:21:00 +00:00
|
|
|
blocking = False
|
2018-11-30 20:28:35 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
msg["domain"],
|
|
|
|
msg["service"],
|
|
|
|
msg.get("service_data"),
|
|
|
|
blocking,
|
|
|
|
connection.context(msg),
|
|
|
|
)
|
|
|
|
connection.send_message(messages.result_message(msg["id"]))
|
2019-05-14 05:09:11 +00:00
|
|
|
except ServiceNotFound as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
if err.domain == msg["domain"] and err.service == msg["service"]:
|
|
|
|
connection.send_message(
|
|
|
|
messages.error_message(
|
|
|
|
msg["id"], const.ERR_NOT_FOUND, "Service not found."
|
|
|
|
)
|
|
|
|
)
|
2019-05-14 05:09:11 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(
|
|
|
|
messages.error_message(
|
|
|
|
msg["id"], const.ERR_HOME_ASSISTANT_ERROR, str(err)
|
|
|
|
)
|
|
|
|
)
|
2019-03-02 07:09:31 +00:00
|
|
|
except HomeAssistantError as err:
|
2019-03-04 05:22:22 +00:00
|
|
|
connection.logger.exception(err)
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(
|
|
|
|
messages.error_message(msg["id"], const.ERR_HOME_ASSISTANT_ERROR, str(err))
|
|
|
|
)
|
2019-03-02 07:09:31 +00:00
|
|
|
except Exception as err: # pylint: disable=broad-except
|
2019-03-04 05:22:22 +00:00
|
|
|
connection.logger.exception(err)
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(
|
|
|
|
messages.error_message(msg["id"], const.ERR_UNKNOWN_ERROR, str(err))
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command({vol.Required("type"): "get_states"})
|
2018-10-01 09:21:00 +00:00
|
|
|
def handle_get_states(hass, connection, msg):
|
|
|
|
"""Handle get states command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
if connection.user.permissions.access_all_entities("read"):
|
2019-04-23 10:46:23 +00:00
|
|
|
states = hass.states.async_all()
|
|
|
|
else:
|
|
|
|
entity_perm = connection.user.permissions.check_entity
|
|
|
|
states = [
|
2019-07-31 19:25:30 +00:00
|
|
|
state
|
|
|
|
for state in hass.states.async_all()
|
|
|
|
if entity_perm(state.entity_id, "read")
|
2019-04-23 10:46:23 +00:00
|
|
|
]
|
2018-11-27 09:12:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.result_message(msg["id"], states))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@decorators.async_response
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command({vol.Required("type"): "get_services"})
|
2018-10-01 09:21:00 +00:00
|
|
|
async def handle_get_services(hass, connection, msg):
|
|
|
|
"""Handle get services command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
descriptions = await async_get_all_descriptions(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.result_message(msg["id"], descriptions))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command({vol.Required("type"): "get_config"})
|
2018-10-01 09:21:00 +00:00
|
|
|
def handle_get_config(hass, connection, msg):
|
|
|
|
"""Handle get config command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(messages.result_message(msg["id"], hass.config.as_dict()))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
@decorators.websocket_command({vol.Required("type"): "ping"})
|
2018-10-01 09:21:00 +00:00
|
|
|
def handle_ping(hass, connection, msg):
|
|
|
|
"""Handle ping command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_message(pong_message(msg["id"]))
|