Skip to content

Commit

Permalink
Prepare for refactoring of MQTT related tests (home-assistant#88557)
Browse files Browse the repository at this point in the history
* Update mqtt_mock

* Tests manual_mqtt

* Tests mqtt_json

* Tests mqtt_room
  • Loading branch information
jbouwh authored Feb 27, 2023
1 parent b542f6b commit b25f6e3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
65 changes: 32 additions & 33 deletions tests/components/manual_mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
async_fire_time_changed,
)
from tests.components.alarm_control_panel import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClient

CODE = "HELLO_CODE"


async def test_fail_setup_without_state_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test for failing with no state topic."""
with assert_setup_component(0, alarm_control_panel.DOMAIN) as config:
Expand All @@ -57,7 +57,7 @@ async def test_fail_setup_without_state_topic(


async def test_fail_setup_without_command_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test failing with no command topic."""
with assert_setup_component(0, alarm_control_panel.DOMAIN):
Expand Down Expand Up @@ -87,7 +87,7 @@ async def test_no_pending(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Test arm method."""
assert await async_setup_component(
Expand Down Expand Up @@ -135,7 +135,7 @@ async def test_no_pending_when_code_not_req(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Test arm method."""
assert await async_setup_component(
Expand Down Expand Up @@ -184,7 +184,7 @@ async def test_with_pending(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Test arm method."""
assert await async_setup_component(
Expand Down Expand Up @@ -256,7 +256,7 @@ async def test_with_invalid_code(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Attempt to arm without a valid code."""
assert await async_setup_component(
Expand Down Expand Up @@ -304,7 +304,7 @@ async def test_with_template_code(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Attempt to arm with a template-based code."""
assert await async_setup_component(
Expand Down Expand Up @@ -353,7 +353,7 @@ async def test_with_specific_pending(
hass: HomeAssistant,
service,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Test arm method."""
assert await async_setup_component(
Expand Down Expand Up @@ -395,7 +395,7 @@ async def test_with_specific_pending(


async def test_trigger_no_pending(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test triggering when no pending submitted method."""
assert await async_setup_component(
Expand Down Expand Up @@ -435,7 +435,7 @@ async def test_trigger_no_pending(


async def test_trigger_with_delay(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method and switch from pending to triggered."""
assert await async_setup_component(
Expand Down Expand Up @@ -483,7 +483,7 @@ async def test_trigger_with_delay(


async def test_trigger_zero_trigger_time(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disabled trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -513,7 +513,7 @@ async def test_trigger_zero_trigger_time(


async def test_trigger_zero_trigger_time_with_pending(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disabled trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -543,7 +543,7 @@ async def test_trigger_zero_trigger_time_with_pending(


async def test_trigger_with_pending(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test arm home method."""
assert await async_setup_component(
Expand Down Expand Up @@ -596,7 +596,7 @@ async def test_trigger_with_pending(


async def test_trigger_with_disarm_after_trigger(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarm after trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -636,7 +636,7 @@ async def test_trigger_with_disarm_after_trigger(


async def test_trigger_with_zero_specific_trigger_time(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method."""
assert await async_setup_component(
Expand Down Expand Up @@ -667,7 +667,7 @@ async def test_trigger_with_zero_specific_trigger_time(


async def test_trigger_with_unused_zero_specific_trigger_time(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarm after trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -708,7 +708,7 @@ async def test_trigger_with_unused_zero_specific_trigger_time(


async def test_trigger_with_specific_trigger_time(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarm after trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -748,7 +748,7 @@ async def test_trigger_with_specific_trigger_time(


async def test_back_to_back_trigger_with_no_disarm_after_trigger(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test no disarm after back to back trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -806,7 +806,7 @@ async def test_back_to_back_trigger_with_no_disarm_after_trigger(


async def test_disarm_while_pending_trigger(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarming while pending state."""
assert await async_setup_component(
Expand Down Expand Up @@ -849,7 +849,7 @@ async def test_disarm_while_pending_trigger(


async def test_disarm_during_trigger_with_invalid_code(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarming while code is invalid."""
assert await async_setup_component(
Expand Down Expand Up @@ -897,7 +897,7 @@ async def test_disarm_during_trigger_with_invalid_code(


async def test_trigger_with_unused_specific_delay(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method and switch from pending to triggered."""
assert await async_setup_component(
Expand Down Expand Up @@ -946,7 +946,7 @@ async def test_trigger_with_unused_specific_delay(


async def test_trigger_with_specific_delay(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method and switch from pending to triggered."""
assert await async_setup_component(
Expand Down Expand Up @@ -995,7 +995,7 @@ async def test_trigger_with_specific_delay(


async def test_trigger_with_pending_and_delay(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method and switch from pending to triggered."""
assert await async_setup_component(
Expand Down Expand Up @@ -1056,7 +1056,7 @@ async def test_trigger_with_pending_and_delay(


async def test_trigger_with_pending_and_specific_delay(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test trigger method and switch from pending to triggered."""
assert await async_setup_component(
Expand Down Expand Up @@ -1118,7 +1118,7 @@ async def test_trigger_with_pending_and_specific_delay(


async def test_trigger_with_specific_pending(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test arm home method."""
assert await async_setup_component(
Expand Down Expand Up @@ -1167,7 +1167,7 @@ async def test_trigger_with_specific_pending(


async def test_trigger_with_no_disarm_after_trigger(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarm after trigger."""
assert await async_setup_component(
Expand Down Expand Up @@ -1212,7 +1212,7 @@ async def test_trigger_with_no_disarm_after_trigger(


async def test_arm_away_after_disabled_disarmed(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test pending state with and without zero trigger time."""
assert await async_setup_component(
Expand Down Expand Up @@ -1278,7 +1278,7 @@ async def test_arm_away_after_disabled_disarmed(


async def test_disarm_with_template_code(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Attempt to disarm with a valid or invalid template-based code."""
assert await async_setup_component(
Expand Down Expand Up @@ -1332,7 +1332,7 @@ async def test_arm_via_command_topic(
hass: HomeAssistant,
config,
expected_state,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock: MqttMockHAClient,
) -> None:
"""Test arming via command topic."""
command = config[8:].upper()
Expand Down Expand Up @@ -1374,7 +1374,7 @@ async def test_arm_via_command_topic(


async def test_disarm_pending_via_command_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test disarming pending alarm via command topic."""
assert await async_setup_component(
Expand Down Expand Up @@ -1410,7 +1410,7 @@ async def test_disarm_pending_via_command_topic(


async def test_state_changes_are_published_to_mqtt(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test publishing of MQTT messages when state changes."""
assert await async_setup_component(
Expand All @@ -1431,7 +1431,6 @@ async def test_state_changes_are_published_to_mqtt(

# Component should send disarmed alarm state on startup
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
mqtt_mock.async_publish.assert_called_once_with(
"alarm/state", STATE_ALARM_DISARMED, 0, True
)
Expand Down
6 changes: 5 additions & 1 deletion tests/components/mqtt_json/test_device_tracker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The tests for the JSON MQTT device tracker platform."""
from collections.abc import Generator
import json
import logging
import os
Expand All @@ -15,6 +16,7 @@
from homeassistant.setup import async_setup_component

from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClient

LOCATION_MESSAGE = {
"longitude": 1.0,
Expand All @@ -27,7 +29,9 @@


@pytest.fixture(autouse=True)
async def setup_comp(hass, mqtt_mock_entry_with_yaml_config):
async def setup_comp(
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> Generator[None, None, None]:
"""Initialize components."""
yaml_devices = hass.config.path(YAML_DEVICES)
yield
Expand Down
8 changes: 3 additions & 5 deletions tests/components/mqtt_room/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.util import dt

from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClient

DEVICE_ID = "123TESTMAC"
NAME = "test_device"
Expand Down Expand Up @@ -56,9 +56,7 @@ async def assert_distance(hass, distance):
assert state.attributes.get("distance") == distance


async def test_room_update(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
async def test_room_update(hass: HomeAssistant, mqtt_mock: MqttMockHAClient) -> None:
"""Test the updating between rooms."""
assert await async_setup_component(
hass,
Expand Down Expand Up @@ -96,7 +94,7 @@ async def test_room_update(


async def test_unique_id_is_set(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None:
"""Test the updating between rooms."""
unique_name = "my_unique_name_0123456789"
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,14 @@ def _unsubscribe(topic):
@pytest.fixture
async def mqtt_mock(
hass: HomeAssistant,
mock_hass_config: None,
mqtt_client_mock: MqttMockPahoClient,
mqtt_config_entry_data: dict[str, Any] | None,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> AsyncGenerator[MqttMockHAClient, None]:
"""Fixture to mock MQTT component."""
return await mqtt_mock_entry_no_yaml_config()
with patch("homeassistant.components.mqtt.PLATFORMS", []):
return await mqtt_mock_entry_no_yaml_config()


@asynccontextmanager
Expand Down

0 comments on commit b25f6e3

Please sign in to comment.