Use collection helpers for input_text entities (#30633)
* Refactor input_text to use config dict. * Use collections for input_text. * Update homeassistant/components/input_text/__init__.py Better logging names. Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/input_text/__init__.py Correct artifacts. Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/input_text/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Cleanup. Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
4a66eb0a69
commit
4972b249bf
2 changed files with 385 additions and 62 deletions
|
@ -4,15 +4,71 @@ from unittest.mock import patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.input_text import ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_RELOAD
|
||||
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, State
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_restore_cache
|
||||
|
||||
TEST_VAL_MIN = 2
|
||||
TEST_VAL_MAX = 22
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage_setup(hass, hass_storage):
|
||||
"""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
|
||||
|
||||
|
||||
@bind_hass
|
||||
def set_value(hass, entity_id, value):
|
||||
|
@ -109,7 +165,7 @@ async def test_restore_state(hass):
|
|||
hass.state = CoreState.starting
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, DOMAIN, {DOMAIN: {"b1": None, "b2": {"min": 0, "max": 10}}},
|
||||
hass, DOMAIN, {DOMAIN: {"b1": None, "b2": {"min": 0, "max": 10}}}
|
||||
)
|
||||
|
||||
state = hass.states.get("input_text.b1")
|
||||
|
@ -192,6 +248,11 @@ async def test_config_none(hass):
|
|||
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, hass_admin_user, hass_read_only_user):
|
||||
"""Test reload service."""
|
||||
|
@ -214,14 +275,16 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
|||
assert state_3 is None
|
||||
assert "test 1" == state_1.state
|
||||
assert "test 2" == state_2.state
|
||||
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"},
|
||||
"test_3": {"initial": "test 3"},
|
||||
"test_2": {"initial": "test reloaded", ATTR_MIN: 12},
|
||||
"test_3": {"initial": "test 3", ATTR_MAX: 21},
|
||||
}
|
||||
},
|
||||
):
|
||||
|
@ -250,5 +313,174 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
|||
assert state_1 is None
|
||||
assert state_2 is not None
|
||||
assert state_3 is not None
|
||||
assert "test reloaded" == state_2.state
|
||||
assert "test 3" == state_3.state
|
||||
assert state_2.attributes[ATTR_MIN] == 12
|
||||
assert state_3.attributes[ATTR_MAX] == 21
|
||||
|
||||
|
||||
async def test_load_from_storage(hass, storage_setup):
|
||||
"""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, storage_setup):
|
||||
"""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, hass_ws_client, storage_setup):
|
||||
"""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, hass_ws_client, storage_setup):
|
||||
"""Test WS delete cleans up entity registry."""
|
||||
assert await storage_setup()
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is not None
|
||||
assert ent_reg.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 ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
|
||||
|
||||
|
||||
async def test_update(hass, hass_ws_client, storage_setup):
|
||||
"""Test updating min/max updates the state."""
|
||||
|
||||
assert await storage_setup()
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
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 ent_reg.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}/update",
|
||||
f"{DOMAIN}_id": f"{input_id}",
|
||||
ATTR_NAME: "even newer name",
|
||||
CONF_INITIAL: "newer option",
|
||||
ATTR_MIN: 6,
|
||||
ATTR_MODE: "password",
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
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, hass_ws_client, storage_setup):
|
||||
"""Test create WS."""
|
||||
assert await storage_setup(items=[])
|
||||
|
||||
input_id = "new_input"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is None
|
||||
assert ent_reg.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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue