Add generic test component platform setup function (#114016)
* Add generic entity platform setup fixture * rename fixture * No need for passing hass * Make function instead of fixture * Improve typing * Use Sequence instead * Migrate flux to use the new helper * Use list instead * Use Sequence insteadpull/114198/head
parent
6ceeb1e41f
commit
9b682388f5
|
@ -79,8 +79,10 @@ from homeassistant.helpers.dispatcher import (
|
|||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.json import JSONEncoder, _orjson_default_encoder, json_dumps
|
||||
from homeassistant.helpers.typing import ConfigType, StateType
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.util.async_ import run_callback_threadsafe
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
@ -1646,3 +1648,47 @@ def extract_stack_to_frame(extract_stack: list[Mock]) -> FrameType:
|
|||
current_frame = next_frame
|
||||
|
||||
return top_frame
|
||||
|
||||
|
||||
def setup_test_component_platform(
|
||||
hass: HomeAssistant,
|
||||
domain: str,
|
||||
entities: Sequence[Entity],
|
||||
from_config_entry: bool = False,
|
||||
) -> MockPlatform:
|
||||
"""Mock a test component platform for tests."""
|
||||
|
||||
async def _async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up a test component platform."""
|
||||
async_add_entities(entities)
|
||||
|
||||
platform = MockPlatform(
|
||||
async_setup_platform=_async_setup_platform,
|
||||
)
|
||||
|
||||
# avoid loading config_entry if not needed
|
||||
if from_config_entry:
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
||||
async def _async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a test component platform."""
|
||||
async_add_entities(entities)
|
||||
|
||||
platform.async_setup_entry = _async_setup_entry
|
||||
platform.async_setup_platform = None
|
||||
|
||||
mock_platform(
|
||||
hass,
|
||||
f"test.{domain}",
|
||||
platform,
|
||||
)
|
||||
return platform
|
||||
|
|
|
@ -22,8 +22,9 @@ from tests.common import (
|
|||
async_fire_time_changed,
|
||||
async_mock_service,
|
||||
mock_restore_cache,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.light.common import MockLight, SetupLightPlatformCallable
|
||||
from tests.components.light.common import MockLight
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
@ -138,11 +139,10 @@ async def test_invalid_config_no_lights(hass: HomeAssistant) -> None:
|
|||
|
||||
async def test_flux_when_switch_is_off(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch when it is off."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -191,11 +191,10 @@ async def test_flux_when_switch_is_off(
|
|||
|
||||
async def test_flux_before_sunrise(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch before sunrise."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -252,11 +251,10 @@ async def test_flux_before_sunrise(
|
|||
|
||||
async def test_flux_before_sunrise_known_location(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch before sunrise."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -312,11 +310,10 @@ async def test_flux_before_sunrise_known_location(
|
|||
|
||||
async def test_flux_after_sunrise_before_sunset(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after sunrise and before sunset."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -372,11 +369,10 @@ async def test_flux_after_sunrise_before_sunset(
|
|||
|
||||
async def test_flux_after_sunset_before_stop(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after sunset and before stop."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -433,11 +429,10 @@ async def test_flux_after_sunset_before_stop(
|
|||
|
||||
async def test_flux_after_stop_before_sunrise(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after stop and before sunrise."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -493,11 +488,10 @@ async def test_flux_after_stop_before_sunrise(
|
|||
|
||||
async def test_flux_with_custom_start_stop_times(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux with custom start and stop times."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -555,14 +549,13 @@ async def test_flux_with_custom_start_stop_times(
|
|||
|
||||
async def test_flux_before_sunrise_stop_next_day(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch before sunrise.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -619,14 +612,13 @@ async def test_flux_before_sunrise_stop_next_day(
|
|||
|
||||
async def test_flux_after_sunrise_before_sunset_stop_next_day(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after sunrise and before sunset.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -685,14 +677,13 @@ async def test_flux_after_sunrise_before_sunset_stop_next_day(
|
|||
async def test_flux_after_sunset_before_midnight_stop_next_day(
|
||||
hass: HomeAssistant,
|
||||
x,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after sunset and before stop.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -749,14 +740,13 @@ async def test_flux_after_sunset_before_midnight_stop_next_day(
|
|||
|
||||
async def test_flux_after_sunset_after_midnight_stop_next_day(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after sunset and before stop.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -813,14 +803,13 @@ async def test_flux_after_sunset_after_midnight_stop_next_day(
|
|||
|
||||
async def test_flux_after_stop_before_sunrise_stop_next_day(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch after stop and before sunrise.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -877,11 +866,10 @@ async def test_flux_after_stop_before_sunrise_stop_next_day(
|
|||
|
||||
async def test_flux_with_custom_colortemps(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux with custom start and stop colortemps."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -940,11 +928,10 @@ async def test_flux_with_custom_colortemps(
|
|||
|
||||
async def test_flux_with_custom_brightness(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux with custom start and stop colortemps."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -1002,11 +989,10 @@ async def test_flux_with_custom_brightness(
|
|||
|
||||
async def test_flux_with_multiple_lights(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch with multiple light entities."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -1085,11 +1071,10 @@ async def test_flux_with_multiple_lights(
|
|||
|
||||
async def test_flux_with_mired(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch´s mode mired."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
@ -1144,11 +1129,10 @@ async def test_flux_with_mired(
|
|||
|
||||
async def test_flux_with_rgb(
|
||||
hass: HomeAssistant,
|
||||
setup_light_platform: SetupLightPlatformCallable,
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test the flux switch´s mode rgb."""
|
||||
setup_light_platform(hass, mock_light_entities)
|
||||
setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
|
|
Loading…
Reference in New Issue