mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use service_calls fixture in knx tests (#120930)
This commit is contained in:
parent
5513682de4
commit
bc3562a9e8
@ -18,18 +18,12 @@ from homeassistant.setup import async_setup_component
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
from tests.common import async_get_device_automations, async_mock_service
|
from tests.common import async_get_device_automations
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def calls(hass: HomeAssistant) -> list[ServiceCall]:
|
|
||||||
"""Track calls to a mock service."""
|
|
||||||
return async_mock_service(hass, "test", "automation")
|
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_telegram(
|
async def test_if_fires_on_telegram(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -98,31 +92,31 @@ async def test_if_fires_on_telegram(
|
|||||||
|
|
||||||
# "specific" shall ignore destination address
|
# "specific" shall ignore destination address
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
||||||
assert len(calls) == 2
|
assert len(service_calls) == 2
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["specific"] == "telegram - 1/2/4"
|
assert test_call.data["specific"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == "test-id"
|
assert test_call.data["id"] == "test-id"
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
# "specific" shall ignore GroupValueRead
|
# "specific" shall ignore GroupValueRead
|
||||||
await knx.receive_read("1/2/4")
|
await knx.receive_read("1/2/4")
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_default_if_fires_on_telegram(
|
async def test_default_if_fires_on_telegram(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -179,34 +173,34 @@ async def test_default_if_fires_on_telegram(
|
|||||||
)
|
)
|
||||||
|
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
||||||
assert len(calls) == 2
|
assert len(service_calls) == 2
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["specific"] == "telegram - 1/2/4"
|
assert test_call.data["specific"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == "test-id"
|
assert test_call.data["id"] == "test-id"
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
# "specific" shall catch GroupValueRead as it is not set explicitly
|
# "specific" shall catch GroupValueRead as it is not set explicitly
|
||||||
await knx.receive_read("1/2/4")
|
await knx.receive_read("1/2/4")
|
||||||
assert len(calls) == 2
|
assert len(service_calls) == 2
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["specific"] == "telegram - 1/2/4"
|
assert test_call.data["specific"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == "test-id"
|
assert test_call.data["id"] == "test-id"
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_remove_device_trigger(
|
async def test_remove_device_trigger(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -241,8 +235,8 @@ async def test_remove_device_trigger(
|
|||||||
)
|
)
|
||||||
|
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
automation.DOMAIN,
|
automation.DOMAIN,
|
||||||
@ -250,8 +244,10 @@ async def test_remove_device_trigger(
|
|||||||
{ATTR_ENTITY_ID: f"automation.{automation_name}"},
|
{ATTR_ENTITY_ID: f"automation.{automation_name}"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_get_triggers(
|
async def test_get_triggers(
|
||||||
|
@ -11,18 +11,10 @@ from homeassistant.setup import async_setup_component
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def calls(hass: HomeAssistant) -> list[ServiceCall]:
|
|
||||||
"""Track calls to a mock service."""
|
|
||||||
return async_mock_service(hass, "test", "automation")
|
|
||||||
|
|
||||||
|
|
||||||
async def test_telegram_trigger(
|
async def test_telegram_trigger(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test telegram triggers firing."""
|
"""Test telegram triggers firing."""
|
||||||
@ -73,24 +65,24 @@ async def test_telegram_trigger(
|
|||||||
|
|
||||||
# "specific" shall ignore destination address
|
# "specific" shall ignore destination address
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
await knx.receive_write("1/2/4", (0x03, 0x2F))
|
||||||
assert len(calls) == 2
|
assert len(service_calls) == 2
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["specific"] == "telegram - 1/2/4"
|
assert test_call.data["specific"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == "test-id"
|
assert test_call.data["id"] == "test-id"
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
# "specific" shall ignore GroupValueRead
|
# "specific" shall ignore GroupValueRead
|
||||||
await knx.receive_read("1/2/4")
|
await knx.receive_read("1/2/4")
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
assert test_call.data["catch_all"] == "telegram - 1/2/4"
|
||||||
assert test_call.data["id"] == 0
|
assert test_call.data["id"] == 0
|
||||||
|
|
||||||
@ -105,7 +97,7 @@ async def test_telegram_trigger(
|
|||||||
)
|
)
|
||||||
async def test_telegram_trigger_dpt_option(
|
async def test_telegram_trigger_dpt_option(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
payload: tuple[int, ...],
|
payload: tuple[int, ...],
|
||||||
type_option: dict[str, bool],
|
type_option: dict[str, bool],
|
||||||
@ -138,16 +130,16 @@ async def test_telegram_trigger_dpt_option(
|
|||||||
)
|
)
|
||||||
await knx.receive_write("0/0/1", payload)
|
await knx.receive_write("0/0/1", payload)
|
||||||
|
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
||||||
assert test_call.data["trigger"]["value"] == expected_value
|
assert test_call.data["trigger"]["value"] == expected_value
|
||||||
assert test_call.data["trigger"]["unit"] == expected_unit
|
assert test_call.data["trigger"]["unit"] == expected_unit
|
||||||
|
|
||||||
await knx.receive_read("0/0/1")
|
await knx.receive_read("0/0/1")
|
||||||
|
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
test_call = calls.pop()
|
test_call = service_calls.pop()
|
||||||
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
assert test_call.data["catch_all"] == "telegram - 0/0/1"
|
||||||
assert test_call.data["trigger"]["value"] is None
|
assert test_call.data["trigger"]["value"] is None
|
||||||
assert test_call.data["trigger"]["unit"] is None
|
assert test_call.data["trigger"]["unit"] is None
|
||||||
@ -192,7 +184,7 @@ async def test_telegram_trigger_dpt_option(
|
|||||||
)
|
)
|
||||||
async def test_telegram_trigger_options(
|
async def test_telegram_trigger_options(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
group_value_options: dict[str, bool],
|
group_value_options: dict[str, bool],
|
||||||
direction_options: dict[str, bool],
|
direction_options: dict[str, bool],
|
||||||
@ -225,28 +217,28 @@ async def test_telegram_trigger_options(
|
|||||||
if group_value_options.get("group_value_write", True) and direction_options.get(
|
if group_value_options.get("group_value_write", True) and direction_options.get(
|
||||||
"incoming", True
|
"incoming", True
|
||||||
):
|
):
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
else:
|
else:
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 0
|
||||||
|
|
||||||
await knx.receive_response("0/0/1", 1)
|
await knx.receive_response("0/0/1", 1)
|
||||||
if group_value_options["group_value_response"] and direction_options.get(
|
if group_value_options["group_value_response"] and direction_options.get(
|
||||||
"incoming", True
|
"incoming", True
|
||||||
):
|
):
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
else:
|
else:
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 0
|
||||||
|
|
||||||
await knx.receive_read("0/0/1")
|
await knx.receive_read("0/0/1")
|
||||||
if group_value_options["group_value_read"] and direction_options.get(
|
if group_value_options["group_value_read"] and direction_options.get(
|
||||||
"incoming", True
|
"incoming", True
|
||||||
):
|
):
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
else:
|
else:
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 0
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"knx",
|
"knx",
|
||||||
@ -254,20 +246,22 @@ async def test_telegram_trigger_options(
|
|||||||
{"address": "0/0/1", "payload": True},
|
{"address": "0/0/1", "payload": True},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
await knx.assert_write("0/0/1", True)
|
await knx.assert_write("0/0/1", True)
|
||||||
if (
|
if (
|
||||||
group_value_options.get("group_value_write", True)
|
group_value_options.get("group_value_write", True)
|
||||||
and direction_options["outgoing"]
|
and direction_options["outgoing"]
|
||||||
):
|
):
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 2
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
else:
|
else:
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_remove_telegram_trigger(
|
async def test_remove_telegram_trigger(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
calls: list[ServiceCall],
|
service_calls: list[ServiceCall],
|
||||||
knx: KNXTestKit,
|
knx: KNXTestKit,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test for removed callback when telegram trigger not used."""
|
"""Test for removed callback when telegram trigger not used."""
|
||||||
@ -296,8 +290,8 @@ async def test_remove_telegram_trigger(
|
|||||||
)
|
)
|
||||||
|
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
assert service_calls.pop().data["catch_all"] == "telegram - 0/0/1"
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
automation.DOMAIN,
|
automation.DOMAIN,
|
||||||
@ -305,8 +299,10 @@ async def test_remove_telegram_trigger(
|
|||||||
{ATTR_ENTITY_ID: f"automation.{automation_name}"},
|
{ATTR_ENTITY_ID: f"automation.{automation_name}"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
await knx.receive_write("0/0/1", (0x03, 0x2F))
|
||||||
assert len(calls) == 0
|
assert len(service_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_trigger(
|
async def test_invalid_trigger(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user