mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Use setup_test_component_platform
helper for text entity component tests instead of hass.components
(#114400)
This commit is contained in:
parent
dbbc6914c4
commit
3fd24989c6
45
tests/components/text/common.py
Normal file
45
tests/components/text/common.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
"""Common test helpers for the text entity component tests."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.text import RestoreText, TextEntity
|
||||||
|
|
||||||
|
|
||||||
|
class MockTextEntity(TextEntity):
|
||||||
|
"""Mock text class."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, native_value="test", native_min=None, native_max=None, pattern=None
|
||||||
|
) -> None:
|
||||||
|
"""Initialize mock text entity."""
|
||||||
|
|
||||||
|
self._attr_native_value = native_value
|
||||||
|
if native_min is not None:
|
||||||
|
self._attr_native_min = native_min
|
||||||
|
if native_max is not None:
|
||||||
|
self._attr_native_max = native_max
|
||||||
|
if pattern is not None:
|
||||||
|
self._attr_pattern = pattern
|
||||||
|
|
||||||
|
def set_value(self, value: str) -> None:
|
||||||
|
"""Change the selected option."""
|
||||||
|
self._attr_native_value = value
|
||||||
|
|
||||||
|
|
||||||
|
class MockRestoreText(MockTextEntity, RestoreText):
|
||||||
|
"""Mock RestoreText class."""
|
||||||
|
|
||||||
|
def __init__(self, name: str, **values: Any) -> None:
|
||||||
|
"""Initialize the MockRestoreText."""
|
||||||
|
super().__init__(**values)
|
||||||
|
|
||||||
|
self._attr_name = name
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Restore native_*."""
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
if (last_text_data := await self.async_get_last_text_data()) is None:
|
||||||
|
return
|
||||||
|
self._attr_native_max = last_text_data.native_max
|
||||||
|
self._attr_native_min = last_text_data.native_min
|
||||||
|
self._attr_native_value = last_text_data.native_value
|
@ -12,7 +12,6 @@ from homeassistant.components.text import (
|
|||||||
ATTR_VALUE,
|
ATTR_VALUE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_SET_VALUE,
|
SERVICE_SET_VALUE,
|
||||||
TextEntity,
|
|
||||||
TextMode,
|
TextMode,
|
||||||
_async_set_value,
|
_async_set_value,
|
||||||
)
|
)
|
||||||
@ -24,27 +23,9 @@ from homeassistant.setup import async_setup_component
|
|||||||
from tests.common import (
|
from tests.common import (
|
||||||
async_mock_restore_state_shutdown_restart,
|
async_mock_restore_state_shutdown_restart,
|
||||||
mock_restore_cache_with_extra_data,
|
mock_restore_cache_with_extra_data,
|
||||||
|
setup_test_component_platform,
|
||||||
)
|
)
|
||||||
|
from tests.components.text.common import MockRestoreText, MockTextEntity
|
||||||
|
|
||||||
class MockTextEntity(TextEntity):
|
|
||||||
"""Mock text device to use in tests."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, native_value="test", native_min=None, native_max=None, pattern=None
|
|
||||||
):
|
|
||||||
"""Initialize mock text entity."""
|
|
||||||
self._attr_native_value = native_value
|
|
||||||
if native_min is not None:
|
|
||||||
self._attr_native_min = native_min
|
|
||||||
if native_max is not None:
|
|
||||||
self._attr_native_max = native_max
|
|
||||||
if pattern is not None:
|
|
||||||
self._attr_pattern = pattern
|
|
||||||
|
|
||||||
async def async_set_value(self, value: str) -> None:
|
|
||||||
"""Set the value of the text."""
|
|
||||||
self._attr_native_value = value
|
|
||||||
|
|
||||||
|
|
||||||
async def test_text_default(hass: HomeAssistant) -> None:
|
async def test_text_default(hass: HomeAssistant) -> None:
|
||||||
@ -126,21 +107,16 @@ RESTORE_DATA = {
|
|||||||
async def test_restore_number_save_state(
|
async def test_restore_number_save_state(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_storage: dict[str, Any],
|
hass_storage: dict[str, Any],
|
||||||
enable_custom_integrations: None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RestoreNumber."""
|
"""Test RestoreNumber."""
|
||||||
platform = getattr(hass.components, "test.text")
|
entity0 = MockRestoreText(
|
||||||
platform.init(empty=True)
|
name="Test",
|
||||||
platform.ENTITIES.append(
|
native_max=5,
|
||||||
platform.MockRestoreText(
|
native_min=1,
|
||||||
name="Test",
|
native_value="Hello",
|
||||||
native_max=5,
|
|
||||||
native_min=1,
|
|
||||||
native_value="Hello",
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
||||||
|
|
||||||
entity0 = platform.ENTITIES[0]
|
|
||||||
assert await async_setup_component(hass, "text", {"text": {"platform": "test"}})
|
assert await async_setup_component(hass, "text", {"text": {"platform": "test"}})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -167,7 +143,6 @@ async def test_restore_number_save_state(
|
|||||||
)
|
)
|
||||||
async def test_restore_number_restore_state(
|
async def test_restore_number_restore_state(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
enable_custom_integrations: None,
|
|
||||||
hass_storage: dict[str, Any],
|
hass_storage: dict[str, Any],
|
||||||
native_max,
|
native_max,
|
||||||
native_min,
|
native_min,
|
||||||
@ -178,18 +153,14 @@ async def test_restore_number_restore_state(
|
|||||||
"""Test RestoreNumber."""
|
"""Test RestoreNumber."""
|
||||||
mock_restore_cache_with_extra_data(hass, ((State("text.test", ""), extra_data),))
|
mock_restore_cache_with_extra_data(hass, ((State("text.test", ""), extra_data),))
|
||||||
|
|
||||||
platform = getattr(hass.components, "test.text")
|
entity0 = MockRestoreText(
|
||||||
platform.init(empty=True)
|
native_max=native_max,
|
||||||
platform.ENTITIES.append(
|
native_min=native_min,
|
||||||
platform.MockRestoreText(
|
name="Test",
|
||||||
native_max=native_max,
|
native_value=None,
|
||||||
native_min=native_min,
|
|
||||||
name="Test",
|
|
||||||
native_value=None,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
||||||
|
|
||||||
entity0 = platform.ENTITIES[0]
|
|
||||||
assert await async_setup_component(hass, "text", {"text": {"platform": "test"}})
|
assert await async_setup_component(hass, "text", {"text": {"platform": "test"}})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
@ -1,86 +0,0 @@
|
|||||||
"""Provide a mock text platform.
|
|
||||||
|
|
||||||
Call init before using it in your tests to ensure clean test data.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from homeassistant.components.text import RestoreText, TextEntity, TextMode
|
|
||||||
|
|
||||||
from tests.common import MockEntity
|
|
||||||
|
|
||||||
UNIQUE_TEXT = "unique_text"
|
|
||||||
|
|
||||||
ENTITIES = []
|
|
||||||
|
|
||||||
|
|
||||||
class MockTextEntity(MockEntity, TextEntity):
|
|
||||||
"""Mock text class."""
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_max(self):
|
|
||||||
"""Return the native native_max."""
|
|
||||||
return self._handle("native_max")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_min(self):
|
|
||||||
"""Return the native native_min."""
|
|
||||||
return self._handle("native_min")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def mode(self):
|
|
||||||
"""Return the mode."""
|
|
||||||
return self._handle("mode")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def pattern(self):
|
|
||||||
"""Return the pattern."""
|
|
||||||
return self._handle("pattern")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the native value of this text."""
|
|
||||||
return self._handle("native_value")
|
|
||||||
|
|
||||||
def set_native_value(self, value: str) -> None:
|
|
||||||
"""Change the selected option."""
|
|
||||||
self._values["native_value"] = value
|
|
||||||
|
|
||||||
|
|
||||||
class MockRestoreText(MockTextEntity, RestoreText):
|
|
||||||
"""Mock RestoreText class."""
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
|
||||||
"""Restore native_*."""
|
|
||||||
await super().async_added_to_hass()
|
|
||||||
if (last_text_data := await self.async_get_last_text_data()) is None:
|
|
||||||
return
|
|
||||||
self._values["native_max"] = last_text_data.native_max
|
|
||||||
self._values["native_min"] = last_text_data.native_min
|
|
||||||
self._values["native_value"] = last_text_data.native_value
|
|
||||||
|
|
||||||
|
|
||||||
def init(empty=False):
|
|
||||||
"""Initialize the platform with entities."""
|
|
||||||
global ENTITIES
|
|
||||||
|
|
||||||
ENTITIES = (
|
|
||||||
[]
|
|
||||||
if empty
|
|
||||||
else [
|
|
||||||
MockTextEntity(
|
|
||||||
name="test",
|
|
||||||
native_min=1,
|
|
||||||
native_max=5,
|
|
||||||
mode=TextMode.TEXT,
|
|
||||||
pattern=r"[A-Za-z0-9]",
|
|
||||||
unique_id=UNIQUE_TEXT,
|
|
||||||
native_value="Hello",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
|
||||||
hass, config, async_add_entities_callback, discovery_info=None
|
|
||||||
):
|
|
||||||
"""Return mock entities."""
|
|
||||||
async_add_entities_callback(ENTITIES)
|
|
Loading…
x
Reference in New Issue
Block a user