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

@ -17,11 +17,7 @@ from homeassistant.setup import async_setup_component
from .test_common import help_test_unload_config_entry from .test_common import help_test_unload_config_entry
from tests.common import ( from tests.common import async_fire_mqtt_message, async_get_device_automations
async_fire_mqtt_message,
async_get_device_automations,
async_mock_service,
)
from tests.typing import MqttMockHAClient, MqttMockHAClientGenerator, WebSocketGenerator from tests.typing import MqttMockHAClient, MqttMockHAClientGenerator, WebSocketGenerator
@ -30,12 +26,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,
@ -284,7 +274,7 @@ async def test_update_remove_triggers(
async def test_if_fires_on_mqtt_message( async def test_if_fires_on_mqtt_message(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test triggers firing.""" """Test triggers firing."""
@ -350,20 +340,20 @@ async def test_if_fires_on_mqtt_message(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
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"] == "short_press" assert service_calls[0].data["some"] == "short_press"
# Fake long press. # Fake long press.
async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press") async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press")
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"] == "long_press" assert service_calls[1].data["some"] == "long_press"
async def test_if_discovery_id_is_prefered( async def test_if_discovery_id_is_prefered(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test if discovery is preferred over referencing by type/subtype. """Test if discovery is preferred over referencing by type/subtype.
@ -437,21 +427,21 @@ async def test_if_discovery_id_is_prefered(
# Fake short press, matching on type and subtype # Fake short press, matching on type and subtype
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
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"] == "short_press" assert service_calls[0].data["some"] == "short_press"
# Fake long press, matching on discovery_id # Fake long press, matching on discovery_id
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "foobar/triggers/button1", "long_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "long_press")
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"] == "long_press" assert service_calls[0].data["some"] == "long_press"
async def test_non_unique_triggers( async def test_non_unique_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
@ -528,20 +518,20 @@ async def test_non_unique_triggers(
# and triggers both attached instances. # and triggers both attached instances.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
all_calls = {calls[0].data["some"], calls[1].data["some"]} all_calls = {service_calls[0].data["some"], service_calls[1].data["some"]}
assert all_calls == {"press1", "press2"} assert all_calls == {"press1", "press2"}
# Trigger second config references to same trigger # Trigger second config references to same trigger
# and triggers both attached instances. # and triggers both attached instances.
async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press") async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
all_calls = {calls[0].data["some"], calls[1].data["some"]} all_calls = {service_calls[0].data["some"], service_calls[1].data["some"]}
assert all_calls == {"press1", "press2"} assert all_calls == {"press1", "press2"}
# Removing the first trigger will clean up # Removing the first trigger will clean up
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "") async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done() await hass.async_block_till_done()
@ -549,13 +539,13 @@ async def test_non_unique_triggers(
"Device trigger ('device_automation', 'bla1') has been removed" in caplog.text "Device trigger ('device_automation', 'bla1') has been removed" in caplog.text
) )
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
assert len(calls) == 0 assert len(service_calls) == 0
async def test_if_fires_on_mqtt_message_template( async def test_if_fires_on_mqtt_message_template(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test triggers firing with a message template and a shared topic.""" """Test triggers firing with a message template and a shared topic."""
@ -623,20 +613,20 @@ async def test_if_fires_on_mqtt_message_template(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button4", '{"button":"short_press"}') async_fire_mqtt_message(hass, "foobar/triggers/button4", '{"button":"short_press"}')
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"] == "short_press" assert service_calls[0].data["some"] == "short_press"
# Fake long press. # Fake long press.
async_fire_mqtt_message(hass, "foobar/triggers/button4", '{"button":"long_press"}') async_fire_mqtt_message(hass, "foobar/triggers/button4", '{"button":"long_press"}')
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"] == "long_press" assert service_calls[1].data["some"] == "long_press"
async def test_if_fires_on_mqtt_message_late_discover( async def test_if_fires_on_mqtt_message_late_discover(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test triggers firing of MQTT device triggers discovered after setup.""" """Test triggers firing of MQTT device triggers discovered after setup."""
@ -710,20 +700,20 @@ async def test_if_fires_on_mqtt_message_late_discover(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
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"] == "short_press" assert service_calls[0].data["some"] == "short_press"
# Fake long press. # Fake long press.
async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press") async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press")
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"] == "long_press" assert service_calls[1].data["some"] == "long_press"
async def test_if_fires_on_mqtt_message_after_update( async def test_if_fires_on_mqtt_message_after_update(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
@ -782,7 +772,7 @@ async def test_if_fires_on_mqtt_message_after_update(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "") async_fire_mqtt_message(hass, "foobar/triggers/button1", "")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Update the trigger with existing type/subtype change # Update the trigger with existing type/subtype change
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data1)
@ -793,29 +783,29 @@ async def test_if_fires_on_mqtt_message_after_update(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data3) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data3)
await hass.async_block_till_done() await hass.async_block_till_done()
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "foobar/triggers/button1", "") async_fire_mqtt_message(hass, "foobar/triggers/button1", "")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "foobar/triggers/buttonOne", "") async_fire_mqtt_message(hass, "foobar/triggers/buttonOne", "")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Update the trigger with same topic # Update the trigger with same topic
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data3) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data3)
await hass.async_block_till_done() await hass.async_block_till_done()
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "foobar/triggers/button1", "") async_fire_mqtt_message(hass, "foobar/triggers/button1", "")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
calls.clear() service_calls.clear()
async_fire_mqtt_message(hass, "foobar/triggers/buttonOne", "") async_fire_mqtt_message(hass, "foobar/triggers/buttonOne", "")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_no_resubscribe_same_topic( async def test_no_resubscribe_same_topic(
@ -868,7 +858,7 @@ async def test_no_resubscribe_same_topic(
async def test_not_fires_on_mqtt_message_after_remove_by_mqtt( async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test triggers not firing after removal.""" """Test triggers not firing after removal."""
@ -911,7 +901,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Remove the trigger # Remove the trigger
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "") async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
@ -919,7 +909,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Rediscover the trigger # Rediscover the trigger
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
@ -927,14 +917,14 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2
async def test_not_fires_on_mqtt_message_after_remove_from_registry( async def test_not_fires_on_mqtt_message_after_remove_from_registry(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
calls: list[ServiceCall], service_calls: list[ServiceCall],
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test triggers not firing after removal.""" """Test triggers not firing after removal."""
@ -982,7 +972,7 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry(
# Fake short press. # Fake short press.
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Remove MQTT from the device # Remove MQTT from the device
mqtt_config_entry = hass.config_entries.async_entries(DOMAIN)[0] mqtt_config_entry = hass.config_entries.async_entries(DOMAIN)[0]
@ -994,7 +984,7 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry(
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_attach_remove( async def test_attach_remove(
@ -1684,7 +1674,7 @@ async def test_trigger_debug_info(
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
calls: list[ServiceCall], service_calls: list[ServiceCall],
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock: MqttMockHAClient, mqtt_mock: MqttMockHAClient,
) -> None: ) -> None:
@ -1727,7 +1717,7 @@ async def test_unload_entry(
# Fake short press 1 # Fake short press 1
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
await help_test_unload_config_entry(hass) await help_test_unload_config_entry(hass)
@ -1736,7 +1726,7 @@ async def test_unload_entry(
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
# Start entry again # Start entry again
mqtt_entry = hass.config_entries.async_entries("mqtt")[0] mqtt_entry = hass.config_entries.async_entries("mqtt")[0]
@ -1747,4 +1737,4 @@ async def test_unload_entry(
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(service_calls) == 2

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.core import HassJobType, HomeAssistant, ServiceCall
from homeassistant.setup import async_setup_component 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 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.""" """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) @pytest.fixture(autouse=True)
async def setup_comp( async def setup_comp(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
@ -34,7 +28,7 @@ async def setup_comp(
async def test_if_fires_on_topic_match( async def test_if_fires_on_topic_match(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is fired on topic match.""" """Test if message is fired on topic match."""
assert await async_setup_component( 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" }') async_fire_mqtt_message(hass, "test-topic", '{ "hello": "world" }')
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"] == 'mqtt - test-topic - { "hello": "world" } - world - 0' service_calls[0].data["some"]
== 'mqtt - test-topic - { "hello": "world" } - world - 0'
) )
await hass.services.async_call( await hass.services.async_call(
@ -68,13 +63,15 @@ async def test_if_fires_on_topic_match(
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
blocking=True, blocking=True,
) )
assert len(service_calls) == 2
async_fire_mqtt_message(hass, "test-topic", "test_payload") async_fire_mqtt_message(hass, "test-topic", "test_payload")
await hass.async_block_till_done() 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( async def test_if_fires_on_topic_and_payload_match(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is fired on topic and payload match.""" """Test if message is fired on topic and payload match."""
assert await async_setup_component( 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") async_fire_mqtt_message(hass, "test-topic", "hello")
await hass.async_block_till_done() 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( async def test_if_fires_on_topic_and_payload_match2(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is fired on topic and payload match. """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") async_fire_mqtt_message(hass, "test-topic", "0")
await hass.async_block_till_done() 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( async def test_if_fires_on_templated_topic_and_payload_match(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is fired on templated topic and payload match.""" """Test if message is fired on templated topic and payload match."""
assert await async_setup_component( 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") async_fire_mqtt_message(hass, "test-topic-", "foo")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic-4", "foo") async_fire_mqtt_message(hass, "test-topic-4", "foo")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
async_fire_mqtt_message(hass, "test-topic-4", "bar") async_fire_mqtt_message(hass, "test-topic-4", "bar")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_if_fires_on_payload_template( async def test_if_fires_on_payload_template(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is fired on templated topic and payload match.""" """Test if message is fired on templated topic and payload match."""
assert await async_setup_component( 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") async_fire_mqtt_message(hass, "test-topic", "hello")
await hass.async_block_till_done() 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"}') async_fire_mqtt_message(hass, "test-topic", '{"unwanted_key":"hello"}')
await hass.async_block_till_done() 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"}') async_fire_mqtt_message(hass, "test-topic", '{"wanted_key":"hello"}')
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(service_calls) == 1
async def test_non_allowed_templates( async def test_non_allowed_templates(
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture hass: HomeAssistant,
service_calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test non allowed function in template.""" """Test non allowed function in template."""
assert await async_setup_component( 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( async def test_if_not_fires_on_topic_but_no_payload_match(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, service_calls: list[ServiceCall]
) -> None: ) -> None:
"""Test if message is not fired on topic but no payload.""" """Test if message is not fired on topic but no payload."""
assert await async_setup_component( 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") async_fire_mqtt_message(hass, "test-topic", "no-hello")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 0
async def test_encoding_default( async def test_encoding_default(
hass: HomeAssistant, calls: list[ServiceCall], setup_comp hass: HomeAssistant, service_calls: list[ServiceCall], setup_comp
) -> None: ) -> None:
"""Test default encoding.""" """Test default encoding."""
assert await async_setup_component( assert await async_setup_component(
@ -258,7 +257,7 @@ async def test_encoding_default(
async def test_encoding_custom( async def test_encoding_custom(
hass: HomeAssistant, calls: list[ServiceCall], setup_comp hass: HomeAssistant, service_calls: list[ServiceCall], setup_comp
) -> None: ) -> None:
"""Test default encoding.""" """Test default encoding."""
assert await async_setup_component( assert await async_setup_component(