1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Use service_calls fixture in core platform tests [a-l] (#120904)

This commit is contained in:
epenet
2024-07-01 19:27:50 +02:00
committed by GitHub
parent c2dc9e9b67
commit 07f095aa42
20 changed files with 316 additions and 473 deletions

View File

@@ -22,7 +22,6 @@ from tests.common import (
MockConfigEntry,
async_get_device_automation_capabilities,
async_get_device_automations,
async_mock_service,
setup_test_component_platform,
)
@@ -32,12 +31,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")
async def test_get_conditions(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
@@ -239,7 +232,7 @@ async def test_if_state(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for turn_on and turn_off conditions."""
@@ -308,26 +301,26 @@ async def test_if_state(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1"
assert len(service_calls) == 1
assert service_calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "is_off event - test_event2"
assert len(service_calls) == 2
assert service_calls[1].data["some"] == "is_off event - test_event2"
async def test_if_state_legacy(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for turn_on and turn_off conditions."""
@@ -375,19 +368,19 @@ async def test_if_state_legacy(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1"
assert len(service_calls) == 1
assert service_calls[0].data["some"] == "is_on event - test_event1"
async def test_if_fires_on_for_condition(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for firing if condition is on with delay."""
@@ -439,26 +432,26 @@ async def test_if_fires_on_for_condition(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
# Time travel 10 secs into the future
time_freeze.move_to(point2)
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
# Time travel 20 secs into the future
time_freeze.move_to(point3)
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "is_off event - test_event1"
assert len(service_calls) == 1
assert service_calls[0].data["some"] == "is_off event - test_event1"

View File

@@ -22,7 +22,6 @@ from tests.common import (
async_fire_time_changed,
async_get_device_automation_capabilities,
async_get_device_automations,
async_mock_service,
setup_test_component_platform,
)
@@ -32,12 +31,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")
async def test_get_triggers(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
@@ -240,7 +233,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for on and off triggers firing."""
@@ -313,21 +306,22 @@ async def test_if_fires_on_state_change(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
assert (
calls[0].data["some"]
service_calls[0].data["some"]
== f"not_bat_low device - {entry.entity_id} - on - off - None"
)
hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert len(service_calls) == 2
assert (
calls[1].data["some"] == f"bat_low device - {entry.entity_id} - off - on - None"
service_calls[1].data["some"]
== f"bat_low device - {entry.entity_id} - off - on - None"
)
@@ -335,7 +329,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for triggers firing with delay."""
@@ -388,17 +382,17 @@ async def test_if_fires_on_state_change_with_for(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
await hass.async_block_till_done()
assert (
calls[0].data["some"]
service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
)
@@ -407,7 +401,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
mock_binary_sensor_entities: dict[str, MockBinarySensor],
) -> None:
"""Test for triggers firing."""
@@ -459,12 +453,12 @@ async def test_if_fires_on_state_change_legacy(
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
assert (
calls[0].data["some"]
service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - None"
)