Skip to content

Commit

Permalink
Remove mock_entity_platform test helper (home-assistant#104073)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Nov 16, 2023
1 parent 7c030cf commit 4536fb3
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 209 deletions.
12 changes: 0 additions & 12 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,18 +1339,6 @@ def mock_import_platform(platform_name: str) -> NoReturn:
return integration


def mock_entity_platform(
hass: HomeAssistant, platform_path: str, module: MockPlatform | None
) -> None:
"""Mock a entity platform.
platform_path is in form light.hue. Will create platform
hue.light.
"""
domain, platform_name = platform_path.split(".")
mock_platform(hass, f"{platform_name}.{domain}", module)


def mock_platform(
hass: HomeAssistant, platform_path: str, module: Mock | MockPlatform | None = None
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/components/assist_pipeline/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from tests.common import MockConfigEntry, MockPlatform, mock_entity_platform
from tests.common import MockConfigEntry, MockPlatform, mock_platform


class SelectPlatform(MockPlatform):
Expand All @@ -47,7 +47,7 @@ async def async_setup_entry(
@pytest.fixture
async def init_select(hass: HomeAssistant, init_components) -> ConfigEntry:
"""Initialize select entity."""
mock_entity_platform(hass, "select.assist_pipeline", SelectPlatform())
mock_platform(hass, "assist_pipeline.select", SelectPlatform())
config_entry = MockConfigEntry(domain="assist_pipeline")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_forward_entry_setup(config_entry, "select")
Expand Down
32 changes: 16 additions & 16 deletions tests/components/config/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
MockConfigEntry,
MockModule,
MockUser,
mock_entity_platform,
mock_integration,
mock_platform,
)
from tests.typing import WebSocketGenerator

Expand Down Expand Up @@ -304,7 +304,7 @@ async def test_reload_entry_in_setup_retry(
async_migrate_entry=mock_migrate_entry,
),
)
mock_entity_platform(hass, "config_flow.comp", None)
mock_platform(hass, "comp.config_flow", None)
entry = MockConfigEntry(domain="comp", state=core_ce.ConfigEntryState.SETUP_RETRY)
entry.supports_unload = True
entry.add_to_hass(hass)
Expand Down Expand Up @@ -353,7 +353,7 @@ async def test_available_flows(

async def test_initialize_flow(hass: HomeAssistant, client) -> None:
"""Test we can initialize a flow."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
async def async_step_user(self, user_input=None):
Expand Down Expand Up @@ -402,7 +402,7 @@ async def async_step_user(self, user_input=None):

async def test_initialize_flow_unmet_dependency(hass: HomeAssistant, client) -> None:
"""Test unmet dependencies are listed."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

config_schema = vol.Schema({"comp_conf": {"hello": str}}, required=True)
mock_integration(
Expand Down Expand Up @@ -458,7 +458,7 @@ async def async_step_user(self, user_input=None):

async def test_abort(hass: HomeAssistant, client) -> None:
"""Test a flow that aborts."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
async def async_step_user(self, user_input=None):
Expand All @@ -484,7 +484,7 @@ async def test_create_account(
hass: HomeAssistant, client, enable_custom_integrations: None
) -> None:
"""Test a flow that creates an account."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
Expand Down Expand Up @@ -542,7 +542,7 @@ async def test_two_step_flow(
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
VERSION = 1
Expand Down Expand Up @@ -619,7 +619,7 @@ async def test_continue_flow_unauth(
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
VERSION = 1
Expand Down Expand Up @@ -666,7 +666,7 @@ async def test_get_progress_index(
) -> None:
"""Test querying for the flows that are in progress."""
assert await async_setup_component(hass, "config", {})
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)
ws_client = await hass_ws_client(hass)

class TestFlow(core_ce.ConfigFlow):
Expand Down Expand Up @@ -714,7 +714,7 @@ async def test_get_progress_index_unauth(

async def test_get_progress_flow(hass: HomeAssistant, client) -> None:
"""Test we can query the API for same result as we get from init a flow."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
async def async_step_user(self, user_input=None):
Expand Down Expand Up @@ -750,7 +750,7 @@ async def test_get_progress_flow_unauth(
hass: HomeAssistant, client, hass_admin_user: MockUser
) -> None:
"""Test we can can't query the API for result of flow."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
async def async_step_user(self, user_input=None):
Expand Down Expand Up @@ -804,7 +804,7 @@ async def async_step_user(self, user_input=None):
return OptionsFlowHandler()

mock_integration(hass, MockModule("test"))
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)
MockConfigEntry(
domain="test",
entry_id="test1",
Expand Down Expand Up @@ -862,7 +862,7 @@ async def async_step_init(self, user_input=None):
return OptionsFlowHandler()

mock_integration(hass, MockModule("test"))
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)
MockConfigEntry(
domain="test",
entry_id="test1",
Expand All @@ -883,7 +883,7 @@ async def test_two_step_options_flow(hass: HomeAssistant, client) -> None:
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
@staticmethod
Expand Down Expand Up @@ -950,7 +950,7 @@ async def test_options_flow_with_invalid_data(hass: HomeAssistant, client) -> No
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
@staticmethod
Expand Down Expand Up @@ -1265,7 +1265,7 @@ async def test_ignore_flow(
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

class TestFlow(core_ce.ConfigFlow):
VERSION = 1
Expand Down
9 changes: 2 additions & 7 deletions tests/components/hassio/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import (
MockModule,
mock_config_flow,
mock_entity_platform,
mock_integration,
)
from tests.common import MockModule, mock_config_flow, mock_integration, mock_platform
from tests.test_util.aiohttp import AiohttpClientMocker


@pytest.fixture(name="mock_mqtt")
async def mock_mqtt_fixture(hass):
"""Mock the MQTT integration's config flow."""
mock_integration(hass, MockModule(MQTT_DOMAIN))
mock_entity_platform(hass, f"config_flow.{MQTT_DOMAIN}", None)
mock_platform(hass, f"{MQTT_DOMAIN}.config_flow", None)

class MqttFlow(config_entries.ConfigFlow):
"""Test flow."""
Expand Down
6 changes: 3 additions & 3 deletions tests/components/mqtt/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
MockConfigEntry,
async_capture_events,
async_fire_mqtt_message,
mock_entity_platform,
mock_platform,
)
from tests.typing import (
MqttMockHAClientGenerator,
Expand Down Expand Up @@ -1499,7 +1499,7 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
) -> None:
"""Check MQTT integration discovery subscribe and unsubscribe."""
mqtt_mock = await mqtt_mock_entry()
mock_entity_platform(hass, "config_flow.comp", None)
mock_platform(hass, "comp.config_flow", None)

entry = hass.config_entries.async_entries("mqtt")[0]
mqtt_mock().connected = True
Expand Down Expand Up @@ -1552,7 +1552,7 @@ async def test_mqtt_discovery_unsubscribe_once(
) -> None:
"""Check MQTT integration discovery unsubscribe once."""
mqtt_mock = await mqtt_mock_entry()
mock_entity_platform(hass, "config_flow.comp", None)
mock_platform(hass, "comp.config_flow", None)

entry = hass.config_entries.async_entries("mqtt")[0]
mqtt_mock().connected = True
Expand Down
21 changes: 8 additions & 13 deletions tests/helpers/test_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_flow

from tests.common import (
MockConfigEntry,
MockModule,
mock_entity_platform,
mock_integration,
)
from tests.common import MockConfigEntry, MockModule, mock_integration, mock_platform


@pytest.fixture
Expand Down Expand Up @@ -77,7 +72,7 @@ async def test_user_has_confirmation(
) -> None:
"""Test user requires confirmation to setup."""
discovery_flow_conf["discovered"] = True
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

result = await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_USER}, data={}
Expand Down Expand Up @@ -184,7 +179,7 @@ async def test_multiple_discoveries(
hass: HomeAssistant, discovery_flow_conf: dict[str, bool]
) -> None:
"""Test we only create one instance for multiple discoveries."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

result = await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
Expand All @@ -202,7 +197,7 @@ async def test_only_one_in_progress(
hass: HomeAssistant, discovery_flow_conf: dict[str, bool]
) -> None:
"""Test a user initialized one will finish and cancel discovered one."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

# Discovery starts flow
result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -230,7 +225,7 @@ async def test_import_abort_discovery(
hass: HomeAssistant, discovery_flow_conf: dict[str, bool]
) -> None:
"""Test import will finish and cancel discovered one."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

# Discovery starts flow
result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -280,7 +275,7 @@ async def test_ignored_discoveries(
hass: HomeAssistant, discovery_flow_conf: dict[str, bool]
) -> None:
"""Test we can ignore discovered entries."""
mock_entity_platform(hass, "config_flow.test", None)
mock_platform(hass, "test.config_flow", None)

result = await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
Expand Down Expand Up @@ -373,7 +368,7 @@ async def test_webhook_create_cloudhook(
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
),
)
mock_entity_platform(hass, "config_flow.test_single", None)
mock_platform(hass, "test_single.config_flow", None)

result = await hass.config_entries.flow.async_init(
"test_single", context={"source": config_entries.SOURCE_USER}
Expand Down Expand Up @@ -428,7 +423,7 @@ async def test_webhook_create_cloudhook_aborts_not_connected(
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
),
)
mock_entity_platform(hass, "config_flow.test_single", None)
mock_platform(hass, "test_single.config_flow", None)

result = await hass.config_entries.flow.async_init(
"test_single", context={"source": config_entries.SOURCE_USER}
Expand Down
9 changes: 2 additions & 7 deletions tests/helpers/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
from homeassistant.helpers import discovery
from homeassistant.helpers.dispatcher import async_dispatcher_send

from tests.common import (
MockModule,
MockPlatform,
mock_entity_platform,
mock_integration,
)
from tests.common import MockModule, MockPlatform, mock_integration, mock_platform


@pytest.fixture
Expand Down Expand Up @@ -136,7 +131,7 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
# dependencies are only set in component level
# since we are using manifest to hold them
mock_integration(hass, MockModule("test_circular", dependencies=["test_component"]))
mock_entity_platform(hass, "switch.test_circular", MockPlatform(setup_platform))
mock_platform(hass, "test_circular.switch", MockPlatform(setup_platform))

await setup.async_setup_component(
hass,
Expand Down
Loading

0 comments on commit 4536fb3

Please sign in to comment.