Add raw service data to event (#19163)

pull/19215/head
Paulus Schoutsen 2018-12-10 12:58:51 +01:00 committed by Paulus Schoutsen
parent e3b1008511
commit 2da5a02285
2 changed files with 30 additions and 3 deletions

View File

@ -1098,9 +1098,11 @@ class ServiceRegistry:
raise ServiceNotFound(domain, service) from None
if handler.schema:
service_data = handler.schema(service_data)
processed_data = handler.schema(service_data)
else:
processed_data = service_data
service_call = ServiceCall(domain, service, service_data, context)
service_call = ServiceCall(domain, service, processed_data, context)
self._hass.bus.async_fire(EVENT_CALL_SERVICE, {
ATTR_DOMAIN: domain.lower(),

View File

@ -8,6 +8,7 @@ from unittest.mock import patch, MagicMock
from datetime import datetime, timedelta
from tempfile import TemporaryDirectory
import voluptuous as vol
import pytz
import pytest
@ -21,7 +22,7 @@ from homeassistant.const import (
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
ATTR_NOW, EVENT_TIME_CHANGED, EVENT_TIMER_OUT_OF_SYNC, ATTR_SECONDS,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_CLOSE,
EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED)
EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, EVENT_CALL_SERVICE)
from tests.common import get_test_home_assistant, async_mock_service
@ -1000,3 +1001,27 @@ async def test_service_executed_with_subservices(hass):
assert len(calls) == 4
assert [call.service for call in calls] == [
'outer', 'inner', 'inner', 'outer']
async def test_service_call_event_contains_original_data(hass):
"""Test that service call event contains original data."""
events = []
@ha.callback
def callback(event):
events.append(event)
hass.bus.async_listen(EVENT_CALL_SERVICE, callback)
calls = async_mock_service(hass, 'test', 'service', vol.Schema({
'number': vol.Coerce(int)
}))
await hass.services.async_call('test', 'service', {
'number': '23'
}, blocking=True)
await hass.async_block_till_done()
assert len(events) == 1
assert events[0].data['service_data']['number'] == '23'
assert len(calls) == 1
assert calls[0].data['number'] == 23