From 71a0a7fe0011db7643fa217782496ccc09c5f0dd Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Thu, 28 Mar 2024 13:56:23 +0100 Subject: [PATCH] Use `setup_test_component_platform` helper for switch entity component tests instead of `hass.components` (#114305) * Use `setup_test_component_platform` helper for switch entity component tests instead of `hass.components` * Do not import fixtures * Re-add switch.py to testing_config as stub * Rename to mock_toggle_entities --- tests/components/conftest.py | 10 +++++++ .../generic_hygrostat/test_humidifier.py | 9 +++--- .../generic_thermostat/test_climate.py | 9 +++--- tests/components/switch/common.py | 13 ++++++++ tests/components/switch/conftest.py | 3 -- tests/components/switch/test_init.py | 9 +++--- .../custom_components/test/switch.py | 30 ++----------------- 7 files changed, 41 insertions(+), 42 deletions(-) delete mode 100644 tests/components/switch/conftest.py diff --git a/tests/components/conftest.py b/tests/components/conftest.py index d84fb3600ab..0831c566666 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -8,6 +8,8 @@ import pytest from homeassistant.const import STATE_OFF, STATE_ON +from tests.common import MockToggleEntity + if TYPE_CHECKING: from tests.components.light.common import MockLight from tests.components.sensor.common import MockSensor @@ -127,3 +129,11 @@ def mock_sensor_entities() -> dict[str, "MockSensor"]: from tests.components.sensor.common import get_mock_sensor_entities return get_mock_sensor_entities() + + +@pytest.fixture +def mock_toggle_entities() -> list[MockToggleEntity]: + """Return mocked toggle entities.""" + from tests.components.switch.common import get_mock_toggle_entities + + return get_mock_toggle_entities() diff --git a/tests/components/generic_hygrostat/test_humidifier.py b/tests/components/generic_hygrostat/test_humidifier.py index fdad20f5b2d..528418b9974 100644 --- a/tests/components/generic_hygrostat/test_humidifier.py +++ b/tests/components/generic_hygrostat/test_humidifier.py @@ -37,9 +37,11 @@ from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.common import ( + MockToggleEntity, assert_setup_component, async_fire_time_changed, mock_restore_cache, + setup_test_component_platform, ) ENTITY = "humidifier.test" @@ -127,12 +129,11 @@ async def test_humidifier_input_boolean(hass: HomeAssistant, setup_comp_1) -> No async def test_humidifier_switch( - hass: HomeAssistant, setup_comp_1, enable_custom_integrations: None + hass: HomeAssistant, setup_comp_1, mock_toggle_entities: list[MockToggleEntity] ) -> None: """Test humidifier switching test switch.""" - platform = getattr(hass.components, "test.switch") - platform.init() - switch_1 = platform.ENTITIES[1] + setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) + switch_1 = mock_toggle_entities[1] assert await async_setup_component( hass, switch.DOMAIN, {"switch": {"platform": "test"}} ) diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index fdcad219d93..f6424f894cf 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -50,11 +50,13 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM from tests.common import ( + MockToggleEntity, assert_setup_component, async_fire_time_changed, async_mock_service, get_fixture_path, mock_restore_cache, + setup_test_component_platform, ) from tests.components.climate import common @@ -140,12 +142,11 @@ async def test_heater_input_boolean(hass: HomeAssistant, setup_comp_1) -> None: async def test_heater_switch( - hass: HomeAssistant, setup_comp_1, enable_custom_integrations: None + hass: HomeAssistant, setup_comp_1, mock_toggle_entities: list[MockToggleEntity] ) -> None: """Test heater switching test switch.""" - platform = getattr(hass.components, "test.switch") - platform.init() - switch_1 = platform.ENTITIES[1] + setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) + switch_1 = mock_toggle_entities[1] assert await async_setup_component( hass, switch.DOMAIN, {"switch": {"platform": "test"}} ) diff --git a/tests/components/switch/common.py b/tests/components/switch/common.py index 60c79fdf6a8..cb30efe47d0 100644 --- a/tests/components/switch/common.py +++ b/tests/components/switch/common.py @@ -10,9 +10,13 @@ from homeassistant.const import ( ENTITY_MATCH_ALL, SERVICE_TURN_OFF, SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, ) from homeassistant.loader import bind_hass +from tests.common import MockToggleEntity + @bind_hass def turn_on(hass, entity_id=ENTITY_MATCH_ALL): @@ -36,3 +40,12 @@ async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL): """Turn all or specified switch off.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True) + + +def get_mock_toggle_entities() -> list[MockToggleEntity]: + """Return a list of mock toggle entities.""" + return [ + MockToggleEntity("AC", STATE_ON), + MockToggleEntity("AC", STATE_OFF), + MockToggleEntity(None, STATE_OFF), + ] diff --git a/tests/components/switch/conftest.py b/tests/components/switch/conftest.py deleted file mode 100644 index c526ef4c4fe..00000000000 --- a/tests/components/switch/conftest.py +++ /dev/null @@ -1,3 +0,0 @@ -"""switch conftest.""" - -from tests.components.light.conftest import mock_light_profiles # noqa: F401 diff --git a/tests/components/switch/test_init.py b/tests/components/switch/test_init.py index 62801346744..28e9d273570 100644 --- a/tests/components/switch/test_init.py +++ b/tests/components/switch/test_init.py @@ -11,18 +11,19 @@ from homeassistant.setup import async_setup_component from . import common from tests.common import ( + MockToggleEntity, MockUser, help_test_all, import_and_test_deprecated_constant_enum, + setup_test_component_platform, ) @pytest.fixture(autouse=True) -def entities(hass): +def entities(hass: HomeAssistant, mock_toggle_entities: list[MockToggleEntity]): """Initialize the test switch.""" - platform = getattr(hass.components, "test.switch") - platform.init() - return platform.ENTITIES + setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) + return mock_toggle_entities async def test_methods( diff --git a/tests/testing_config/custom_components/test/switch.py b/tests/testing_config/custom_components/test/switch.py index 5a2cd7bc17d..b06db33746f 100644 --- a/tests/testing_config/custom_components/test/switch.py +++ b/tests/testing_config/custom_components/test/switch.py @@ -1,32 +1,8 @@ -"""Provide a mock switch platform. - -Call init before using it in your tests to ensure clean test data. -""" - -from homeassistant.const import STATE_OFF, STATE_ON - -from tests.common import MockToggleEntity - -ENTITIES = [] - - -def init(empty=False): - """Initialize the platform with entities.""" - global ENTITIES - - ENTITIES = ( - [] - if empty - else [ - MockToggleEntity("AC", STATE_ON), - MockToggleEntity("AC", STATE_OFF), - MockToggleEntity(None, STATE_OFF), - ] - ) +"""Stub switch platform for translation tests.""" async def async_setup_platform( hass, config, async_add_entities_callback, discovery_info=None ): - """Return mock entities.""" - async_add_entities_callback(ENTITIES) + """Stub setup for translation tests.""" + async_add_entities_callback([])