Use service_calls fixture in mqtt tests (#120984)

This commit is contained in:
epenet 2024-07-02 11:23:46 +02:00 committed by GitHub
parent fac511aa46
commit e3516be3e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 77 additions and 88 deletions

View file

@ -9,7 +9,7 @@ from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_O
from homeassistant.core import HassJobType, HomeAssistant, ServiceCall
from homeassistant.setup import async_setup_component
from tests.common import async_fire_mqtt_message, async_mock_service, mock_component
from tests.common import async_fire_mqtt_message, mock_component
from tests.typing import MqttMockHAClient, MqttMockHAClientGenerator
@ -18,12 +18,6 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
"""Stub copying the blueprints to the config folder."""
@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")
@pytest.fixture(autouse=True)
async def setup_comp(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
@ -34,7 +28,7 @@ async def setup_comp(
async def test_if_fires_on_topic_match(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is fired on topic match."""
assert await async_setup_component(
@ -57,9 +51,10 @@ async def test_if_fires_on_topic_match(
async_fire_mqtt_message(hass, "test-topic", '{ "hello": "world" }')
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
assert (
calls[0].data["some"] == 'mqtt - test-topic - { "hello": "world" } - world - 0'
service_calls[0].data["some"]
== 'mqtt - test-topic - { "hello": "world" } - world - 0'
)
await hass.services.async_call(
@ -68,13 +63,15 @@ async def test_if_fires_on_topic_match(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True,
)
assert len(service_calls) == 2
async_fire_mqtt_message(hass, "test-topic", "test_payload")
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 2
async def test_if_fires_on_topic_and_payload_match(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is fired on topic and payload match."""
assert await async_setup_component(
@ -94,11 +91,11 @@ async def test_if_fires_on_topic_and_payload_match(
async_fire_mqtt_message(hass, "test-topic", "hello")
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
async def test_if_fires_on_topic_and_payload_match2(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is fired on topic and payload match.
@ -121,11 +118,11 @@ async def test_if_fires_on_topic_and_payload_match2(
async_fire_mqtt_message(hass, "test-topic", "0")
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
async def test_if_fires_on_templated_topic_and_payload_match(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is fired on templated topic and payload match."""
assert await async_setup_component(
@ -145,19 +142,19 @@ async def test_if_fires_on_templated_topic_and_payload_match(
async_fire_mqtt_message(hass, "test-topic-", "foo")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic-4", "foo")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic-4", "bar")
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
async def test_if_fires_on_payload_template(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is fired on templated topic and payload match."""
assert await async_setup_component(
@ -178,19 +175,21 @@ async def test_if_fires_on_payload_template(
async_fire_mqtt_message(hass, "test-topic", "hello")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic", '{"unwanted_key":"hello"}')
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic", '{"wanted_key":"hello"}')
await hass.async_block_till_done()
assert len(calls) == 1
assert len(service_calls) == 1
async def test_non_allowed_templates(
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
service_calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test non allowed function in template."""
assert await async_setup_component(
@ -214,7 +213,7 @@ async def test_non_allowed_templates(
async def test_if_not_fires_on_topic_but_no_payload_match(
hass: HomeAssistant, calls: list[ServiceCall]
hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None:
"""Test if message is not fired on topic but no payload."""
assert await async_setup_component(
@ -234,11 +233,11 @@ async def test_if_not_fires_on_topic_but_no_payload_match(
async_fire_mqtt_message(hass, "test-topic", "no-hello")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(service_calls) == 0
async def test_encoding_default(
hass: HomeAssistant, calls: list[ServiceCall], setup_comp
hass: HomeAssistant, service_calls: list[ServiceCall], setup_comp
) -> None:
"""Test default encoding."""
assert await async_setup_component(
@ -258,7 +257,7 @@ async def test_encoding_default(
async def test_encoding_custom(
hass: HomeAssistant, calls: list[ServiceCall], setup_comp
hass: HomeAssistant, service_calls: list[ServiceCall], setup_comp
) -> None:
"""Test default encoding."""
assert await async_setup_component(