Use setup_test_component_platform helper for select entity component tests instead of hass.components (#114412)

* Use `setup_test_component_platform` helper for select entity component tests instead of `hass.components`

* Use _values instead of _attr_current_option

* Clean up

* Set default current_option for second mock entity

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Jan-Philipp Benecke 2024-03-29 14:36:33 +01:00 committed by GitHub
parent 3381469076
commit 8d6d70d6b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 66 deletions

View file

@ -0,0 +1,23 @@
"""Common helpers for select entity component tests."""
from homeassistant.components.select import SelectEntity
from tests.common import MockEntity
class MockSelectEntity(MockEntity, SelectEntity):
"""Mock Select class."""
@property
def current_option(self):
"""Return the current option of this select."""
return self._handle("current_option")
@property
def options(self) -> list:
"""Return the list of available options of this select."""
return self._handle("options")
def select_option(self, option: str) -> None:
"""Change the selected option."""
self._values["current_option"] = option

View file

@ -0,0 +1,24 @@
"""Fixtures for the select entity component tests."""
import pytest
from tests.components.select.common import MockSelectEntity
@pytest.fixture
def mock_select_entities() -> list[MockSelectEntity]:
"""Return a list of mock select entities."""
return [
MockSelectEntity(
name="select 1",
unique_id="unique_select_1",
options=["option 1", "option 2", "option 3"],
current_option="option 1",
),
MockSelectEntity(
name="select 2",
unique_id="unique_select_2",
options=["option 1", "option 2", "option 3"],
current_option=None,
),
]

View file

@ -21,6 +21,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError from homeassistant.exceptions import ServiceValidationError
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import setup_test_component_platform
class MockSelectEntity(SelectEntity): class MockSelectEntity(SelectEntity):
"""Mock SelectEntity to use in tests.""" """Mock SelectEntity to use in tests."""
@ -91,11 +93,11 @@ async def test_select(hass: HomeAssistant) -> None:
async def test_custom_integration_and_validation( async def test_custom_integration_and_validation(
hass: HomeAssistant, enable_custom_integrations: None hass: HomeAssistant,
mock_select_entities: list[MockSelectEntity],
) -> None: ) -> None:
"""Test we can only select valid options.""" """Test we can only select valid options."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_select_entities)
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -1,63 +0,0 @@
"""Provide a mock select platform.
Call init before using it in your tests to ensure clean test data.
"""
from homeassistant.components.select import SelectEntity
from tests.common import MockEntity
UNIQUE_SELECT_1 = "unique_select_1"
UNIQUE_SELECT_2 = "unique_select_2"
ENTITIES = []
class MockSelectEntity(MockEntity, SelectEntity):
"""Mock Select class."""
_attr_current_option = None
@property
def current_option(self):
"""Return the current option of this select."""
return self._handle("current_option")
@property
def options(self) -> list:
"""Return the list of available options of this select."""
return self._handle("options")
def select_option(self, option: str) -> None:
"""Change the selected option."""
self._attr_current_option = option
def init(empty=False):
"""Initialize the platform with entities."""
global ENTITIES
ENTITIES = (
[]
if empty
else [
MockSelectEntity(
name="select 1",
unique_id="unique_select_1",
options=["option 1", "option 2", "option 3"],
current_option="option 1",
),
MockSelectEntity(
name="select 2",
unique_id="unique_select_2",
options=["option 1", "option 2", "option 3"],
),
]
)
async def async_setup_platform(
hass, config, async_add_entities_callback, discovery_info=None
):
"""Return mock entities."""
async_add_entities_callback(ENTITIES)