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:
parent
3381469076
commit
8d6d70d6b5
4 changed files with 52 additions and 66 deletions
23
tests/components/select/common.py
Normal file
23
tests/components/select/common.py
Normal 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
|
24
tests/components/select/conftest.py
Normal file
24
tests/components/select/conftest.py
Normal 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,
|
||||
),
|
||||
]
|
|
@ -21,6 +21,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import setup_test_component_platform
|
||||
|
||||
|
||||
class MockSelectEntity(SelectEntity):
|
||||
"""Mock SelectEntity to use in tests."""
|
||||
|
@ -91,11 +93,11 @@ async def test_select(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
async def test_custom_integration_and_validation(
|
||||
hass: HomeAssistant, enable_custom_integrations: None
|
||||
hass: HomeAssistant,
|
||||
mock_select_entities: list[MockSelectEntity],
|
||||
) -> None:
|
||||
"""Test we can only select valid options."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_select_entities)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
|
|
@ -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)
|
Loading…
Add table
Reference in a new issue