2017-02-23 21:02:56 +00:00
|
|
|
"""Test dispatcher helpers."""
|
2020-03-27 03:44:44 +00:00
|
|
|
from functools import partial
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-02-23 21:02:56 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import (
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect,
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_simple_function(hass):
|
|
|
|
"""Test simple function (executor)."""
|
|
|
|
calls = []
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
def test_funct(data):
|
|
|
|
"""Test function."""
|
|
|
|
calls.append(data)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_connect(hass, "test", test_funct)
|
|
|
|
async_dispatcher_send(hass, "test", 3)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test", "bla")
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3, "bla"]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_simple_function_unsub(hass):
|
|
|
|
"""Test simple function (executor) and unsub."""
|
|
|
|
calls1 = []
|
|
|
|
calls2 = []
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
def test_funct1(data):
|
|
|
|
"""Test function."""
|
|
|
|
calls1.append(data)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
def test_funct2(data):
|
|
|
|
"""Test function."""
|
|
|
|
calls2.append(data)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_connect(hass, "test1", test_funct1)
|
|
|
|
unsub = async_dispatcher_connect(hass, "test2", test_funct2)
|
|
|
|
async_dispatcher_send(hass, "test1", 3)
|
|
|
|
async_dispatcher_send(hass, "test2", 4)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls1 == [3]
|
|
|
|
assert calls2 == [4]
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
unsub()
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test1", 5)
|
|
|
|
async_dispatcher_send(hass, "test2", 6)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls1 == [3, 5]
|
|
|
|
assert calls2 == [4]
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
# check don't kill the flow
|
|
|
|
unsub()
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test1", 7)
|
|
|
|
async_dispatcher_send(hass, "test2", 8)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls1 == [3, 5, 7]
|
|
|
|
assert calls2 == [4]
|
2017-02-25 01:11:50 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_simple_callback(hass):
|
|
|
|
"""Test simple callback (async)."""
|
|
|
|
calls = []
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
@callback
|
|
|
|
def test_funct(data):
|
|
|
|
"""Test function."""
|
|
|
|
calls.append(data)
|
2017-02-25 01:11:50 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_connect(hass, "test", test_funct)
|
|
|
|
async_dispatcher_send(hass, "test", 3)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test", "bla")
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3, "bla"]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_simple_coro(hass):
|
|
|
|
"""Test simple coro (async)."""
|
|
|
|
calls = []
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def async_test_funct(data):
|
|
|
|
"""Test function."""
|
|
|
|
calls.append(data)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_connect(hass, "test", async_test_funct)
|
|
|
|
async_dispatcher_send(hass, "test", 3)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test", "bla")
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3, "bla"]
|
2017-02-23 21:02:56 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_simple_function_multiargs(hass):
|
|
|
|
"""Test simple function (executor)."""
|
|
|
|
calls = []
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
def test_funct(data1, data2, data3):
|
|
|
|
"""Test function."""
|
|
|
|
calls.append(data1)
|
|
|
|
calls.append(data2)
|
|
|
|
calls.append(data3)
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_connect(hass, "test", test_funct)
|
|
|
|
async_dispatcher_send(hass, "test", 3, 2, "bla")
|
|
|
|
await hass.async_block_till_done()
|
2017-02-23 21:02:56 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
assert calls == [3, 2, "bla"]
|
2019-01-17 22:44:57 +00:00
|
|
|
|
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
@pytest.mark.no_fail_on_log_exception
|
2019-01-17 22:44:57 +00:00
|
|
|
async def test_callback_exception_gets_logged(hass, caplog):
|
|
|
|
"""Test exception raised by signal handler."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-01-17 22:44:57 +00:00
|
|
|
@callback
|
|
|
|
def bad_handler(*args):
|
|
|
|
"""Record calls."""
|
2019-07-31 19:25:30 +00:00
|
|
|
raise Exception("This is a bad message callback")
|
2019-01-17 22:44:57 +00:00
|
|
|
|
2020-03-27 03:44:44 +00:00
|
|
|
# wrap in partial to test message logging.
|
|
|
|
async_dispatcher_connect(hass, "test", partial(bad_handler))
|
2020-04-07 16:33:23 +00:00
|
|
|
async_dispatcher_send(hass, "test", "bad")
|
2019-01-17 22:44:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-03-27 03:44:44 +00:00
|
|
|
assert (
|
|
|
|
f"Exception in functools.partial({bad_handler}) when dispatching 'test': ('bad',)"
|
|
|
|
in caplog.text
|
|
|
|
)
|