init home-assistant
This commit is contained in:
1
home-assistant/tests/components/input_text/__init__.py
Normal file
1
home-assistant/tests/components/input_text/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the input_text component."""
|
||||
532
home-assistant/tests/components/input_text/test_init.py
Normal file
532
home-assistant/tests/components/input_text/test_init.py
Normal file
@@ -0,0 +1,532 @@
|
||||
"""The tests for the Input text component."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.input_text import (
|
||||
ATTR_MAX,
|
||||
ATTR_MIN,
|
||||
ATTR_MODE,
|
||||
ATTR_VALUE,
|
||||
CONF_INITIAL,
|
||||
CONF_MAX_VALUE,
|
||||
CONF_MIN_VALUE,
|
||||
DOMAIN,
|
||||
MODE_TEXT,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_EDITABLE,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_NAME,
|
||||
SERVICE_RELOAD,
|
||||
)
|
||||
from homeassistant.core import Context, CoreState, HomeAssistant, State
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockUser, mock_restore_cache
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
TEST_VAL_MIN = 2
|
||||
TEST_VAL_MAX = 22
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]):
|
||||
"""Storage setup."""
|
||||
|
||||
async def _storage(items=None, config=None):
|
||||
if items is None:
|
||||
hass_storage[DOMAIN] = {
|
||||
"key": DOMAIN,
|
||||
"version": 1,
|
||||
"data": {
|
||||
"items": [
|
||||
{
|
||||
"id": "from_storage",
|
||||
"name": "from storage",
|
||||
"initial": "loaded from storage",
|
||||
ATTR_MAX: TEST_VAL_MAX,
|
||||
ATTR_MIN: TEST_VAL_MIN,
|
||||
ATTR_MODE: MODE_TEXT,
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
else:
|
||||
hass_storage[DOMAIN] = {
|
||||
"key": DOMAIN,
|
||||
"version": 1,
|
||||
"data": {"items": items},
|
||||
}
|
||||
if config is None:
|
||||
config = {DOMAIN: {}}
|
||||
return await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
return _storage
|
||||
|
||||
|
||||
async def async_set_value(hass: HomeAssistant, entity_id: str, value: str) -> None:
|
||||
"""Set input_text to value."""
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_config(hass: HomeAssistant) -> None:
|
||||
"""Test config."""
|
||||
invalid_configs = [
|
||||
None,
|
||||
{},
|
||||
{"name with space": None},
|
||||
{"test_1": {"min": 50, "max": 50}},
|
||||
]
|
||||
for cfg in invalid_configs:
|
||||
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
|
||||
|
||||
|
||||
async def test_set_value(hass: HomeAssistant) -> None:
|
||||
"""Test set_value method."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"test_1": {"initial": "test", "min": 3, "max": 10},
|
||||
"test_2": {},
|
||||
}
|
||||
},
|
||||
)
|
||||
entity_id = "input_text.test_1"
|
||||
entity_id_2 = "input_text.test_2"
|
||||
assert hass.states.get(entity_id).state == "test"
|
||||
assert hass.states.get(entity_id_2).state == "unknown"
|
||||
|
||||
for entity in (entity_id, entity_id_2):
|
||||
await async_set_value(hass, entity, "testing")
|
||||
assert hass.states.get(entity).state == "testing"
|
||||
|
||||
# Too long for entity 1
|
||||
await async_set_value(hass, entity, "testing too long")
|
||||
assert hass.states.get(entity_id).state == "testing"
|
||||
|
||||
# Set to empty string
|
||||
await async_set_value(hass, entity_id_2, "")
|
||||
assert hass.states.get(entity_id_2).state == ""
|
||||
|
||||
|
||||
async def test_mode(hass: HomeAssistant) -> None:
|
||||
"""Test mode settings."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"test_default_text": {"initial": "test", "min": 3, "max": 10},
|
||||
"test_explicit_text": {
|
||||
"initial": "test",
|
||||
"min": 3,
|
||||
"max": 10,
|
||||
"mode": "text",
|
||||
},
|
||||
"test_explicit_password": {
|
||||
"initial": "test",
|
||||
"min": 3,
|
||||
"max": 10,
|
||||
"mode": "password",
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
state = hass.states.get("input_text.test_default_text")
|
||||
assert state
|
||||
assert state.attributes["mode"] == "text"
|
||||
|
||||
state = hass.states.get("input_text.test_explicit_text")
|
||||
assert state
|
||||
assert state.attributes["mode"] == "text"
|
||||
|
||||
state = hass.states.get("input_text.test_explicit_password")
|
||||
assert state
|
||||
assert state.attributes["mode"] == "password"
|
||||
|
||||
|
||||
async def test_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Ensure states are restored on startup."""
|
||||
mock_restore_cache(
|
||||
hass,
|
||||
(State("input_text.b1", "test"), State("input_text.b2", "testing too long")),
|
||||
)
|
||||
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, DOMAIN, {DOMAIN: {"b1": None, "b2": {"min": 0, "max": 10}}}
|
||||
)
|
||||
|
||||
state = hass.states.get("input_text.b1")
|
||||
assert state
|
||||
assert str(state.state) == "test"
|
||||
|
||||
state = hass.states.get("input_text.b2")
|
||||
assert state
|
||||
assert str(state.state) == "unknown"
|
||||
|
||||
|
||||
async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Ensure states are restored on startup."""
|
||||
mock_restore_cache(
|
||||
hass,
|
||||
(State("input_text.b1", "testing"), State("input_text.b2", "testing too long")),
|
||||
)
|
||||
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"b1": {"initial": "test", "min": 0, "max": 10},
|
||||
"b2": {"initial": "test", "min": 0, "max": 10},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
state = hass.states.get("input_text.b1")
|
||||
assert state
|
||||
assert str(state.state) == "test"
|
||||
|
||||
state = hass.states.get("input_text.b2")
|
||||
assert state
|
||||
assert str(state.state) == "test"
|
||||
|
||||
|
||||
async def test_no_initial_state_and_no_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Ensure that entity is create without initial and restore feature."""
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": {"min": 0, "max": 100}}})
|
||||
|
||||
state = hass.states.get("input_text.b1")
|
||||
assert state
|
||||
assert str(state.state) == "unknown"
|
||||
|
||||
|
||||
async def test_input_text_context(
|
||||
hass: HomeAssistant, hass_admin_user: MockUser
|
||||
) -> None:
|
||||
"""Test that input_text context works."""
|
||||
assert await async_setup_component(
|
||||
hass, "input_text", {"input_text": {"t1": {"initial": "bla"}}}
|
||||
)
|
||||
|
||||
state = hass.states.get("input_text.t1")
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call(
|
||||
"input_text",
|
||||
"set_value",
|
||||
{"entity_id": state.entity_id, "value": "new_value"},
|
||||
True,
|
||||
Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
||||
state2 = hass.states.get("input_text.t1")
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
|
||||
|
||||
async def test_config_none(hass: HomeAssistant) -> None:
|
||||
"""Set up input_text without any config."""
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": None}})
|
||||
|
||||
state = hass.states.get("input_text.b1")
|
||||
assert state
|
||||
assert str(state.state) == "unknown"
|
||||
|
||||
# with empty config we still should have the defaults
|
||||
assert state.attributes[ATTR_MODE] == MODE_TEXT
|
||||
assert state.attributes[ATTR_MAX] == CONF_MAX_VALUE
|
||||
assert state.attributes[ATTR_MIN] == CONF_MIN_VALUE
|
||||
|
||||
|
||||
async def test_reload(
|
||||
hass: HomeAssistant, hass_admin_user: MockUser, hass_read_only_user: MockUser
|
||||
) -> None:
|
||||
"""Test reload service."""
|
||||
count_start = len(hass.states.async_entity_ids())
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{DOMAIN: {"test_1": {"initial": "test 1"}, "test_2": {"initial": "test 2"}}},
|
||||
)
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
state_1 = hass.states.get("input_text.test_1")
|
||||
state_2 = hass.states.get("input_text.test_2")
|
||||
state_3 = hass.states.get("input_text.test_3")
|
||||
|
||||
assert state_1 is not None
|
||||
assert state_2 is not None
|
||||
assert state_3 is None
|
||||
assert state_1.state == "test 1"
|
||||
assert state_2.state == "test 2"
|
||||
assert state_1.attributes[ATTR_MIN] == 0
|
||||
assert state_2.attributes[ATTR_MAX] == 100
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
autospec=True,
|
||||
return_value={
|
||||
DOMAIN: {
|
||||
"test_2": {"initial": "test reloaded", ATTR_MIN: 12},
|
||||
"test_3": {"initial": "test 3", ATTR_MAX: 21},
|
||||
}
|
||||
},
|
||||
):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
state_1 = hass.states.get("input_text.test_1")
|
||||
state_2 = hass.states.get("input_text.test_2")
|
||||
state_3 = hass.states.get("input_text.test_3")
|
||||
|
||||
assert state_1 is None
|
||||
assert state_2 is not None
|
||||
assert state_3 is not None
|
||||
assert state_2.attributes[ATTR_MIN] == 12
|
||||
assert state_3.attributes[ATTR_MAX] == 21
|
||||
|
||||
|
||||
async def test_load_from_storage(hass: HomeAssistant, storage_setup) -> None:
|
||||
"""Test set up from storage."""
|
||||
assert await storage_setup()
|
||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||
assert state.state == "loaded from storage"
|
||||
assert state.attributes.get(ATTR_EDITABLE)
|
||||
assert state.attributes[ATTR_MAX] == TEST_VAL_MAX
|
||||
assert state.attributes[ATTR_MIN] == TEST_VAL_MIN
|
||||
|
||||
|
||||
async def test_editable_state_attribute(hass: HomeAssistant, storage_setup) -> None:
|
||||
"""Test editable attribute."""
|
||||
assert await storage_setup(
|
||||
config={
|
||||
DOMAIN: {
|
||||
"from_yaml": {
|
||||
"initial": "yaml initial value",
|
||||
ATTR_MODE: MODE_TEXT,
|
||||
ATTR_MAX: 33,
|
||||
ATTR_MIN: 3,
|
||||
ATTR_NAME: "yaml friendly name",
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||
assert state.state == "loaded from storage"
|
||||
assert state.attributes.get(ATTR_EDITABLE)
|
||||
assert state.attributes[ATTR_MAX] == TEST_VAL_MAX
|
||||
assert state.attributes[ATTR_MIN] == TEST_VAL_MIN
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
||||
assert state.state == "yaml initial value"
|
||||
assert not state.attributes[ATTR_EDITABLE]
|
||||
assert state.attributes[ATTR_MAX] == 33
|
||||
assert state.attributes[ATTR_MIN] == 3
|
||||
|
||||
|
||||
async def test_ws_list(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
|
||||
) -> None:
|
||||
"""Test listing via WS."""
|
||||
assert await storage_setup(
|
||||
config={
|
||||
DOMAIN: {
|
||||
"from_yaml": {
|
||||
"initial": "yaml initial value",
|
||||
ATTR_MODE: MODE_TEXT,
|
||||
ATTR_MAX: 33,
|
||||
ATTR_MIN: 3,
|
||||
ATTR_NAME: "yaml friendly name",
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json({"id": 6, "type": f"{DOMAIN}/list"})
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
storage_ent = "from_storage"
|
||||
yaml_ent = "from_yaml"
|
||||
result = {item["id"]: item for item in resp["result"]}
|
||||
|
||||
assert len(result) == 1
|
||||
assert storage_ent in result
|
||||
assert yaml_ent not in result
|
||||
assert result[storage_ent][ATTR_NAME] == "from storage"
|
||||
|
||||
|
||||
async def test_ws_delete(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
storage_setup,
|
||||
) -> None:
|
||||
"""Test WS delete cleans up entity registry."""
|
||||
assert await storage_setup()
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is not None
|
||||
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{"id": 6, "type": f"{DOMAIN}/delete", f"{DOMAIN}_id": f"{input_id}"}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is None
|
||||
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
|
||||
|
||||
|
||||
async def test_update(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
storage_setup,
|
||||
) -> None:
|
||||
"""Test updating min/max updates the state."""
|
||||
|
||||
assert await storage_setup()
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "from storage"
|
||||
assert state.attributes[ATTR_MODE] == MODE_TEXT
|
||||
assert state.state == "loaded from storage"
|
||||
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
updated_settings = {
|
||||
ATTR_NAME: "even newer name",
|
||||
CONF_INITIAL: "newer option",
|
||||
ATTR_MAX: TEST_VAL_MAX,
|
||||
ATTR_MIN: 6,
|
||||
ATTR_MODE: "password",
|
||||
}
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": f"{DOMAIN}/update",
|
||||
f"{DOMAIN}_id": f"{input_id}",
|
||||
**updated_settings,
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
assert resp["result"] == {"id": "from_storage"} | updated_settings
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state.state == "loaded from storage"
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "even newer name"
|
||||
assert state.attributes[ATTR_MODE] == "password"
|
||||
assert state.attributes[ATTR_MIN] == 6
|
||||
assert state.attributes[ATTR_MAX] == TEST_VAL_MAX
|
||||
|
||||
|
||||
async def test_ws_create(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
storage_setup,
|
||||
) -> None:
|
||||
"""Test create WS."""
|
||||
assert await storage_setup(items=[])
|
||||
|
||||
input_id = "new_input"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is None
|
||||
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": f"{DOMAIN}/create",
|
||||
"name": "New Input",
|
||||
"initial": "even newer option",
|
||||
ATTR_MAX: 44,
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state.state == "even newer option"
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "New Input"
|
||||
assert state.attributes[ATTR_EDITABLE]
|
||||
assert state.attributes[ATTR_MAX] == 44
|
||||
assert state.attributes[ATTR_MIN] == 0
|
||||
|
||||
|
||||
async def test_setup_no_config(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
|
||||
"""Test component setup with no config."""
|
||||
count_start = len(hass.states.async_entity_ids())
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.load_yaml_config_file", autospec=True, return_value={}
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start == len(hass.states.async_entity_ids())
|
||||
56
home-assistant/tests/components/input_text/test_recorder.py
Normal file
56
home-assistant/tests/components/input_text/test_recorder.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""The tests for recorder platform."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.input_text import (
|
||||
ATTR_MAX,
|
||||
ATTR_MIN,
|
||||
ATTR_MODE,
|
||||
ATTR_PATTERN,
|
||||
DOMAIN,
|
||||
MODE_TEXT,
|
||||
)
|
||||
from homeassistant.components.recorder.history import get_significant_states
|
||||
from homeassistant.const import ATTR_EDITABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.recorder.common import async_wait_recording_done
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("recorder_mock", "enable_custom_integrations")
|
||||
async def test_exclude_attributes(hass: HomeAssistant) -> None:
|
||||
"""Test attributes to be excluded."""
|
||||
now = dt_util.utcnow()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {"test": {}}})
|
||||
|
||||
state = hass.states.get("input_text.test")
|
||||
assert state
|
||||
assert state.attributes[ATTR_EDITABLE] is False
|
||||
assert state.attributes[ATTR_MAX] == 100
|
||||
assert state.attributes[ATTR_MIN] == 0
|
||||
assert state.attributes[ATTR_MODE] == MODE_TEXT
|
||||
assert state.attributes[ATTR_PATTERN] is None
|
||||
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
|
||||
await hass.async_block_till_done()
|
||||
await async_wait_recording_done(hass)
|
||||
|
||||
states = await hass.async_add_executor_job(
|
||||
get_significant_states, hass, now, None, hass.states.async_entity_ids()
|
||||
)
|
||||
assert len(states) >= 1
|
||||
for entity_states in states.values():
|
||||
for state in entity_states:
|
||||
assert ATTR_EDITABLE not in state.attributes
|
||||
assert ATTR_MAX not in state.attributes
|
||||
assert ATTR_MIN not in state.attributes
|
||||
assert ATTR_MODE not in state.attributes
|
||||
assert ATTR_PATTERN not in state.attributes
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Test reproduce state for Input text."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.state import async_reproduce_state
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
VALID_TEXT1 = "Test text"
|
||||
VALID_TEXT2 = "LoremIpsum"
|
||||
INVALID_TEXT1 = "This text is too long!"
|
||||
INVALID_TEXT2 = "Short"
|
||||
|
||||
|
||||
async def test_reproducing_states(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test reproducing Input text states."""
|
||||
|
||||
# Setup entity for testing
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"input_text",
|
||||
{
|
||||
"input_text": {
|
||||
"test_text": {"min": "6", "max": "10", "initial": VALID_TEXT1}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# These calls should do nothing as entities already in desired state
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("input_text.test_text", VALID_TEXT1),
|
||||
# Should not raise
|
||||
State("input_text.non_existing", VALID_TEXT1),
|
||||
],
|
||||
)
|
||||
|
||||
# Test that entity is in desired state
|
||||
assert hass.states.get("input_text.test_text").state == VALID_TEXT1
|
||||
|
||||
# Try reproducing with different state
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("input_text.test_text", VALID_TEXT2),
|
||||
# Should not raise
|
||||
State("input_text.non_existing", VALID_TEXT2),
|
||||
],
|
||||
)
|
||||
|
||||
# Test that the state was changed
|
||||
assert hass.states.get("input_text.test_text").state == VALID_TEXT2
|
||||
|
||||
# Test setting state to invalid state (length too long)
|
||||
await async_reproduce_state(hass, [State("input_text.test_text", INVALID_TEXT1)])
|
||||
|
||||
# The entity state should be unchanged
|
||||
assert hass.states.get("input_text.test_text").state == VALID_TEXT2
|
||||
|
||||
# Test setting state to invalid state (length too short)
|
||||
await async_reproduce_state(hass, [State("input_text.test_text", INVALID_TEXT2)])
|
||||
|
||||
# The entity state should be unchanged
|
||||
assert hass.states.get("input_text.test_text").state == VALID_TEXT2
|
||||
Reference in New Issue
Block a user