Skip to content

Commit

Permalink
Use service_calls fixture in arcam_fmj tests (#119274)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jun 10, 2024
1 parent b8e57f6 commit 8cbfc5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
28 changes: 9 additions & 19 deletions tests/components/arcam_fmj/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,14 @@
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component

from tests.common import (
MockConfigEntry,
async_get_device_automations,
async_mock_service,
)
from tests.common import MockConfigEntry, async_get_device_automations


@pytest.fixture(autouse=True, name="stub_blueprint_populate")
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
"""Stub copying the blueprints to the config folder."""


@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")


async def test_get_triggers(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
Expand Down Expand Up @@ -69,7 +59,7 @@ async def test_get_triggers(
async def test_if_fires_on_turn_on_request(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
player_setup,
state,
) -> None:
Expand Down Expand Up @@ -111,15 +101,15 @@ async def test_if_fires_on_turn_on_request(
)

await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == player_setup
assert calls[0].data["id"] == 0
assert len(service_calls) == 2
assert service_calls[1].data["some"] == player_setup
assert service_calls[1].data["id"] == 0


async def test_if_fires_on_turn_on_request_legacy(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
calls: list[ServiceCall],
service_calls: list[ServiceCall],
player_setup,
state,
) -> None:
Expand Down Expand Up @@ -161,6 +151,6 @@ async def test_if_fires_on_turn_on_request_legacy(
)

await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == player_setup
assert calls[0].data["id"] == 0
assert len(service_calls) == 2
assert service_calls[1].data["some"] == player_setup
assert service_calls[1].data["id"] == 0
24 changes: 21 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@
from homeassistant.config import YAML_CONFIG_FILE
from homeassistant.config_entries import ConfigEntries, ConfigEntry, ConfigEntryState
from homeassistant.const import HASSIO_USER_NAME
from homeassistant.core import CoreState, HassJob, HomeAssistant, ServiceCall
from homeassistant.core import (
CoreState,
HassJob,
HomeAssistant,
ServiceCall,
ServiceResponse,
)
from homeassistant.helpers import (
area_registry as ar,
category_registry as cr,
Expand Down Expand Up @@ -1776,18 +1782,30 @@ def label_registry(hass: HomeAssistant) -> lr.LabelRegistry:


@pytest.fixture
def service_calls() -> Generator[None, None, list[ServiceCall]]:
def service_calls(hass: HomeAssistant) -> Generator[None, None, list[ServiceCall]]:
"""Track all service calls."""
calls = []

_original_async_call = hass.services.async_call

async def _async_call(
self,
domain: str,
service: str,
service_data: dict[str, Any] | None = None,
**kwargs: Any,
):
) -> ServiceResponse:
calls.append(ServiceCall(domain, service, service_data))
try:
return await _original_async_call(
domain,
service,
service_data,
**kwargs,
)
except ha.ServiceNotFound:
_LOGGER.debug("Ignoring unknown service call to %s.%s", domain, service)
return None

with patch("homeassistant.core.ServiceRegistry.async_call", _async_call):
yield calls
Expand Down

0 comments on commit 8cbfc5a

Please sign in to comment.