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/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test pyscripts test module."""

import asyncio
from datetime import UTC, datetime
from unittest.mock import patch

Expand Down Expand Up @@ -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"))
Expand Down
Loading