Use service_calls fixture in core platform tests [m-z] (#121001)

This commit is contained in:
epenet 2024-07-02 12:34:11 +02:00 committed by GitHub
parent 76a62028ad
commit 22f5f59478
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 237 additions and 360 deletions

View File

@ -20,11 +20,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import RegistryEntryHider from homeassistant.helpers.entity_registry import RegistryEntryHider
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import MockConfigEntry, async_get_device_automations
MockConfigEntry,
async_get_device_automations,
async_mock_service,
)
@pytest.fixture(autouse=True, name="stub_blueprint_populate") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -32,12 +28,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")
async def test_get_conditions( async def test_get_conditions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -136,7 +126,7 @@ async def test_if_state(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -274,8 +264,8 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on - event - test_event1" assert service_calls[0].data["some"] == "is_on - event - test_event1"
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
@ -285,8 +275,8 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["some"] == "is_off - event - test_event2" assert service_calls[1].data["some"] == "is_off - event - test_event2"
hass.states.async_set(entry.entity_id, STATE_IDLE) hass.states.async_set(entry.entity_id, STATE_IDLE)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
@ -296,8 +286,8 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 3 assert len(service_calls) == 3
assert calls[2].data["some"] == "is_idle - event - test_event3" assert service_calls[2].data["some"] == "is_idle - event - test_event3"
hass.states.async_set(entry.entity_id, STATE_PAUSED) hass.states.async_set(entry.entity_id, STATE_PAUSED)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
@ -307,8 +297,8 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(service_calls) == 4
assert calls[3].data["some"] == "is_paused - event - test_event4" assert service_calls[3].data["some"] == "is_paused - event - test_event4"
hass.states.async_set(entry.entity_id, STATE_PLAYING) hass.states.async_set(entry.entity_id, STATE_PLAYING)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
@ -318,8 +308,8 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 5 assert len(service_calls) == 5
assert calls[4].data["some"] == "is_playing - event - test_event5" assert service_calls[4].data["some"] == "is_playing - event - test_event5"
hass.states.async_set(entry.entity_id, STATE_BUFFERING) hass.states.async_set(entry.entity_id, STATE_BUFFERING)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
@ -329,15 +319,15 @@ async def test_if_state(
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6") hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 6 assert len(service_calls) == 6
assert calls[5].data["some"] == "is_buffering - event - test_event6" assert service_calls[5].data["some"] == "is_buffering - event - test_event6"
async def test_if_state_legacy( async def test_if_state_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -380,5 +370,5 @@ async def test_if_state_legacy(
) )
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on - event - test_event1" assert service_calls[0].data["some"] == "is_on - event - test_event1"

View File

@ -28,7 +28,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -37,12 +36,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")
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -209,7 +202,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test triggers firing.""" """Test triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -265,8 +258,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity is turning on. # Fake that the entity is turning on.
hass.states.async_set(entry.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert {calls[0].data["some"], calls[1].data["some"]} == { assert {service_calls[0].data["some"], service_calls[1].data["some"]} == {
"turned_on - device - media_player.test_5678 - off - on - None", "turned_on - device - media_player.test_5678 - off - on - None",
"changed_states - device - media_player.test_5678 - off - on - None", "changed_states - device - media_player.test_5678 - off - on - None",
} }
@ -274,8 +267,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity is turning off. # Fake that the entity is turning off.
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(service_calls) == 4
assert {calls[2].data["some"], calls[3].data["some"]} == { assert {service_calls[2].data["some"], service_calls[3].data["some"]} == {
"turned_off - device - media_player.test_5678 - on - off - None", "turned_off - device - media_player.test_5678 - on - off - None",
"changed_states - device - media_player.test_5678 - on - off - None", "changed_states - device - media_player.test_5678 - on - off - None",
} }
@ -283,8 +276,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity becomes idle. # Fake that the entity becomes idle.
hass.states.async_set(entry.entity_id, STATE_IDLE) hass.states.async_set(entry.entity_id, STATE_IDLE)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 6 assert len(service_calls) == 6
assert {calls[4].data["some"], calls[5].data["some"]} == { assert {service_calls[4].data["some"], service_calls[5].data["some"]} == {
"idle - device - media_player.test_5678 - off - idle - None", "idle - device - media_player.test_5678 - off - idle - None",
"changed_states - device - media_player.test_5678 - off - idle - None", "changed_states - device - media_player.test_5678 - off - idle - None",
} }
@ -292,8 +285,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity starts playing. # Fake that the entity starts playing.
hass.states.async_set(entry.entity_id, STATE_PLAYING) hass.states.async_set(entry.entity_id, STATE_PLAYING)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 8 assert len(service_calls) == 8
assert {calls[6].data["some"], calls[7].data["some"]} == { assert {service_calls[6].data["some"], service_calls[7].data["some"]} == {
"playing - device - media_player.test_5678 - idle - playing - None", "playing - device - media_player.test_5678 - idle - playing - None",
"changed_states - device - media_player.test_5678 - idle - playing - None", "changed_states - device - media_player.test_5678 - idle - playing - None",
} }
@ -301,8 +294,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity is paused. # Fake that the entity is paused.
hass.states.async_set(entry.entity_id, STATE_PAUSED) hass.states.async_set(entry.entity_id, STATE_PAUSED)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 10 assert len(service_calls) == 10
assert {calls[8].data["some"], calls[9].data["some"]} == { assert {service_calls[8].data["some"], service_calls[9].data["some"]} == {
"paused - device - media_player.test_5678 - playing - paused - None", "paused - device - media_player.test_5678 - playing - paused - None",
"changed_states - device - media_player.test_5678 - playing - paused - None", "changed_states - device - media_player.test_5678 - playing - paused - None",
} }
@ -310,8 +303,8 @@ async def test_if_fires_on_state_change(
# Fake that the entity is buffering. # Fake that the entity is buffering.
hass.states.async_set(entry.entity_id, STATE_BUFFERING) hass.states.async_set(entry.entity_id, STATE_BUFFERING)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 12 assert len(service_calls) == 12
assert {calls[10].data["some"], calls[11].data["some"]} == { assert {service_calls[10].data["some"], service_calls[11].data["some"]} == {
"buffering - device - media_player.test_5678 - paused - buffering - None", "buffering - device - media_player.test_5678 - paused - buffering - None",
"changed_states - device - media_player.test_5678 - paused - buffering - None", "changed_states - device - media_player.test_5678 - paused - buffering - None",
} }
@ -321,7 +314,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test triggers firing.""" """Test triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -369,9 +362,9 @@ async def test_if_fires_on_state_change_legacy(
# Fake that the entity is turning on. # Fake that the entity is turning on.
hass.states.async_set(entry.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== "turned_on - device - media_player.test_5678 - off - on - None" == "turned_on - device - media_player.test_5678 - off - on - None"
) )
@ -380,7 +373,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -426,16 +419,16 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done() 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)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - off - on - 0:00:05" == f"turn_off device - {entry.entity_id} - off - on - 0:00:05"
) )

View File

@ -7,7 +7,7 @@ from homeassistant.components import automation
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.remote import DOMAIN from homeassistant.components.remote import DOMAIN
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import RegistryEntryHider from homeassistant.helpers.entity_registry import RegistryEntryHider
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -24,12 +24,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")
async def test_get_actions( async def test_get_actions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -114,7 +108,6 @@ async def test_action(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off actions.""" """Test for turn_on and turn_off actions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -189,7 +182,6 @@ async def test_action_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off actions.""" """Test for turn_on and turn_off actions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})

View File

@ -20,7 +20,6 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -29,12 +28,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")
async def test_get_conditions( async def test_get_conditions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -183,7 +176,7 @@ async def test_if_state(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -249,20 +242,20 @@ async def test_if_state(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1" assert service_calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["some"] == "is_off event - test_event2" assert service_calls[1].data["some"] == "is_off event - test_event2"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -270,7 +263,7 @@ async def test_if_state_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -315,13 +308,13 @@ async def test_if_state_legacy(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1" assert service_calls[0].data["some"] == "is_on event - test_event1"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -329,7 +322,7 @@ async def test_if_fires_on_for_condition(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for firing if condition is on with delay.""" """Test for firing if condition is on with delay."""
point1 = dt_util.utcnow() point1 = dt_util.utcnow()
@ -378,26 +371,26 @@ async def test_if_fires_on_for_condition(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
# Time travel 10 secs into the future # Time travel 10 secs into the future
freezer.move_to(point2) freezer.move_to(point2)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
# Time travel 20 secs into the future # Time travel 20 secs into the future
freezer.move_to(point3) freezer.move_to(point3)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_off event - test_event1" assert service_calls[0].data["some"] == "is_off event - test_event1"

View File

@ -20,7 +20,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -29,12 +28,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")
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -181,7 +174,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -267,20 +260,20 @@ async def test_if_fires_on_state_change(
] ]
}, },
) )
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert {calls[0].data["some"], calls[1].data["some"]} == { assert {service_calls[0].data["some"], service_calls[1].data["some"]} == {
f"turn_off device - {entry.entity_id} - on - off - None", f"turn_off device - {entry.entity_id} - on - off - None",
f"turn_on_or_off device - {entry.entity_id} - on - off - None", f"turn_on_or_off device - {entry.entity_id} - on - off - None",
} }
hass.states.async_set(entry.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(service_calls) == 4
assert {calls[2].data["some"], calls[3].data["some"]} == { assert {service_calls[2].data["some"], service_calls[3].data["some"]} == {
f"turn_on device - {entry.entity_id} - off - on - None", f"turn_on device - {entry.entity_id} - off - on - None",
f"turn_on_or_off device - {entry.entity_id} - off - on - None", f"turn_on_or_off device - {entry.entity_id} - off - on - None",
} }
@ -291,7 +284,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -335,13 +328,13 @@ async def test_if_fires_on_state_change_legacy(
] ]
}, },
) )
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - None" == f"turn_off device - {entry.entity_id} - on - off - None"
) )
@ -351,7 +344,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -397,16 +390,16 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() 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)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05" == f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
) )

View File

@ -21,17 +21,7 @@ from homeassistant.helpers import (
) )
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import MockConfigEntry, async_get_device_automations
MockConfigEntry,
async_get_device_automations,
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_get_conditions( async def test_get_conditions(
@ -115,7 +105,7 @@ async def test_get_conditions_hidden_auxiliary(
async def test_if_selected_option( async def test_if_selected_option(
hass: HomeAssistant, hass: HomeAssistant,
calls: list[ServiceCall], service_calls: list[ServiceCall],
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
@ -181,7 +171,7 @@ async def test_if_selected_option(
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set( hass.states.async_set(
entry.entity_id, "option1", {"options": ["option1", "option2"]} entry.entity_id, "option1", {"options": ["option1", "option2"]}
@ -189,8 +179,8 @@ async def test_if_selected_option(
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["result"] == "option1 - event - test_event1" assert service_calls[0].data["result"] == "option1 - event - test_event1"
hass.states.async_set( hass.states.async_set(
entry.entity_id, "option2", {"options": ["option1", "option2"]} entry.entity_id, "option2", {"options": ["option1", "option2"]}
@ -198,13 +188,13 @@ async def test_if_selected_option(
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["result"] == "option2 - event - test_event2" assert service_calls[1].data["result"] == "option2 - event - test_event2"
async def test_if_selected_option_legacy( async def test_if_selected_option_legacy(
hass: HomeAssistant, hass: HomeAssistant,
calls: list[ServiceCall], service_calls: list[ServiceCall],
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
@ -252,8 +242,8 @@ async def test_if_selected_option_legacy(
) )
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["result"] == "option1 - event - test_event1" assert service_calls[0].data["result"] == "option1 - event - test_event1"
async def test_get_condition_capabilities( async def test_get_condition_capabilities(

View File

@ -21,17 +21,7 @@ from homeassistant.helpers import (
) )
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import MockConfigEntry, async_get_device_automations
MockConfigEntry,
async_get_device_automations,
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_get_triggers( async def test_get_triggers(
@ -117,7 +107,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -210,27 +200,27 @@ async def test_if_fires_on_state_change(
# Test triggering device trigger with a to state # Test triggering device trigger with a to state
hass.states.async_set(entry.entity_id, "option2") hass.states.async_set(entry.entity_id, "option2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0" == f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
) )
# Test triggering device trigger with a from state # Test triggering device trigger with a from state
hass.states.async_set(entry.entity_id, "option3") hass.states.async_set(entry.entity_id, "option3")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert ( assert (
calls[1].data["some"] service_calls[1].data["some"]
== f"from - device - {entry.entity_id} - option2 - option3 - None - 0" == f"from - device - {entry.entity_id} - option2 - option3 - None - 0"
) )
# Test triggering device trigger with both a from and to state # Test triggering device trigger with both a from and to state
hass.states.async_set(entry.entity_id, "option1") hass.states.async_set(entry.entity_id, "option1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 3 assert len(service_calls) == 3
assert ( assert (
calls[2].data["some"] service_calls[2].data["some"]
== f"from-to - device - {entry.entity_id} - option3 - option1 - None - 0" == f"from-to - device - {entry.entity_id} - option3 - option1 - None - 0"
) )
@ -239,7 +229,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -289,9 +279,9 @@ async def test_if_fires_on_state_change_legacy(
# Test triggering device trigger with a to state # Test triggering device trigger with a to state
hass.states.async_set(entry.entity_id, "option2") hass.states.async_set(entry.entity_id, "option2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0" == f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
) )

View File

@ -27,7 +27,6 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
setup_test_component_platform, setup_test_component_platform,
) )
@ -37,12 +36,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.mark.parametrize( @pytest.mark.parametrize(
"device_class", "device_class",
[ [
@ -470,7 +463,6 @@ async def test_if_state_not_above_below(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test for bad value conditions.""" """Test for bad value conditions."""
@ -513,7 +505,7 @@ async def test_if_state_above(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value conditions.""" """Test for value conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -559,22 +551,22 @@ async def test_if_state_above(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "event - test_event1" assert service_calls[0].data["some"] == "event - test_event1"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -582,7 +574,7 @@ async def test_if_state_above_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value conditions.""" """Test for value conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -628,22 +620,22 @@ async def test_if_state_above_legacy(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "event - test_event1" assert service_calls[0].data["some"] == "event - test_event1"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -651,7 +643,7 @@ async def test_if_state_below(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value conditions.""" """Test for value conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -697,22 +689,22 @@ async def test_if_state_below(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "event - test_event1" assert service_calls[0].data["some"] == "event - test_event1"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -720,7 +712,7 @@ async def test_if_state_between(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value conditions.""" """Test for value conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -767,30 +759,30 @@ async def test_if_state_between(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "event - test_event1" assert service_calls[0].data["some"] == "event - test_event1"
hass.states.async_set(entry.entity_id, 21) hass.states.async_set(entry.entity_id, 21)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
hass.states.async_set(entry.entity_id, 19) hass.states.async_set(entry.entity_id, 19)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["some"] == "event - test_event1" assert service_calls[1].data["some"] == "event - test_event1"

View File

@ -31,7 +31,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
setup_test_component_platform, setup_test_component_platform,
) )
@ -41,12 +40,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.mark.parametrize( @pytest.mark.parametrize(
"device_class", "device_class",
[ [
@ -427,7 +420,6 @@ async def test_if_fires_not_on_above_below(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test for value triggers firing.""" """Test for value triggers firing."""
@ -467,7 +459,7 @@ async def test_if_fires_on_state_above(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value triggers firing.""" """Test for value triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -513,17 +505,18 @@ async def test_if_fires_on_state_above(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] == f"bat_low device - {entry.entity_id} - 9 - 11 - None" service_calls[0].data["some"]
== f"bat_low device - {entry.entity_id} - 9 - 11 - None"
) )
@ -532,7 +525,7 @@ async def test_if_fires_on_state_below(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value triggers firing.""" """Test for value triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -578,17 +571,18 @@ async def test_if_fires_on_state_below(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] == f"bat_low device - {entry.entity_id} - 11 - 9 - None" service_calls[0].data["some"]
== f"bat_low device - {entry.entity_id} - 11 - 9 - None"
) )
@ -597,7 +591,7 @@ async def test_if_fires_on_state_between(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value triggers firing.""" """Test for value triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -644,28 +638,30 @@ async def test_if_fires_on_state_between(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] == f"bat_low device - {entry.entity_id} - 9 - 11 - None" service_calls[0].data["some"]
== f"bat_low device - {entry.entity_id} - 9 - 11 - None"
) )
hass.states.async_set(entry.entity_id, 21) hass.states.async_set(entry.entity_id, 21)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
hass.states.async_set(entry.entity_id, 19) hass.states.async_set(entry.entity_id, 19)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert ( assert (
calls[1].data["some"] == f"bat_low device - {entry.entity_id} - 21 - 19 - None" service_calls[1].data["some"]
== f"bat_low device - {entry.entity_id} - 21 - 19 - None"
) )
@ -674,7 +670,7 @@ async def test_if_fires_on_state_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for value triggers firing.""" """Test for value triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -720,17 +716,18 @@ async def test_if_fires_on_state_legacy(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 9) hass.states.async_set(entry.entity_id, 9)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] == f"bat_low device - {entry.entity_id} - 9 - 11 - None" service_calls[0].data["some"]
== f"bat_low device - {entry.entity_id} - 9 - 11 - None"
) )
@ -739,7 +736,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -786,17 +783,17 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, 10) hass.states.async_set(entry.entity_id, 10)
hass.states.async_set(entry.entity_id, 11) hass.states.async_set(entry.entity_id, 11)
await hass.async_block_till_done() 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)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - 10 - 11 - 0:00:05" == f"turn_off device - {entry.entity_id} - 10 - 11 - 0:00:05"
) )

View File

@ -7,7 +7,7 @@ from homeassistant.components import automation
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.switch import DOMAIN from homeassistant.components.switch import DOMAIN
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import RegistryEntryHider from homeassistant.helpers.entity_registry import RegistryEntryHider
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -24,12 +24,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")
async def test_get_actions( async def test_get_actions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -115,7 +109,6 @@ async def test_action(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off actions.""" """Test for turn_on and turn_off actions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -190,7 +183,6 @@ async def test_action_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off actions.""" """Test for turn_on and turn_off actions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})

View File

@ -20,7 +20,6 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -29,12 +28,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")
async def test_get_conditions( async def test_get_conditions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -183,7 +176,7 @@ async def test_if_state(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -249,20 +242,20 @@ async def test_if_state(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1" assert service_calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(entry.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["some"] == "is_off event - test_event2" assert service_calls[1].data["some"] == "is_off event - test_event2"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -270,7 +263,7 @@ async def test_if_state_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -315,12 +308,12 @@ async def test_if_state_legacy(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1" assert service_calls[0].data["some"] == "is_on event - test_event1"
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
@ -328,7 +321,7 @@ async def test_if_fires_on_for_condition(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for firing if condition is on with delay.""" """Test for firing if condition is on with delay."""
point1 = dt_util.utcnow() point1 = dt_util.utcnow()
@ -377,26 +370,26 @@ async def test_if_fires_on_for_condition(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
# Time travel 10 secs into the future # Time travel 10 secs into the future
freezer.move_to(point2) freezer.move_to(point2)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
# Time travel 20 secs into the future # Time travel 20 secs into the future
freezer.move_to(point3) freezer.move_to(point3)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_off event - test_event1" assert service_calls[0].data["some"] == "is_off event - test_event1"

View File

@ -20,7 +20,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -29,12 +28,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")
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -181,7 +174,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -268,20 +261,20 @@ async def test_if_fires_on_state_change(
}, },
) )
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert {calls[0].data["some"], calls[1].data["some"]} == { assert {service_calls[0].data["some"], service_calls[1].data["some"]} == {
f"turn_off device - {entry.entity_id} - on - off - None", f"turn_off device - {entry.entity_id} - on - off - None",
f"turn_on_or_off device - {entry.entity_id} - on - off - None", f"turn_on_or_off device - {entry.entity_id} - on - off - None",
} }
hass.states.async_set(entry.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(service_calls) == 4
assert {calls[2].data["some"], calls[3].data["some"]} == { assert {service_calls[2].data["some"], service_calls[3].data["some"]} == {
f"turn_on device - {entry.entity_id} - off - on - None", f"turn_on device - {entry.entity_id} - off - on - None",
f"turn_on_or_off device - {entry.entity_id} - off - on - None", f"turn_on_or_off device - {entry.entity_id} - off - on - None",
} }
@ -292,7 +285,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -337,13 +330,13 @@ async def test_if_fires_on_state_change_legacy(
}, },
) )
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - None" == f"turn_off device - {entry.entity_id} - on - off - None"
) )
@ -353,7 +346,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -399,16 +392,16 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() 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.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done() 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)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05" == f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
) )

View File

@ -11,8 +11,6 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import async_mock_service
@pytest.fixture(autouse=True, name="stub_blueprint_populate") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None: def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
@ -39,14 +37,8 @@ def tag_setup(hass: HomeAssistant, hass_storage: dict[str, Any]):
return _storage return _storage
@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")
async def test_triggers( async def test_triggers(
hass: HomeAssistant, tag_setup, calls: list[ServiceCall] hass: HomeAssistant, tag_setup, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test tag triggers.""" """Test tag triggers."""
assert await tag_setup() assert await tag_setup()
@ -75,9 +67,9 @@ async def test_triggers(
await async_scan_tag(hass, "abc123", None) await async_scan_tag(hass, "abc123", None)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["message"] == "service called" assert service_calls[0].data["message"] == "service called"
assert calls[0].data["id"] == 0 assert service_calls[0].data["id"] == 0
await hass.services.async_call( await hass.services.async_call(
automation.DOMAIN, automation.DOMAIN,
@ -85,15 +77,16 @@ async def test_triggers(
{ATTR_ENTITY_ID: "automation.test"}, {ATTR_ENTITY_ID: "automation.test"},
blocking=True, blocking=True,
) )
assert len(service_calls) == 2
await async_scan_tag(hass, "abc123", None) await async_scan_tag(hass, "abc123", None)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 2
async def test_exception_bad_trigger( async def test_exception_bad_trigger(
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test for exception on event triggers firing.""" """Test for exception on event triggers firing."""
@ -117,7 +110,7 @@ async def test_exception_bad_trigger(
async def test_multiple_tags_and_devices_trigger( async def test_multiple_tags_and_devices_trigger(
hass: HomeAssistant, tag_setup, calls: list[ServiceCall] hass: HomeAssistant, tag_setup, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test multiple tags and devices triggers.""" """Test multiple tags and devices triggers."""
assert await tag_setup() assert await tag_setup()
@ -158,8 +151,8 @@ async def test_multiple_tags_and_devices_trigger(
await async_scan_tag(hass, "def456", device_id="jkl0123") await async_scan_tag(hass, "def456", device_id="jkl0123")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(service_calls) == 4
assert calls[0].data["message"] == "service called" assert service_calls[0].data["message"] == "service called"
assert calls[1].data["message"] == "service called" assert service_calls[1].data["message"] == "service called"
assert calls[2].data["message"] == "service called" assert service_calls[2].data["message"] == "service called"
assert calls[3].data["message"] == "service called" assert service_calls[3].data["message"] == "service called"

View File

@ -21,7 +21,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
setup_test_component_platform, setup_test_component_platform,
) )
@ -31,12 +30,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")
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -182,7 +175,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mock_update_entities: list[MockUpdateEntity], mock_update_entities: list[MockUpdateEntity],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
@ -253,21 +246,21 @@ async def test_if_fires_on_state_change(
state = hass.states.get("update.update_available") state = hass.states.get("update.update_available")
assert state assert state
assert state.state == STATE_ON assert state.state == STATE_ON
assert not calls assert not service_calls
hass.states.async_set("update.update_available", STATE_OFF) hass.states.async_set("update.update_available", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== "no_update device - update.update_available - on - off - None" == "no_update device - update.update_available - on - off - None"
) )
hass.states.async_set("update.update_available", STATE_ON) hass.states.async_set("update.update_available", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert ( assert (
calls[1].data["some"] service_calls[1].data["some"]
== "update_available device - update.update_available - off - on - None" == "update_available device - update.update_available - off - on - None"
) )
@ -276,7 +269,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mock_update_entities: list[MockUpdateEntity], mock_update_entities: list[MockUpdateEntity],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
@ -326,13 +319,13 @@ async def test_if_fires_on_state_change_legacy(
state = hass.states.get("update.update_available") state = hass.states.get("update.update_available")
assert state assert state
assert state.state == STATE_ON assert state.state == STATE_ON
assert not calls assert not service_calls
hass.states.async_set("update.update_available", STATE_OFF) hass.states.async_set("update.update_available", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== "no_update device - update.update_available - on - off - None" == "no_update device - update.update_available - on - off - None"
) )
@ -341,7 +334,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mock_update_entities: list[MockUpdateEntity], mock_update_entities: list[MockUpdateEntity],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
@ -392,16 +385,16 @@ async def test_if_fires_on_state_change_with_for(
state = hass.states.get("update.update_available") state = hass.states.get("update.update_available")
assert state assert state
assert state.state == STATE_ON assert state.state == STATE_ON
assert not calls assert not service_calls
hass.states.async_set("update.update_available", STATE_OFF) hass.states.async_set("update.update_available", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert not calls assert not service_calls
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== "turn_off device - update.update_available - on - off - 0:00:05" == "turn_off device - update.update_available - on - off - 0:00:05"
) )

View File

@ -17,11 +17,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import RegistryEntryHider from homeassistant.helpers.entity_registry import RegistryEntryHider
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import MockConfigEntry, async_get_device_automations
MockConfigEntry,
async_get_device_automations,
async_mock_service,
)
@pytest.fixture(autouse=True, name="stub_blueprint_populate") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -29,12 +25,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")
async def test_get_conditions( async def test_get_conditions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -119,7 +109,7 @@ async def test_if_state(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -181,30 +171,30 @@ async def test_if_state(
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_docked - event - test_event2" assert service_calls[0].data["some"] == "is_docked - event - test_event2"
hass.states.async_set(entry.entity_id, STATE_CLEANING) hass.states.async_set(entry.entity_id, STATE_CLEANING)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert calls[1].data["some"] == "is_cleaning - event - test_event1" assert service_calls[1].data["some"] == "is_cleaning - event - test_event1"
# Returning means it's still cleaning # Returning means it's still cleaning
hass.states.async_set(entry.entity_id, STATE_RETURNING) hass.states.async_set(entry.entity_id, STATE_RETURNING)
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 3 assert len(service_calls) == 3
assert calls[2].data["some"] == "is_cleaning - event - test_event1" assert service_calls[2].data["some"] == "is_cleaning - event - test_event1"
async def test_if_state_legacy( async def test_if_state_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -247,5 +237,5 @@ async def test_if_state_legacy(
) )
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert calls[0].data["some"] == "is_cleaning - event - test_event1" assert service_calls[0].data["some"] == "is_cleaning - event - test_event1"

View File

@ -20,7 +20,6 @@ from tests.common import (
async_fire_time_changed, async_fire_time_changed,
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service,
) )
@ -29,12 +28,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")
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -182,7 +175,7 @@ async def test_if_fires_on_state_change(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -247,18 +240,18 @@ async def test_if_fires_on_state_change(
# Fake that the entity is cleaning # Fake that the entity is cleaning
hass.states.async_set(entry.entity_id, STATE_CLEANING) hass.states.async_set(entry.entity_id, STATE_CLEANING)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"cleaning - device - {entry.entity_id} - docked - cleaning" == f"cleaning - device - {entry.entity_id} - docked - cleaning"
) )
# Fake that the entity is docked # Fake that the entity is docked
hass.states.async_set(entry.entity_id, STATE_DOCKED) hass.states.async_set(entry.entity_id, STATE_DOCKED)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
assert ( assert (
calls[1].data["some"] service_calls[1].data["some"]
== f"docked - device - {entry.entity_id} - cleaning - docked" == f"docked - device - {entry.entity_id} - cleaning - docked"
) )
@ -267,7 +260,7 @@ async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -313,9 +306,9 @@ async def test_if_fires_on_state_change_legacy(
# Fake that the entity is cleaning # Fake that the entity is cleaning
hass.states.async_set(entry.entity_id, STATE_CLEANING) hass.states.async_set(entry.entity_id, STATE_CLEANING)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"cleaning - device - {entry.entity_id} - docked - cleaning" == f"cleaning - device - {entry.entity_id} - docked - cleaning"
) )
@ -324,7 +317,7 @@ async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
@ -370,16 +363,16 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
hass.states.async_set(entry.entity_id, STATE_CLEANING) hass.states.async_set(entry.entity_id, STATE_CLEANING)
await hass.async_block_till_done() 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)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
calls[0].data["some"] service_calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - docked - cleaning - 0:00:05" == f"turn_off device - {entry.entity_id} - docked - cleaning - 0:00:05"
) )