Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/pyscript/decorators/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class StateTriggerDecorator(TriggerDecorator, ExpressionDecorator, AutoKwargsDec
vol.Optional("state_hold"): vol.Any(None, cv.positive_float),
vol.Optional("state_hold_false"): vol.Any(None, cv.positive_float),
vol.Optional("state_check_now"): cv.boolean,
vol.Optional("watch"): vol.Coerce(set[str], msg="should be type list or set"),
vol.Optional("watch"): vol.Any(None, vol.Coerce(set), msg="should be type list or set"),
vol.Optional("__test_handshake__"): vol.Coerce(list),
}
)
Expand Down
6 changes: 3 additions & 3 deletions custom_components/pyscript/decorators/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class TimeActiveDecorator(TriggerHandlerDecorator, AutoKwargsDecorator):

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

hold_off: float
hold_off: float | None

last_trig_time: float = 0.0

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

Expand Down
7 changes: 4 additions & 3 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Configuration

Starting with version ``2.0.0``, pyscript uses the new decorator subsystem by default.
If you run into a problem in the new implementation, you can temporarily set
``legacy_decorators: true`` to switch back to the legacy subsystem. If you do that,
please also file a bug report in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
so the problem can be fixed.
``legacy_decorators: true`` to switch back to the legacy subsystem. This setting takes
effect only after restarting Home Assistant. If you do that, please also file a bug report
in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__ so
the problem can be fixed.

- Add files with a suffix of ``.py`` in the folder ``<config>/pyscript``.
- Restart HASS after installing pyscript.
Expand Down
8 changes: 5 additions & 3 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ time by selecting "configure" under Pyscript Python scripting on the Settings
-> Devices & services -> Integrations page. For the new settings to take
effect, you will need to either select "Reload" from the overflow menu
(button with three dots) next to pyscript or call the ``pyscript.reload``
service from Developer tools -> Services.
service from Developer tools -> Services, except for ``legacy_decorators``,
which currently takes effect only after restarting Home Assistant.

Alternatively, for yaml configuration, add ``pyscript:`` to ``<config>/configuration.yaml``.
You can't mix these two methods - your initial choice determines how you should update
Expand All @@ -35,8 +36,9 @@ in ``<config>/configuration.yaml``:

Starting with version ``2.0.0``, pyscript uses the new decorator subsystem by default.
If you find a problem in the new implementation, you can temporarily set
``legacy_decorators: true`` to switch back to the legacy subsystem. If you do,
please also file a bug report in the `GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
``legacy_decorators: true`` to switch back to the legacy subsystem. This setting takes effect
only after restarting Home Assistant. If you do, please also file a bug report in the
`GitHub issue tracker <https://github.com/custom-components/pyscript/issues>`__
so the problem can be fixed.

It is recommended you put your pyscript configuration its own ``yaml`` file in the ``pyscript``
Expand Down
39 changes: 39 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,45 @@ def func10d(var_name=None, value=None, trigger_type=None, context=None, old_valu
)


@pytest.mark.asyncio
async def test_trigger_kwargs_none(hass):
"""Test that explicit None kwargs are accepted for trigger decorators."""
notify_q = asyncio.Queue(0)

await setup_script(
hass,
notify_q,
None,
[dt(2020, 7, 1, 10, 59, 59, 999998)],
"""
seq_num = 0

@state_trigger("True", watch={"pyscript.var1"})
@time_active(hold_off=None)
def func1(var_name=None, value=None):
global seq_num

seq_num += 1
pyscript.done = ["hold_off", seq_num, var_name, value]

@state_trigger("pyscript.var2 == '2'", watch=None)
def func2(var_name=None, value=None):
pyscript.done = ["watch_none", var_name, value]
""",
)

hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
hass.states.async_set("pyscript.var1", 2)
assert literal_eval(await wait_until_done(notify_q)) == ["hold_off", 1, "pyscript.var1", "2"]

hass.states.async_set("pyscript.var1", 0)
assert literal_eval(await wait_until_done(notify_q)) == ["hold_off", 2, "pyscript.var1", "0"]

hass.states.async_set("pyscript.var2", 2)
assert literal_eval(await wait_until_done(notify_q)) == ["watch_none", "pyscript.var2", "2"]


@pytest.mark.asyncio
async def test_state_trigger_time(hass, caplog):
"""Test state trigger."""
Expand Down
Loading