Skip to content

Commit

Permalink
added some service response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarratt committed Jul 30, 2023
1 parent 88eebeb commit 83aba1b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 6 additions & 2 deletions tests/test_apps_modules.py
Expand Up @@ -29,9 +29,10 @@ async def test_service_exists(hass, caplog):
import xyz2
from xyz2 import f_minus
@service
@service(supports_response = "optional")
def func1():
pyscript.done = [xyz2.f_add(1, 2), xyz2.f_mult(3, 4), xyz2.f_add(10, 20), f_minus(50, 20)]
return {"a": 1}
""",
#
# this will fail to load since import doesn't exist
Expand Down Expand Up @@ -197,9 +198,12 @@ async def state_changed(event):
assert hass.services.has_service("pyscript", "func14")
assert not hass.services.has_service("pyscript", "func15")

await hass.services.async_call("pyscript", "func1", {})
service_ret = await hass.services.async_call(
"pyscript", "func1", {}, return_response=True, blocking=True
)
ret = await wait_until_done(notify_q)
assert literal_eval(ret) == [1 + 2, 3 * 4, 10 + 20, 50 - 20]
assert service_ret == {"a": 1}

await hass.services.async_call("pyscript", "func20", {})
ret = await wait_until_done(notify_q)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_function.py
Expand Up @@ -1275,16 +1275,16 @@ def func_startup():
seq_num += 1
pyscript.var1 = 1
pyscript.service1(blocking=True)
pyscript.done = [seq_num, pyscript.var1]
r = pyscript.service1(blocking=True, return_response = True)
pyscript.done = [seq_num, r["var1"]]
seq_num += 1
pyscript.service1(blocking=True)
pyscript.done = [seq_num, pyscript.var1]
seq_num += 1
service.call("pyscript", "service1", blocking=True)
pyscript.done = [seq_num, pyscript.var1]
r = service.call("pyscript", "service1", blocking=True, return_response = True)
pyscript.done = [seq_num, r["var1"]]
seq_num += 1
service.call("pyscript", "short_sleep", blocking=True)
Expand All @@ -1301,9 +1301,10 @@ def short_sleep():
task.sleep(0.0001)
pyscript.var1 = int(pyscript.var1) + 1
@service
@service(supports_response = "optional")
def service1():
pyscript.var1 = int(pyscript.var1) + 1
return {"var1": pyscript.var1}
""",
config={DOMAIN: {CONF_ALLOW_ALL_IMPORTS: True, CONF_HASS_IS_GLOBAL: True}},
Expand Down

0 comments on commit 83aba1b

Please sign in to comment.