Use service_calls fixture in zone tests (#120995)

This commit is contained in:
epenet 2024-07-02 12:29:21 +02:00 committed by GitHub
parent 4a8436d6bc
commit 1f6744847d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,7 +8,7 @@ from homeassistant.core import Context, HomeAssistant, ServiceCall
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import async_mock_service, mock_component from tests.common import mock_component
@pytest.fixture(autouse=True, name="stub_blueprint_populate") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -16,12 +16,6 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
"""Stub copying the blueprints to the config folder.""" """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")
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup_comp(hass): def setup_comp(hass):
"""Initialize components.""" """Initialize components."""
@ -43,7 +37,7 @@ def setup_comp(hass):
async def test_if_fires_on_zone_enter( async def test_if_fires_on_zone_enter(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for firing on zone enter.""" """Test for firing on zone enter."""
context = Context() context = Context()
@ -88,9 +82,11 @@ async def test_if_fires_on_zone_enter(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].context.parent_id == context.id assert service_calls[0].context.parent_id == context.id
assert calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0" assert (
service_calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
)
# Set out of zone again so we can trigger call # Set out of zone again so we can trigger call
hass.states.async_set( hass.states.async_set(
@ -104,17 +100,20 @@ async def test_if_fires_on_zone_enter(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True, blocking=True,
) )
assert len(service_calls) == 2
hass.states.async_set( hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564} "test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 2
async def test_if_fires_on_zone_enter_uuid( async def test_if_fires_on_zone_enter_uuid(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls: list[ServiceCall] hass: HomeAssistant,
entity_registry: er.EntityRegistry,
service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for firing on zone enter when device is specified by entity registry id.""" """Test for firing on zone enter when device is specified by entity registry id."""
context = Context() context = Context()
@ -165,9 +164,11 @@ async def test_if_fires_on_zone_enter_uuid(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].context.parent_id == context.id assert service_calls[0].context.parent_id == context.id
assert calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0" assert (
service_calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
)
# Set out of zone again so we can trigger call # Set out of zone again so we can trigger call
hass.states.async_set( hass.states.async_set(
@ -181,17 +182,18 @@ async def test_if_fires_on_zone_enter_uuid(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True, blocking=True,
) )
assert len(service_calls) == 2
hass.states.async_set( hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564} "test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 2
async def test_if_not_fires_for_enter_on_zone_leave( async def test_if_not_fires_for_enter_on_zone_leave(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for not firing on zone leave.""" """Test for not firing on zone leave."""
hass.states.async_set( hass.states.async_set(
@ -220,11 +222,11 @@ async def test_if_not_fires_for_enter_on_zone_leave(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
async def test_if_fires_on_zone_leave( async def test_if_fires_on_zone_leave(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for firing on zone leave.""" """Test for firing on zone leave."""
hass.states.async_set( hass.states.async_set(
@ -253,11 +255,11 @@ async def test_if_fires_on_zone_leave(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_if_not_fires_for_leave_on_zone_enter( async def test_if_not_fires_for_leave_on_zone_enter(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for not firing on zone enter.""" """Test for not firing on zone enter."""
hass.states.async_set( hass.states.async_set(
@ -286,10 +288,12 @@ async def test_if_not_fires_for_leave_on_zone_enter(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
async def test_zone_condition(hass: HomeAssistant, calls: list[ServiceCall]) -> None: async def test_zone_condition(
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test for zone condition.""" """Test for zone condition."""
hass.states.async_set( hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564} "test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
@ -314,11 +318,11 @@ async def test_zone_condition(hass: HomeAssistant, calls: list[ServiceCall]) ->
hass.bus.async_fire("test_event") hass.bus.async_fire("test_event")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_unknown_zone( async def test_unknown_zone(
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test for firing on zone enter.""" """Test for firing on zone enter."""
context = Context() context = Context()