Skip to content

Commit

Permalink
Use service_calls fixture in zone tests (#120995)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jul 2, 2024
1 parent 4a8436d commit 1f67448
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions tests/components/zone/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
from homeassistant.helpers import entity_registry as er
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")
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")


@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
Expand All @@ -43,7 +37,7 @@ def setup_comp(hass):


async def test_if_fires_on_zone_enter(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test for firing on zone enter."""
context = Context()
Expand Down Expand Up @@ -88,9 +82,11 @@ async def test_if_fires_on_zone_enter(
)
await hass.async_block_till_done()

assert len(calls) == 1
assert calls[0].context.parent_id == context.id
assert calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
assert len(service_calls) == 1
assert service_calls[0].context.parent_id == context.id
assert (
service_calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
)

# Set out of zone again so we can trigger call
hass.states.async_set(
Expand All @@ -104,17 +100,20 @@ async def test_if_fires_on_zone_enter(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True,
)
assert len(service_calls) == 2

hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
)
await hass.async_block_till_done()

assert len(calls) == 1
assert len(service_calls) == 2


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:
"""Test for firing on zone enter when device is specified by entity registry id."""
context = Context()
Expand Down Expand Up @@ -165,9 +164,11 @@ async def test_if_fires_on_zone_enter_uuid(
)
await hass.async_block_till_done()

assert len(calls) == 1
assert calls[0].context.parent_id == context.id
assert calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
assert len(service_calls) == 1
assert service_calls[0].context.parent_id == context.id
assert (
service_calls[0].data["some"] == "zone - test.entity - hello - hello - test - 0"
)

# Set out of zone again so we can trigger call
hass.states.async_set(
Expand All @@ -181,17 +182,18 @@ async def test_if_fires_on_zone_enter_uuid(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True,
)
assert len(service_calls) == 2

hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
)
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(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test for not firing on zone leave."""
hass.states.async_set(
Expand Down Expand Up @@ -220,11 +222,11 @@ async def test_if_not_fires_for_enter_on_zone_leave(
)
await hass.async_block_till_done()

assert len(calls) == 0
assert len(service_calls) == 0


async def test_if_fires_on_zone_leave(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test for firing on zone leave."""
hass.states.async_set(
Expand Down Expand Up @@ -253,11 +255,11 @@ async def test_if_fires_on_zone_leave(
)
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(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test for not firing on zone enter."""
hass.states.async_set(
Expand Down Expand Up @@ -286,10 +288,12 @@ async def test_if_not_fires_for_leave_on_zone_enter(
)
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."""
hass.states.async_set(
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
Expand All @@ -314,11 +318,11 @@ async def test_zone_condition(hass: HomeAssistant, calls: list[ServiceCall]) ->

hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1


async def test_unknown_zone(
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test for firing on zone enter."""
context = Context()
Expand Down

0 comments on commit 1f67448

Please sign in to comment.