Skip to content

Commit d320aff

Browse files
authored
Merge pull request #822 from dmamelin/dm-fix1
DM fixes
2 parents 71309dc + df01c7b commit d320aff

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

custom_components/pyscript/decorators/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class StateTriggerDecorator(TriggerDecorator, ExpressionDecorator, AutoKwargsDec
7979
vol.Optional("state_hold"): vol.Any(None, cv.positive_float),
8080
vol.Optional("state_hold_false"): vol.Any(None, cv.positive_float),
8181
vol.Optional("state_check_now"): cv.boolean,
82-
vol.Optional("watch"): vol.Coerce(set[str], msg="should be type list or set"),
82+
vol.Optional("watch"): vol.Any(None, vol.Coerce(set), msg="should be type list or set"),
8383
vol.Optional("__test_handshake__"): vol.Coerce(list),
8484
}
8585
)

custom_components/pyscript/decorators/timing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ class TimeActiveDecorator(TriggerHandlerDecorator, AutoKwargsDecorator):
3030

3131
name = "time_active"
3232
args_schema = vol.Schema(vol.All([vol.Coerce(str)], vol.Length(min=0)))
33-
kwargs_schema = vol.Schema({vol.Optional("hold_off", default=0.0): cv.positive_float})
33+
kwargs_schema = vol.Schema({vol.Optional("hold_off", default=0.0): vol.Any(None, cv.positive_float)})
3434

35-
hold_off: float
35+
hold_off: float | None
3636

3737
last_trig_time: float = 0.0
3838

3939
async def handle_dispatch(self, data: DispatchData) -> bool:
4040
"""Handle dispatch."""
41-
if self.last_trig_time > 0.0 and self.hold_off > 0.0:
41+
if self.last_trig_time > 0.0 and self.hold_off is not None and self.hold_off > 0.0:
4242
if time.monotonic() - self.last_trig_time < self.hold_off:
4343
return False
4444

docs/configuration.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ Configuration
2020
2121
Starting with version ``2.0.0``, pyscript uses the new decorator subsystem by default.
2222
If you run into a problem in the new implementation, you can temporarily set
23-
``legacy_decorators: true`` to switch back to the legacy subsystem. If you do that,
24-
please also file a bug report in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
25-
so the problem can be fixed.
23+
``legacy_decorators: true`` to switch back to the legacy subsystem. This setting takes
24+
effect only after restarting Home Assistant. If you do that, please also file a bug report
25+
in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__ so
26+
the problem can be fixed.
2627

2728
- Add files with a suffix of ``.py`` in the folder ``<config>/pyscript``.
2829
- Restart HASS after installing pyscript.

docs/reference.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ time by selecting "configure" under Pyscript Python scripting on the Settings
1313
-> Devices & services -> Integrations page. For the new settings to take
1414
effect, you will need to either select "Reload" from the overflow menu
1515
(button with three dots) next to pyscript or call the ``pyscript.reload``
16-
service from Developer tools -> Services.
16+
service from Developer tools -> Services, except for ``legacy_decorators``,
17+
which currently takes effect only after restarting Home Assistant.
1718

1819
Alternatively, for yaml configuration, add ``pyscript:`` to ``<config>/configuration.yaml``.
1920
You can't mix these two methods - your initial choice determines how you should update
@@ -35,8 +36,9 @@ in ``<config>/configuration.yaml``:
3536
3637
Starting with version ``2.0.0``, pyscript uses the new decorator subsystem by default.
3738
If you find a problem in the new implementation, you can temporarily set
38-
``legacy_decorators: true`` to switch back to the legacy subsystem. If you do,
39-
please also file a bug report in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
39+
``legacy_decorators: true`` to switch back to the legacy subsystem. This setting takes effect
40+
only after restarting Home Assistant. If you do, please also file a bug report in the
41+
`GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
4042
so the problem can be fixed.
4143

4244
It is recommended you put your pyscript configuration its own ``yaml`` file in the ``pyscript``

tests/test_function.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,45 @@ def func10d(var_name=None, value=None, trigger_type=None, context=None, old_valu
750750
)
751751

752752

753+
@pytest.mark.asyncio
754+
async def test_trigger_kwargs_none(hass):
755+
"""Test that explicit None kwargs are accepted for trigger decorators."""
756+
notify_q = asyncio.Queue(0)
757+
758+
await setup_script(
759+
hass,
760+
notify_q,
761+
None,
762+
[dt(2020, 7, 1, 10, 59, 59, 999998)],
763+
"""
764+
seq_num = 0
765+
766+
@state_trigger("True", watch={"pyscript.var1"})
767+
@time_active(hold_off=None)
768+
def func1(var_name=None, value=None):
769+
global seq_num
770+
771+
seq_num += 1
772+
pyscript.done = ["hold_off", seq_num, var_name, value]
773+
774+
@state_trigger("pyscript.var2 == '2'", watch=None)
775+
def func2(var_name=None, value=None):
776+
pyscript.done = ["watch_none", var_name, value]
777+
""",
778+
)
779+
780+
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
781+
await hass.async_block_till_done()
782+
hass.states.async_set("pyscript.var1", 2)
783+
assert literal_eval(await wait_until_done(notify_q)) == ["hold_off", 1, "pyscript.var1", "2"]
784+
785+
hass.states.async_set("pyscript.var1", 0)
786+
assert literal_eval(await wait_until_done(notify_q)) == ["hold_off", 2, "pyscript.var1", "0"]
787+
788+
hass.states.async_set("pyscript.var2", 2)
789+
assert literal_eval(await wait_until_done(notify_q)) == ["watch_none", "pyscript.var2", "2"]
790+
791+
753792
@pytest.mark.asyncio
754793
async def test_state_trigger_time(hass, caplog):
755794
"""Test state trigger."""

0 commit comments

Comments
 (0)