Use service_calls fixture in homekit_controller tests (#120927)

pull/120945/head
epenet 2024-07-01 17:55:39 +02:00 committed by GitHub
parent b3a50893cf
commit afb0a6e0ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 24 deletions

View File

@ -15,7 +15,7 @@ from homeassistant.setup import async_setup_component
from .common import setup_test_component
from tests.common import async_get_device_automations, async_mock_service
from tests.common import async_get_device_automations
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -23,12 +23,6 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
"""Stub copying the blueprints to the config folder."""
@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")
def create_remote(accessory):
"""Define characteristics for a button (that is inn a group)."""
service_label = accessory.add_service(ServicesTypes.SERVICE_LABEL)
@ -239,7 +233,7 @@ async def test_handle_events(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
) -> None:
"""Test that events are handled."""
helper = await setup_test_component(hass, create_remote)
@ -303,8 +297,8 @@ async def test_handle_events(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "device - button1 - single_press - 0"
assert len(service_calls) == 1
assert service_calls[0].data["some"] == "device - button1 - single_press - 0"
# Make sure automation doesn't trigger for long press
helper.pairing.testing.update_named_service(
@ -312,7 +306,7 @@ async def test_handle_events(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
# Make sure automation doesn't trigger for double press
helper.pairing.testing.update_named_service(
@ -320,7 +314,7 @@ async def test_handle_events(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
# Make sure second automation fires for long press
helper.pairing.testing.update_named_service(
@ -328,8 +322,8 @@ async def test_handle_events(
)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "device - button2 - long_press - 0"
assert len(service_calls) == 2
assert service_calls[1].data["some"] == "device - button2 - long_press - 0"
# Turn the automations off
await hass.services.async_call(
@ -338,6 +332,7 @@ async def test_handle_events(
{"entity_id": "automation.long_press"},
blocking=True,
)
assert len(service_calls) == 3
await hass.services.async_call(
"automation",
@ -345,6 +340,7 @@ async def test_handle_events(
{"entity_id": "automation.single_press"},
blocking=True,
)
assert len(service_calls) == 4
# Make sure event no longer fires
helper.pairing.testing.update_named_service(
@ -352,14 +348,14 @@ async def test_handle_events(
)
await hass.async_block_till_done()
assert len(calls) == 2
assert len(service_calls) == 4
async def test_handle_events_late_setup(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
) -> None:
"""Test that events are handled when setup happens after startup."""
helper = await setup_test_component(hass, create_remote)
@ -432,8 +428,8 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "device - button1 - single_press - 0"
assert len(service_calls) == 1
assert service_calls[0].data["some"] == "device - button1 - single_press - 0"
# Make sure automation doesn't trigger for a polled None
helper.pairing.testing.update_named_service(
@ -441,7 +437,7 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
# Make sure automation doesn't trigger for long press
helper.pairing.testing.update_named_service(
@ -449,7 +445,7 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
# Make sure automation doesn't trigger for double press
helper.pairing.testing.update_named_service(
@ -457,7 +453,7 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
# Make sure second automation fires for long press
helper.pairing.testing.update_named_service(
@ -465,8 +461,8 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "device - button2 - long_press - 0"
assert len(service_calls) == 2
assert service_calls[1].data["some"] == "device - button2 - long_press - 0"
# Turn the automations off
await hass.services.async_call(
@ -475,6 +471,7 @@ async def test_handle_events_late_setup(
{"entity_id": "automation.long_press"},
blocking=True,
)
assert len(service_calls) == 3
await hass.services.async_call(
"automation",
@ -482,6 +479,7 @@ async def test_handle_events_late_setup(
{"entity_id": "automation.single_press"},
blocking=True,
)
assert len(service_calls) == 4
# Make sure event no longer fires
helper.pairing.testing.update_named_service(
@ -489,4 +487,4 @@ async def test_handle_events_late_setup(
)
await hass.async_block_till_done()
assert len(calls) == 2
assert len(service_calls) == 4