Improve dispatcher typing (#106872)

This commit is contained in:
Marc Mueller 2024-01-08 09:45:37 +01:00 committed by GitHub
parent ea4143154b
commit fde7a6e9ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 161 additions and 15 deletions

View file

@ -5,6 +5,7 @@ import pytest
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import (
SignalType,
async_dispatcher_connect,
async_dispatcher_send,
)
@ -30,6 +31,32 @@ async def test_simple_function(hass: HomeAssistant) -> None:
assert calls == [3, "bla"]
async def test_signal_type(hass: HomeAssistant) -> None:
"""Test dispatcher with SignalType."""
signal: SignalType[str, int] = SignalType("test")
calls: list[tuple[str, int]] = []
def test_funct(data1: str, data2: int) -> None:
calls.append((data1, data2))
async_dispatcher_connect(hass, signal, test_funct)
async_dispatcher_send(hass, signal, "Hello", 2)
await hass.async_block_till_done()
assert calls == [("Hello", 2)]
async_dispatcher_send(hass, signal, "World", 3)
await hass.async_block_till_done()
assert calls == [("Hello", 2), ("World", 3)]
# Test compatibility with string keys
async_dispatcher_send(hass, "test", "x", 4)
await hass.async_block_till_done()
assert calls == [("Hello", 2), ("World", 3), ("x", 4)]
async def test_simple_function_unsub(hass: HomeAssistant) -> None:
"""Test simple function (executor) and unsub."""
calls1 = []