Skip to content

Commit 8cbfc5a

Browse files
authored
Use service_calls fixture in arcam_fmj tests (#119274)
1 parent b8e57f6 commit 8cbfc5a

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

tests/components/arcam_fmj/test_device_trigger.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,14 @@
99
from homeassistant.helpers import device_registry as dr, entity_registry as er
1010
from homeassistant.setup import async_setup_component
1111

12-
from tests.common import (
13-
MockConfigEntry,
14-
async_get_device_automations,
15-
async_mock_service,
16-
)
12+
from tests.common import MockConfigEntry, async_get_device_automations
1713

1814

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

2319

24-
@pytest.fixture
25-
def calls(hass: HomeAssistant) -> list[ServiceCall]:
26-
"""Track calls to a mock service."""
27-
return async_mock_service(hass, "test", "automation")
28-
29-
3020
async def test_get_triggers(
3121
hass: HomeAssistant,
3222
device_registry: dr.DeviceRegistry,
@@ -69,7 +59,7 @@ async def test_get_triggers(
6959
async def test_if_fires_on_turn_on_request(
7060
hass: HomeAssistant,
7161
entity_registry: er.EntityRegistry,
72-
calls: list[ServiceCall],
62+
service_calls: list[ServiceCall],
7363
player_setup,
7464
state,
7565
) -> None:
@@ -111,15 +101,15 @@ async def test_if_fires_on_turn_on_request(
111101
)
112102

113103
await hass.async_block_till_done()
114-
assert len(calls) == 1
115-
assert calls[0].data["some"] == player_setup
116-
assert calls[0].data["id"] == 0
104+
assert len(service_calls) == 2
105+
assert service_calls[1].data["some"] == player_setup
106+
assert service_calls[1].data["id"] == 0
117107

118108

119109
async def test_if_fires_on_turn_on_request_legacy(
120110
hass: HomeAssistant,
121111
entity_registry: er.EntityRegistry,
122-
calls: list[ServiceCall],
112+
service_calls: list[ServiceCall],
123113
player_setup,
124114
state,
125115
) -> None:
@@ -161,6 +151,6 @@ async def test_if_fires_on_turn_on_request_legacy(
161151
)
162152

163153
await hass.async_block_till_done()
164-
assert len(calls) == 1
165-
assert calls[0].data["some"] == player_setup
166-
assert calls[0].data["id"] == 0
154+
assert len(service_calls) == 2
155+
assert service_calls[1].data["some"] == player_setup
156+
assert service_calls[1].data["id"] == 0

tests/conftest.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
from homeassistant.config import YAML_CONFIG_FILE
5252
from homeassistant.config_entries import ConfigEntries, ConfigEntry, ConfigEntryState
5353
from homeassistant.const import HASSIO_USER_NAME
54-
from homeassistant.core import CoreState, HassJob, HomeAssistant, ServiceCall
54+
from homeassistant.core import (
55+
CoreState,
56+
HassJob,
57+
HomeAssistant,
58+
ServiceCall,
59+
ServiceResponse,
60+
)
5561
from homeassistant.helpers import (
5662
area_registry as ar,
5763
category_registry as cr,
@@ -1776,18 +1782,30 @@ def label_registry(hass: HomeAssistant) -> lr.LabelRegistry:
17761782

17771783

17781784
@pytest.fixture
1779-
def service_calls() -> Generator[None, None, list[ServiceCall]]:
1785+
def service_calls(hass: HomeAssistant) -> Generator[None, None, list[ServiceCall]]:
17801786
"""Track all service calls."""
17811787
calls = []
17821788

1789+
_original_async_call = hass.services.async_call
1790+
17831791
async def _async_call(
17841792
self,
17851793
domain: str,
17861794
service: str,
17871795
service_data: dict[str, Any] | None = None,
17881796
**kwargs: Any,
1789-
):
1797+
) -> ServiceResponse:
17901798
calls.append(ServiceCall(domain, service, service_data))
1799+
try:
1800+
return await _original_async_call(
1801+
domain,
1802+
service,
1803+
service_data,
1804+
**kwargs,
1805+
)
1806+
except ha.ServiceNotFound:
1807+
_LOGGER.debug("Ignoring unknown service call to %s.%s", domain, service)
1808+
return None
17911809

17921810
with patch("homeassistant.core.ServiceRegistry.async_call", _async_call):
17931811
yield calls

0 commit comments

Comments
 (0)