Use setup_test_component_platform helper for text entity component tests instead of hass.components (#114400)

This commit is contained in:
Jan-Philipp Benecke 2024-03-28 21:08:25 +01:00 committed by GitHub
parent dbbc6914c4
commit 3fd24989c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 129 deletions

View 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

View File

@ -12,7 +12,6 @@ from homeassistant.components.text import (
ATTR_VALUE,
DOMAIN,
SERVICE_SET_VALUE,
TextEntity,
TextMode,
_async_set_value,
)
@ -24,27 +23,9 @@ from homeassistant.setup import async_setup_component
from tests.common import (
async_mock_restore_state_shutdown_restart,
mock_restore_cache_with_extra_data,
setup_test_component_platform,
)
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
from tests.components.text.common import MockRestoreText, MockTextEntity
async def test_text_default(hass: HomeAssistant) -> None:
@ -126,21 +107,16 @@ RESTORE_DATA = {
async def test_restore_number_save_state(
hass: HomeAssistant,
hass_storage: dict[str, Any],
enable_custom_integrations: None,
) -> None:
"""Test RestoreNumber."""
platform = getattr(hass.components, "test.text")
platform.init(empty=True)
platform.ENTITIES.append(
platform.MockRestoreText(
name="Test",
native_max=5,
native_min=1,
native_value="Hello",
)
entity0 = MockRestoreText(
name="Test",
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"}})
await hass.async_block_till_done()
@ -167,7 +143,6 @@ async def test_restore_number_save_state(
)
async def test_restore_number_restore_state(
hass: HomeAssistant,
enable_custom_integrations: None,
hass_storage: dict[str, Any],
native_max,
native_min,
@ -178,18 +153,14 @@ async def test_restore_number_restore_state(
"""Test RestoreNumber."""
mock_restore_cache_with_extra_data(hass, ((State("text.test", ""), extra_data),))
platform = getattr(hass.components, "test.text")
platform.init(empty=True)
platform.ENTITIES.append(
platform.MockRestoreText(
native_max=native_max,
native_min=native_min,
name="Test",
native_value=None,
)
entity0 = MockRestoreText(
native_max=native_max,
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"}})
await hass.async_block_till_done()

View File

@ -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)