Skip to content

Commit a1b04ef

Browse files
craigbarrattraman325
authored andcommitted
fix args to service trigger functions; added context tests
1 parent f113a89 commit a1b04ef

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

custom_components/pyscript/eval.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,12 @@ async def pyscript_service_handler(call):
357357
Function.install_ast_funcs(ast_ctx)
358358
func_args = {
359359
"trigger_type": "service",
360+
"context": call.context,
360361
}
361362
func_args.update(call.data)
362363

363364
async def do_service_call(func, ast_ctx, data):
364-
await func.call(ast_ctx, **call.data)
365+
await func.call(ast_ctx, **data)
365366
if ast_ctx.get_exception_obj():
366367
ast_ctx.get_logger().error(ast_ctx.get_exception_long())
367368

custom_components/pyscript/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import logging
44

5-
from homeassistant.helpers.restore_state import RestoreStateData
65
from homeassistant.core import Context
6+
from homeassistant.helpers.restore_state import RestoreStateData
77

88
from .const import LOGGER_PATH
99
from .function import Function

tests/test_init.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from homeassistant import loader
1616
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_STATE_CHANGED
17+
from homeassistant.core import Context
1718
from homeassistant.helpers.service import async_get_all_descriptions
1819
from homeassistant.setup import async_setup_component
1920

@@ -259,11 +260,11 @@ async def test_service_run(hass, caplog):
259260
"""
260261
261262
@service
262-
def func1(arg1=1, arg2=2):
263+
def func1(arg1=1, arg2=2, context=None):
263264
x = 1
264265
x = 2 * x + 3
265266
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)]
267268
268269
@service
269270
def func2(**kwargs):
@@ -272,6 +273,7 @@ def func2(**kwargs):
272273
log.info(f"this is func1 x = {x}, kwargs = {kwargs}")
273274
has2 = service.has_service("pyscript", "func2")
274275
has3 = service.has_service("pyscript", "func3")
276+
del kwargs["context"]
275277
pyscript.done = [x, kwargs, has2, has3]
276278
277279
@service
@@ -284,34 +286,42 @@ def call_service(domain=None, name=None, **kwargs):
284286
285287
""",
286288
)
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)
288291
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)]
290293
assert "this is func1 x = 5" in caplog.text
291294

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+
292299
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,
294304
)
295305
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)]
297307

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)
299309
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)]
301311

302312
await hass.services.async_call("pyscript", "call_service", {"domain": "pyscript", "name": "func2"})
303313
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]
305315

306316
await hass.services.async_call(
307317
"pyscript", "call_service", {"domain": "pyscript", "name": "func2", "arg1": "string1"},
308318
)
309319
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]
311321

312322
await hass.services.async_call("pyscript", "func2", {"arg1": "string1", "arg2": 123})
313323
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]
315325

316326

317327
async def test_reload(hass, caplog):

0 commit comments

Comments
 (0)