Improve dispatcher typing (#106872)
This commit is contained in:
parent
ea4143154b
commit
fde7a6e9ef
5 changed files with 161 additions and 15 deletions
|
@ -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 = []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue