Skip to content

Commit

Permalink
Add MockPlatform type hints in tests (home-assistant#120012)
Browse files Browse the repository at this point in the history
* Add MockPlatform type hints in tests

* Remove useless code

* Improve

* Revert "Improve"

This reverts commit 9ad04f9.
  • Loading branch information
epenet authored Jun 21, 2024
1 parent 53022df commit dc6c1f4
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
28 changes: 20 additions & 8 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import asyncio
from collections.abc import Mapping, Sequence
from collections.abc import Callable, Coroutine, Mapping, Sequence
from contextlib import asynccontextmanager, contextmanager
from datetime import UTC, datetime, timedelta
from enum import Enum
Expand Down Expand Up @@ -858,13 +858,25 @@ class MockPlatform:

def __init__(
self,
setup_platform=None,
dependencies=None,
platform_schema=None,
async_setup_platform=None,
async_setup_entry=None,
scan_interval=None,
):
*,
setup_platform: Callable[
[HomeAssistant, ConfigType, AddEntitiesCallback, DiscoveryInfoType | None],
None,
]
| None = None,
dependencies: list[str] | None = None,
platform_schema: vol.Schema | None = None,
async_setup_platform: Callable[
[HomeAssistant, ConfigType, AddEntitiesCallback, DiscoveryInfoType | None],
Coroutine[Any, Any, None],
]
| None = None,
async_setup_entry: Callable[
[HomeAssistant, ConfigEntry, AddEntitiesCallback], Coroutine[Any, Any, None]
]
| None = None,
scan_interval: timedelta | None = None,
) -> None:
"""Initialize the platform."""
self.DEPENDENCIES = dependencies or []

Expand Down
1 change: 0 additions & 1 deletion tests/components/alarm_control_panel/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ async def async_setup_entry_init(
)
return True

MockPlatform(hass, f"{TEST_DOMAIN}.config_flow")
mock_integration(
hass,
MockModule(
Expand Down
1 change: 0 additions & 1 deletion tests/components/lock/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ async def async_setup_entry_init(
)
return True

MockPlatform(hass, f"{TEST_DOMAIN}.config_flow")
mock_integration(
hass,
MockModule(
Expand Down
4 changes: 3 additions & 1 deletion tests/helpers/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ 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_platform(hass, "test_circular.switch", MockPlatform(setup_platform))
mock_platform(
hass, "test_circular.switch", MockPlatform(setup_platform=setup_platform)
)

await setup.async_setup_component(
hass,
Expand Down
24 changes: 17 additions & 7 deletions tests/helpers/test_entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def test_setup_loads_platforms(hass: HomeAssistant) -> None:
mock_integration(hass, MockModule("test_component", setup=component_setup))
# mock the dependencies
mock_integration(hass, MockModule("mod2", dependencies=["test_component"]))
mock_platform(hass, "mod2.test_domain", MockPlatform(platform_setup))
mock_platform(hass, "mod2.test_domain", MockPlatform(setup_platform=platform_setup))

component = EntityComponent(_LOGGER, DOMAIN, hass)

Expand All @@ -71,8 +71,12 @@ async def test_setup_recovers_when_setup_raises(hass: HomeAssistant) -> None:
platform1_setup = Mock(side_effect=Exception("Broken"))
platform2_setup = Mock(return_value=None)

mock_platform(hass, "mod1.test_domain", MockPlatform(platform1_setup))
mock_platform(hass, "mod2.test_domain", MockPlatform(platform2_setup))
mock_platform(
hass, "mod1.test_domain", MockPlatform(setup_platform=platform1_setup)
)
mock_platform(
hass, "mod2.test_domain", MockPlatform(setup_platform=platform2_setup)
)

component = EntityComponent(_LOGGER, DOMAIN, hass)

Expand Down Expand Up @@ -128,7 +132,9 @@ def platform_setup(
"""Test the platform setup."""
add_entities([MockEntity(should_poll=True)])

mock_platform(hass, "platform.test_domain", MockPlatform(platform_setup))
mock_platform(
hass, "platform.test_domain", MockPlatform(setup_platform=platform_setup)
)

component = EntityComponent(_LOGGER, DOMAIN, hass)

Expand All @@ -154,7 +160,7 @@ def platform_setup(
"""Test the platform setup."""
add_entities([MockEntity(name="beer"), MockEntity(name=None)])

platform = MockPlatform(platform_setup)
platform = MockPlatform(setup_platform=platform_setup)

mock_platform(hass, "platform.test_domain", platform)

Expand Down Expand Up @@ -204,7 +210,9 @@ async def test_platform_not_ready(hass: HomeAssistant) -> None:
"""Test that we retry when platform not ready."""
platform1_setup = Mock(side_effect=[PlatformNotReady, PlatformNotReady, None])
mock_integration(hass, MockModule("mod1"))
mock_platform(hass, "mod1.test_domain", MockPlatform(platform1_setup))
mock_platform(
hass, "mod1.test_domain", MockPlatform(setup_platform=platform1_setup)
)

component = EntityComponent(_LOGGER, DOMAIN, hass)

Expand Down Expand Up @@ -678,7 +686,9 @@ async def test_platforms_shutdown_on_stop(hass: HomeAssistant) -> None:
"""Test that we shutdown platforms on stop."""
platform1_setup = Mock(side_effect=[PlatformNotReady, PlatformNotReady, None])
mock_integration(hass, MockModule("mod1"))
mock_platform(hass, "mod1.test_domain", MockPlatform(platform1_setup))
mock_platform(
hass, "mod1.test_domain", MockPlatform(setup_platform=platform1_setup)
)

component = EntityComponent(_LOGGER, DOMAIN, hass)

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def platform_setup(
"""Test the platform setup."""
add_entities([MockEntity(should_poll=True)])

platform = MockPlatform(platform_setup)
platform = MockPlatform(setup_platform=platform_setup)
platform.SCAN_INTERVAL = timedelta(seconds=30)

mock_platform(hass, "platform.test_domain", platform)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def test_validate_platform_config_2(
mock_platform(
hass,
"whatever.platform_conf",
MockPlatform("whatever", platform_schema=platform_schema),
MockPlatform(platform_schema=platform_schema),
)

with assert_setup_component(1):
Expand Down Expand Up @@ -192,7 +192,7 @@ async def test_validate_platform_config_3(
mock_platform(
hass,
"whatever.platform_conf",
MockPlatform("whatever", platform_schema=platform_schema),
MockPlatform(platform_schema=platform_schema),
)

with assert_setup_component(1):
Expand Down

0 comments on commit dc6c1f4

Please sign in to comment.