Allow non-admins to listen to certain events (#22137)

This commit is contained in:
Paulus Schoutsen
2019-03-17 19:13:06 -07:00
committed by GitHub
parent c020b7c47d
commit 29131a655d
3 changed files with 125 additions and 10 deletions

View File

@@ -1,7 +1,9 @@
"""Commands part of Websocket API."""
import voluptuous as vol
from homeassistant.const import MATCH_ALL, EVENT_TIME_CHANGED
from homeassistant.auth.permissions.const import POLICY_READ
from homeassistant.const import (
MATCH_ALL, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED)
from homeassistant.core import callback, DOMAIN as HASS_DOMAIN
from homeassistant.exceptions import Unauthorized, ServiceNotFound, \
HomeAssistantError
@@ -42,20 +44,37 @@ def handle_subscribe_events(hass, connection, msg):
Async friendly.
"""
if not connection.user.is_admin:
from .permissions import SUBSCRIBE_WHITELIST
event_type = msg['event_type']
if (event_type not in SUBSCRIBE_WHITELIST and
not connection.user.is_admin):
raise Unauthorized
async def forward_events(event):
"""Forward events to websocket."""
if event.event_type == EVENT_TIME_CHANGED:
return
if event_type == EVENT_STATE_CHANGED:
@callback
def forward_events(event):
"""Forward state changed events to websocket."""
if not connection.user.permissions.check_entity(
event.data['entity_id'], POLICY_READ):
return
connection.send_message(messages.event_message(
msg['id'], event.as_dict()
))
connection.send_message(messages.event_message(msg['id'], event))
else:
@callback
def forward_events(event):
"""Forward events to websocket."""
if event.event_type == EVENT_TIME_CHANGED:
return
connection.send_message(messages.event_message(
msg['id'], event.as_dict()
))
connection.subscriptions[msg['id']] = hass.bus.async_listen(
msg['event_type'], forward_events)
event_type, forward_events)
connection.send_message(messages.result_message(msg['id']))