14
14
15
15
from homeassistant import loader
16
16
from homeassistant .const import EVENT_HOMEASSISTANT_STARTED , EVENT_STATE_CHANGED
17
+ from homeassistant .core import Context
17
18
from homeassistant .helpers .service import async_get_all_descriptions
18
19
from homeassistant .setup import async_setup_component
19
20
@@ -259,11 +260,11 @@ async def test_service_run(hass, caplog):
259
260
"""
260
261
261
262
@service
262
- def func1(arg1=1, arg2=2):
263
+ def func1(arg1=1, arg2=2, context=None ):
263
264
x = 1
264
265
x = 2 * x + 3
265
266
log.info(f"this is func1 x = {x}, arg1 = {arg1}, arg2 = {arg2}")
266
- pyscript.done = [x, arg1, arg2]
267
+ pyscript.done = [x, arg1, arg2, str(context) ]
267
268
268
269
@service
269
270
def func2(**kwargs):
@@ -272,6 +273,7 @@ def func2(**kwargs):
272
273
log.info(f"this is func1 x = {x}, kwargs = {kwargs}")
273
274
has2 = service.has_service("pyscript", "func2")
274
275
has3 = service.has_service("pyscript", "func3")
276
+ del kwargs["context"]
275
277
pyscript.done = [x, kwargs, has2, has3]
276
278
277
279
@service
@@ -284,34 +286,42 @@ def call_service(domain=None, name=None, **kwargs):
284
286
285
287
""" ,
286
288
)
287
- await hass .services .async_call ("pyscript" , "func1" , {})
289
+ context = Context (user_id = "1234" , parent_id = "5678" , id = "8901" )
290
+ await hass .services .async_call ("pyscript" , "func1" , {}, context = context )
288
291
ret = await wait_until_done (notify_q )
289
- assert literal_eval (ret ) == [5 , 1 , 2 ]
292
+ assert literal_eval (ret ) == [5 , 1 , 2 , str ( context ) ]
290
293
assert "this is func1 x = 5" in caplog .text
291
294
295
+ await hass .services .async_call ("pyscript" , "func1" , {"arg1" : 10 }, context = context )
296
+ ret = await wait_until_done (notify_q )
297
+ assert literal_eval (ret ) == [5 , 10 , 2 , str (context )]
298
+
292
299
await hass .services .async_call (
293
- "pyscript" , "call_service" , {"domain" : "pyscript" , "name" : "func1" , "arg1" : "string1" },
300
+ "pyscript" ,
301
+ "call_service" ,
302
+ {"domain" : "pyscript" , "name" : "func1" , "arg1" : "string1" },
303
+ context = context ,
294
304
)
295
305
ret = await wait_until_done (notify_q )
296
- assert literal_eval (ret ) == [5 , "string1" , 2 ]
306
+ assert literal_eval (ret ) == [5 , "string1" , 2 , str ( context ) ]
297
307
298
- await hass .services .async_call ("pyscript" , "func1" , {"arg1" : "string1" , "arg2" : 123 })
308
+ await hass .services .async_call ("pyscript" , "func1" , {"arg1" : "string1" , "arg2" : 123 }, context = context )
299
309
ret = await wait_until_done (notify_q )
300
- assert literal_eval (ret ) == [5 , "string1" , 123 ]
310
+ assert literal_eval (ret ) == [5 , "string1" , 123 , str ( context ) ]
301
311
302
312
await hass .services .async_call ("pyscript" , "call_service" , {"domain" : "pyscript" , "name" : "func2" })
303
313
ret = await wait_until_done (notify_q )
304
- assert literal_eval (ret ) == [5 , {}, 1 , 0 ]
314
+ assert literal_eval (ret ) == [5 , {"trigger_type" : "service" }, 1 , 0 ]
305
315
306
316
await hass .services .async_call (
307
317
"pyscript" , "call_service" , {"domain" : "pyscript" , "name" : "func2" , "arg1" : "string1" },
308
318
)
309
319
ret = await wait_until_done (notify_q )
310
- assert literal_eval (ret ) == [5 , {"arg1" : "string1" }, 1 , 0 ]
320
+ assert literal_eval (ret ) == [5 , {"trigger_type" : "service" , " arg1" : "string1" }, 1 , 0 ]
311
321
312
322
await hass .services .async_call ("pyscript" , "func2" , {"arg1" : "string1" , "arg2" : 123 })
313
323
ret = await wait_until_done (notify_q )
314
- assert literal_eval (ret ) == [5 , {"arg1" : "string1" , "arg2" : 123 }, 1 , 0 ]
324
+ assert literal_eval (ret ) == [5 , {"trigger_type" : "service" , " arg1" : "string1" , "arg2" : 123 }, 1 , 0 ]
315
325
316
326
317
327
async def test_reload (hass , caplog ):
0 commit comments