From d2fea76fd7e3466bd80498acd3669604ede31387 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 18 Feb 2019 13:07:44 -0800 Subject: [PATCH] Add context to service call event (#21181) --- homeassistant/core.py | 2 +- tests/test_core.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 5cd23e9f9a2..c181ad453f3 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1117,7 +1117,7 @@ class ServiceRegistry: ATTR_DOMAIN: domain.lower(), ATTR_SERVICE: service.lower(), ATTR_SERVICE_DATA: service_data, - }) + }, context=context) if not blocking: self._hass.async_create_task( diff --git a/tests/test_core.py b/tests/test_core.py index 3cb5b87b4bb..818c6e4c087 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1013,6 +1013,7 @@ def test_track_task_functions(loop): async def test_service_executed_with_subservices(hass): """Test we block correctly till all services done.""" calls = async_mock_service(hass, 'test', 'inner') + context = ha.Context() async def handle_outer(call): """Handle outer service call.""" @@ -1026,11 +1027,13 @@ async def test_service_executed_with_subservices(hass): hass.services.async_register('test', 'outer', handle_outer) - await hass.services.async_call('test', 'outer', blocking=True) + await hass.services.async_call('test', 'outer', blocking=True, + context=context) assert len(calls) == 4 assert [call.service for call in calls] == [ 'outer', 'inner', 'inner', 'outer'] + assert all(call.context is context for call in calls) async def test_service_call_event_contains_original_data(hass): @@ -1047,11 +1050,14 @@ async def test_service_call_event_contains_original_data(hass): 'number': vol.Coerce(int) })) + context = ha.Context() await hass.services.async_call('test', 'service', { 'number': '23' - }, blocking=True) + }, blocking=True, context=context) await hass.async_block_till_done() assert len(events) == 1 assert events[0].data['service_data']['number'] == '23' + assert events[0].context is context assert len(calls) == 1 assert calls[0].data['number'] == 23 + assert calls[0].context is context