|
5 | 5 | from datetime import datetime as dt
|
6 | 6 | import time
|
7 | 7 |
|
8 |
| -from custom_components.pyscript.const import CONF_ALLOW_ALL_IMPORTS, DOMAIN |
| 8 | +from custom_components.pyscript.const import CONF_ALLOW_ALL_IMPORTS, CONF_HASS_IS_GLOBAL, DOMAIN |
9 | 9 | from custom_components.pyscript.function import Function
|
10 | 10 | import custom_components.pyscript.trigger as trigger
|
11 | 11 | import pytest
|
@@ -98,23 +98,25 @@ async def test_service_completions(root, expected, hass, services): # pylint: d
|
98 | 98 | assert words == expected
|
99 | 99 |
|
100 | 100 |
|
101 |
| -async def setup_script(hass, notify_q, now, source): |
| 101 | +async def setup_script(hass, notify_q, now, source, config=None): |
102 | 102 | """Initialize and load the given pyscript."""
|
103 | 103 | scripts = [
|
104 | 104 | "/some/config/dir/pyscripts/hello.py",
|
105 | 105 | ]
|
106 | 106 |
|
| 107 | + if not config: |
| 108 | + config = {DOMAIN: {CONF_ALLOW_ALL_IMPORTS: True}} |
107 | 109 | with patch("custom_components.pyscript.os.path.isdir", return_value=True), patch(
|
108 | 110 | "custom_components.pyscript.glob.iglob", return_value=scripts
|
109 | 111 | ), patch("custom_components.pyscript.global_ctx.open", mock_open(read_data=source), create=True,), patch(
|
110 | 112 | "custom_components.pyscript.trigger.dt_now", return_value=now
|
111 | 113 | ), patch(
|
112 |
| - "homeassistant.config.load_yaml_config_file", return_value={DOMAIN: {CONF_ALLOW_ALL_IMPORTS: True}} |
| 114 | + "homeassistant.config.load_yaml_config_file", return_value=config |
113 | 115 | ), patch(
|
114 | 116 | "custom_components.pyscript.process_all_requirements",
|
115 | 117 | return_value={"/some/config/dir/pyscript/requirements.txt": ["pytube==2.0.1", "pykakasi==2.0.1"]},
|
116 | 118 | ):
|
117 |
| - assert await async_setup_component(hass, "pyscript", {DOMAIN: {CONF_ALLOW_ALL_IMPORTS: True}}) |
| 119 | + assert await async_setup_component(hass, "pyscript", config) |
118 | 120 |
|
119 | 121 | #
|
120 | 122 | # I'm not sure how to run the mock all the time, so just force the dt_now()
|
@@ -742,3 +744,64 @@ def func_startup_sync2(trigger_type=None, var_name=None):
|
742 | 744 | hass.states.async_set("pyscript.fstartup0", 0)
|
743 | 745 | hass.states.async_set("pyscript.fstartup0", 1)
|
744 | 746 | assert literal_eval(await wait_until_done(notify_q)) == [0, "state", "pyscript.fstartup0"]
|
| 747 | + |
| 748 | + |
| 749 | +async def test_state_methods(hass, caplog): |
| 750 | + """Test state methods that call services.""" |
| 751 | + notify_q = asyncio.Queue(0) |
| 752 | + |
| 753 | + await setup_script( |
| 754 | + hass, |
| 755 | + notify_q, |
| 756 | + [dt(2020, 7, 1, 12, 0, 0, 0)], |
| 757 | + """ |
| 758 | +
|
| 759 | +seq_num = 0 |
| 760 | +
|
| 761 | +@time_trigger("startup") |
| 762 | +def func_startup(): |
| 763 | + pyscript.var1 = 10 |
| 764 | + pyscript.var1.set(20) |
| 765 | + pyscript.var1.set(value=30) |
| 766 | + pyscript.var1.incr() |
| 767 | + pyscript.var1.incr() |
| 768 | + pyscript.var1.set_add(val1=10, val2=40) |
| 769 | + # |
| 770 | + # this will generate an exception |
| 771 | + # |
| 772 | + pyscript.var1.set_add(10) |
| 773 | +
|
| 774 | +@service |
| 775 | +def set(entity_id=None, value=None): |
| 776 | + global seq_num |
| 777 | +
|
| 778 | + seq_num += 1 |
| 779 | + state.set(entity_id, value) |
| 780 | + pyscript.done = [seq_num, entity_id, state.get(entity_id)] |
| 781 | +
|
| 782 | +@service |
| 783 | +def incr(entity_id=None): |
| 784 | + global seq_num |
| 785 | +
|
| 786 | + seq_num += 1 |
| 787 | + state.set(entity_id, int(state.get(entity_id)) + 1) |
| 788 | + pyscript.done = [seq_num, entity_id, state.get(entity_id)] |
| 789 | +
|
| 790 | +@service |
| 791 | +def set_add(entity_id=None, val1=None, val2=None): |
| 792 | + global seq_num |
| 793 | +
|
| 794 | + seq_num += 1 |
| 795 | + state.set(entity_id, val1 + val2) |
| 796 | + pyscript.done = [seq_num, entity_id, state.get(entity_id), type(hass).__name__] |
| 797 | +
|
| 798 | +""", |
| 799 | + config={DOMAIN: {CONF_ALLOW_ALL_IMPORTS: True, CONF_HASS_IS_GLOBAL: True}}, |
| 800 | + ) |
| 801 | + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) |
| 802 | + assert literal_eval(await wait_until_done(notify_q)) == [1, "pyscript.var1", "20"] |
| 803 | + assert literal_eval(await wait_until_done(notify_q)) == [2, "pyscript.var1", "30"] |
| 804 | + assert literal_eval(await wait_until_done(notify_q)) == [3, "pyscript.var1", "31"] |
| 805 | + assert literal_eval(await wait_until_done(notify_q)) == [4, "pyscript.var1", "32"] |
| 806 | + assert literal_eval(await wait_until_done(notify_q)) == [5, "pyscript.var1", "50", "HomeAssistant"] |
| 807 | + assert "TypeError: service pyscript.set_add takes no positional arguments" in caplog.text |
0 commit comments