Use service_calls fixture in scaffold (#119266)

This commit is contained in:
epenet 2024-06-10 10:44:00 +02:00 committed by GitHub
parent e114e6f862
commit e818de1da8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 34 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pytest_unordered import unordered from pytest_unordered import unordered
from homeassistant.components import automation from homeassistant.components import automation
@ -13,17 +12,7 @@ from homeassistant.core import HomeAssistant, ServiceCall
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.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(
@ -63,7 +52,7 @@ async def test_get_conditions(
assert conditions == unordered(expected_conditions) assert conditions == unordered(expected_conditions)
async def test_if_state(hass: HomeAssistant, calls: list[ServiceCall]) -> None: async def test_if_state(hass: HomeAssistant, service_calls: list[ServiceCall]) -> None:
"""Test for turn_on and turn_off conditions.""" """Test for turn_on and turn_off conditions."""
hass.states.async_set("NEW_DOMAIN.entity", STATE_ON) hass.states.async_set("NEW_DOMAIN.entity", STATE_ON)
@ -114,12 +103,12 @@ async def test_if_state(hass: HomeAssistant, calls: list[ServiceCall]) -> None:
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("NEW_DOMAIN.entity", STATE_OFF) hass.states.async_set("NEW_DOMAIN.entity", 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"

View File

@ -1,6 +1,5 @@
"""The tests for NEW_NAME device triggers.""" """The tests for NEW_NAME device triggers."""
import pytest
from pytest_unordered import unordered from pytest_unordered import unordered
from homeassistant.components import automation from homeassistant.components import automation
@ -11,17 +10,7 @@ from homeassistant.core import HomeAssistant, ServiceCall
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.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(
@ -62,7 +51,7 @@ async def test_get_triggers(
async def test_if_fires_on_state_change( async def test_if_fires_on_state_change(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
hass.states.async_set("NEW_DOMAIN.entity", STATE_OFF) hass.states.async_set("NEW_DOMAIN.entity", STATE_OFF)
@ -119,15 +108,15 @@ 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("NEW_DOMAIN.entity", STATE_ON) hass.states.async_set("NEW_DOMAIN.entity", STATE_ON)
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[ assert service_calls[0].data[
"some" "some"
] == "turn_on - device - {} - off - on - None - 0".format("NEW_DOMAIN.entity") ] == "turn_on - device - {} - off - on - None - 0".format("NEW_DOMAIN.entity")
# Fake that the entity is turning off. # Fake that the entity is turning off.
hass.states.async_set("NEW_DOMAIN.entity", STATE_OFF) hass.states.async_set("NEW_DOMAIN.entity", 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[1].data[ assert service_calls[1].data[
"some" "some"
] == "turn_off - device - {} - on - off - None - 0".format("NEW_DOMAIN.entity") ] == "turn_off - device - {} - on - off - None - 0".format("NEW_DOMAIN.entity")