* Add context

* Add context to switch/light services

* Test set_state API

* Lint

* Fix tests

* Do not include context yet in comparison

* Do not pass in loop

* Fix Z-Wave tests

* Add websocket test without user
This commit is contained in:
Paulus Schoutsen authored and Jason Hu committed 2018-07-28 17:53:37 -07:00
1 parent 867f80715e
commit c7f4bdafc0
16 files changed
+363 -109

No files matched your search

+6 -3
View File
@@ -220,7 +220,8 @@ class APIEntityStateView(HomeAssistantView):
is_new_state = hass.states.get(entity_id) is None
# Write state
hass.states.async_set(entity_id, new_state, attributes, force_update)
hass.states.async_set(entity_id, new_state, attributes, force_update,
self.context(request))
# Read the state back for our response
status_code = HTTP_CREATED if is_new_state else 200
@@ -279,7 +280,8 @@ class APIEventView(HomeAssistantView):
event_data[key] = state
request.app['hass'].bus.async_fire(
event_type, event_data, ha.EventOrigin.remote)
event_type, event_data, ha.EventOrigin.remote,
self.context(request))
return self.json_message("Event {} fired.".format(event_type))
@@ -316,7 +318,8 @@ class APIDomainServicesView(HomeAssistantView):
"Data should be valid JSON.", HTTP_BAD_REQUEST)
with AsyncTrackStates(hass) as changed_states:
await hass.services.async_call(domain, service, data, True)
await hass.services.async_call(
domain, service, data, True, self.context(request))
return self.json(changed_states)
+9 -1
View File
@@ -13,7 +13,7 @@ from aiohttp.web_exceptions import HTTPUnauthorized, HTTPInternalServerError
import homeassistant.remote as rem
from homeassistant.components.http.ban import process_success_login
from homeassistant.core import is_callback
from homeassistant.core import Context, is_callback
from homeassistant.const import CONTENT_TYPE_JSON
from .const import KEY_AUTHENTICATED, KEY_REAL_IP
@@ -32,6 +32,14 @@ class HomeAssistantView:
cors_allowed = False
# pylint: disable=no-self-use
def context(self, request):
"""Generate a context from a request."""
user = request.get('hass_user')
if user is None:
return Context()
return Context(user_id=user.id)
def json(self, result, status_code=200, headers=None):
"""Return a JSON response."""
try:
+3 -1
View File
@@ -359,7 +359,9 @@ async def async_setup(hass, config):
if not light.should_poll:
continue
update_tasks.append(light.async_update_ha_state(True))
update_tasks.append(
light.async_update_ha_state(True, service.context))
if update_tasks:
await asyncio.wait(update_tasks, loop=hass.loop)
+2 -1
View File
@@ -114,7 +114,8 @@ async def async_setup(hass, config):
if not switch.should_poll:
continue
update_tasks.append(switch.async_update_ha_state(True))
update_tasks.append(
switch.async_update_ha_state(True, service.context))
if update_tasks:
await asyncio.wait(update_tasks, loop=hass.loop)
+16 -3
View File
@@ -18,7 +18,7 @@ from voluptuous.humanize import humanize_error
from homeassistant.const import (
MATCH_ALL, EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP,
__version__)
from homeassistant.core import callback
from homeassistant.core import Context, callback
from homeassistant.loader import bind_hass
from homeassistant.remote import JSONEncoder
from homeassistant.helpers import config_validation as cv
@@ -262,6 +262,18 @@ class ActiveConnection:
self._handle_task = None
self._writer_task = None
@property
def user(self):
"""Return the user associated with the connection."""
return self.request.get('hass_user')
def context(self, msg):
"""Return a context."""
user = self.user
if user is None:
return Context()
return Context(user_id=user.id)
def debug(self, message1, message2=''):
"""Print a debug message."""
_LOGGER.debug("WS %s: %s %s", id(self.wsock), message1, message2)
@@ -287,7 +299,7 @@ class ActiveConnection:
@callback
def send_message_outside(self, message):
"""Send a message to the client outside of the main task.
"""Send a message to the client.
Closes connection if the client is not reading the messages.
@@ -508,7 +520,8 @@ def handle_call_service(hass, connection, msg):
async def call_service_helper(msg):
"""Call a service and fire complete message."""
await hass.services.async_call(
msg['domain'], msg['service'], msg.get('service_data'), True)
msg['domain'], msg['service'], msg.get('service_data'), True,
connection.context(msg))
connection.send_message_outside(result_message(msg['id']))
hass.async_add_job(call_service_helper(msg))