diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index f72f9c99..f1da14eb 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -209,7 +209,7 @@ def notify_del(cls, var_names: set[str], queue: asyncio.Queue) -> None: continue state_var_name = f"{parts[0]}.{parts[1]}" if state_var_name not in cls.notify or queue not in cls.notify[state_var_name]: - return + continue del cls.notify[state_var_name][queue] @classmethod diff --git a/tests/test_state.py b/tests/test_state.py index fd7539c9..1a5d8d97 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -1,5 +1,6 @@ """Test pyscripts test module.""" +import asyncio from datetime import UTC, datetime from unittest.mock import patch @@ -98,6 +99,27 @@ async def handler(call): await Function.reaper_stop() +class SortedSet(set): + """Set that iterates in sorted order, so the test does not depend on string hashing.""" + + def __iter__(self): + """Iterate over the items in sorted order.""" + return iter(sorted(set.__iter__(self))) + + +@pytest.mark.asyncio +async def test_notify_del_removes_every_watch(): + """Test notify_del removes the queue even after a watch it already removed.""" + queue = asyncio.Queue() + var_names = SortedSet(["sensor.example", "sensor.example.flag", "sensor.other"]) + + await State.notify_add(var_names, queue) + State.notify_del(var_names, queue) + + assert queue not in State.notify.get("sensor.example", {}) + assert queue not in State.notify.get("sensor.other", {}) + + def test_state_val_conversions(): """Test helper conversion methods exposed on StateVal.""" float_state = StateVal(HassState("test.float", "123.45"))