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
This commit is contained in:
Jan-Philipp Benecke 2024-03-28 13:56:23 +01:00 committed by GitHub
parent a3f251674a
commit 71a0a7fe00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 41 additions and 42 deletions

View File

@ -8,6 +8,8 @@ import pytest
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from tests.common import MockToggleEntity
if TYPE_CHECKING: if TYPE_CHECKING:
from tests.components.light.common import MockLight from tests.components.light.common import MockLight
from tests.components.sensor.common import MockSensor 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 from tests.components.sensor.common import get_mock_sensor_entities
return 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()

View File

@ -37,9 +37,11 @@ from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import ( from tests.common import (
MockToggleEntity,
assert_setup_component, assert_setup_component,
async_fire_time_changed, async_fire_time_changed,
mock_restore_cache, mock_restore_cache,
setup_test_component_platform,
) )
ENTITY = "humidifier.test" ENTITY = "humidifier.test"
@ -127,12 +129,11 @@ async def test_humidifier_input_boolean(hass: HomeAssistant, setup_comp_1) -> No
async def test_humidifier_switch( 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: ) -> None:
"""Test humidifier switching test switch.""" """Test humidifier switching test switch."""
platform = getattr(hass.components, "test.switch") setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities)
platform.init() switch_1 = mock_toggle_entities[1]
switch_1 = platform.ENTITIES[1]
assert await async_setup_component( assert await async_setup_component(
hass, switch.DOMAIN, {"switch": {"platform": "test"}} hass, switch.DOMAIN, {"switch": {"platform": "test"}}
) )

View File

@ -50,11 +50,13 @@ from homeassistant.util import dt as dt_util
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from tests.common import ( from tests.common import (
MockToggleEntity,
assert_setup_component, assert_setup_component,
async_fire_time_changed, async_fire_time_changed,
async_mock_service, async_mock_service,
get_fixture_path, get_fixture_path,
mock_restore_cache, mock_restore_cache,
setup_test_component_platform,
) )
from tests.components.climate import common 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( 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: ) -> None:
"""Test heater switching test switch.""" """Test heater switching test switch."""
platform = getattr(hass.components, "test.switch") setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities)
platform.init() switch_1 = mock_toggle_entities[1]
switch_1 = platform.ENTITIES[1]
assert await async_setup_component( assert await async_setup_component(
hass, switch.DOMAIN, {"switch": {"platform": "test"}} hass, switch.DOMAIN, {"switch": {"platform": "test"}}
) )

View File

@ -10,9 +10,13 @@ from homeassistant.const import (
ENTITY_MATCH_ALL, ENTITY_MATCH_ALL,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
) )
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from tests.common import MockToggleEntity
@bind_hass @bind_hass
def turn_on(hass, entity_id=ENTITY_MATCH_ALL): 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.""" """Turn all or specified switch off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True) 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),
]

View File

@ -1,3 +0,0 @@
"""switch conftest."""
from tests.components.light.conftest import mock_light_profiles # noqa: F401

View File

@ -11,18 +11,19 @@ from homeassistant.setup import async_setup_component
from . import common from . import common
from tests.common import ( from tests.common import (
MockToggleEntity,
MockUser, MockUser,
help_test_all, help_test_all,
import_and_test_deprecated_constant_enum, import_and_test_deprecated_constant_enum,
setup_test_component_platform,
) )
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def entities(hass): def entities(hass: HomeAssistant, mock_toggle_entities: list[MockToggleEntity]):
"""Initialize the test switch.""" """Initialize the test switch."""
platform = getattr(hass.components, "test.switch") setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities)
platform.init() return mock_toggle_entities
return platform.ENTITIES
async def test_methods( async def test_methods(

View File

@ -1,32 +1,8 @@
"""Provide a mock switch platform. """Stub switch platform for translation tests."""
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),
]
)
async def async_setup_platform( async def async_setup_platform(
hass, config, async_add_entities_callback, discovery_info=None hass, config, async_add_entities_callback, discovery_info=None
): ):
"""Return mock entities.""" """Stub setup for translation tests."""
async_add_entities_callback(ENTITIES) async_add_entities_callback([])