2017-10-03 19:34:13 +00:00
|
|
|
"""The tests for the Input number component."""
|
2021-01-01 21:31:56 +00:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2019-12-07 20:26:06 +00:00
|
|
|
import pytest
|
2020-02-20 16:30:24 +00:00
|
|
|
import voluptuous as vol
|
2016-03-14 21:03:30 +00:00
|
|
|
|
2017-10-25 13:25:33 +00:00
|
|
|
from homeassistant.components.input_number import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_VALUE,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_DECREMENT,
|
|
|
|
SERVICE_INCREMENT,
|
2019-12-07 20:26:06 +00:00
|
|
|
SERVICE_RELOAD,
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
)
|
2020-01-11 04:27:41 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_EDITABLE,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
ATTR_NAME,
|
|
|
|
)
|
2019-12-07 20:26:06 +00:00
|
|
|
from homeassistant.core import Context, CoreState, State
|
|
|
|
from homeassistant.exceptions import Unauthorized
|
2021-03-09 13:32:08 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2018-11-07 08:56:32 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2017-04-04 16:29:49 +00:00
|
|
|
|
2018-11-07 08:56:32 +00:00
|
|
|
from tests.common import mock_restore_cache
|
2016-09-23 07:12:11 +00:00
|
|
|
|
2016-03-14 21:03:30 +00:00
|
|
|
|
2020-01-11 04:27:41 +00:00
|
|
|
@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",
|
|
|
|
"initial": 10,
|
|
|
|
"name": "from storage",
|
|
|
|
"max": 100,
|
|
|
|
"min": 0,
|
|
|
|
"step": 1,
|
|
|
|
"mode": "slider",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
async def set_value(hass, entity_id, value):
|
2018-09-26 06:50:05 +00:00
|
|
|
"""Set input_number to value.
|
|
|
|
|
|
|
|
This is a legacy helper method. Do not use it for new tests.
|
|
|
|
"""
|
2020-02-20 16:30:24 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value},
|
|
|
|
blocking=True,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-26 06:50:05 +00:00
|
|
|
|
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
async def increment(hass, entity_id):
|
2018-09-26 06:50:05 +00:00
|
|
|
"""Increment value of entity.
|
|
|
|
|
|
|
|
This is a legacy helper method. Do not use it for new tests.
|
|
|
|
"""
|
2020-02-20 16:30:24 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-26 06:50:05 +00:00
|
|
|
|
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
async def decrement(hass, entity_id):
|
2018-09-26 06:50:05 +00:00
|
|
|
"""Decrement value of entity.
|
|
|
|
|
|
|
|
This is a legacy helper method. Do not use it for new tests.
|
|
|
|
"""
|
2020-02-20 16:30:24 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_config(hass):
|
|
|
|
"""Test config."""
|
|
|
|
invalid_configs = [
|
|
|
|
None,
|
|
|
|
{},
|
2019-07-31 19:25:30 +00:00
|
|
|
{"name with space": None},
|
|
|
|
{"test_1": {"min": 50, "max": 50}},
|
2018-11-07 08:56:32 +00:00
|
|
|
]
|
|
|
|
for cfg in invalid_configs:
|
|
|
|
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
|
|
|
|
|
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
async def test_set_value(hass, caplog):
|
2018-11-07 08:56:32 +00:00
|
|
|
"""Test set_value method."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, DOMAIN, {DOMAIN: {"test_1": {"initial": 50, "min": 0, "max": 100}}}
|
|
|
|
)
|
|
|
|
entity_id = "input_number.test_1"
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 50
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await set_value(hass, entity_id, "30.4")
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 30.4
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await set_value(hass, entity_id, "70")
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 70
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
with pytest.raises(vol.Invalid) as excinfo:
|
|
|
|
await set_value(hass, entity_id, "110")
|
|
|
|
|
|
|
|
assert "Invalid value for input_number.test_1: 110.0 (range 0.0 - 100.0)" in str(
|
|
|
|
excinfo.value
|
|
|
|
)
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 70
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_increment(hass):
|
|
|
|
"""Test increment method."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, DOMAIN, {DOMAIN: {"test_2": {"initial": 50, "min": 0, "max": 51}}}
|
|
|
|
)
|
|
|
|
entity_id = "input_number.test_2"
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 50
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await increment(hass, entity_id)
|
2018-11-07 08:56:32 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 51
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await increment(hass, entity_id)
|
2018-11-07 08:56:32 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 51
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
|
2021-04-27 19:48:24 +00:00
|
|
|
async def test_rounding(hass):
|
|
|
|
"""Test increment introducing floating point error is rounded."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{DOMAIN: {"test_2": {"initial": 2.4, "min": 0, "max": 51, "step": 1.2}}},
|
|
|
|
)
|
|
|
|
entity_id = "input_number.test_2"
|
|
|
|
assert 2.4 + 1.2 != 3.6
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
|
|
|
assert float(state.state) == 2.4
|
|
|
|
|
|
|
|
await increment(hass, entity_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
|
|
|
assert float(state.state) == 3.6
|
|
|
|
|
|
|
|
|
2018-11-07 08:56:32 +00:00
|
|
|
async def test_decrement(hass):
|
|
|
|
"""Test decrement method."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, DOMAIN, {DOMAIN: {"test_3": {"initial": 50, "min": 49, "max": 100}}}
|
|
|
|
)
|
|
|
|
entity_id = "input_number.test_3"
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 50
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await decrement(hass, entity_id)
|
2018-11-07 08:56:32 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 49
|
2018-11-07 08:56:32 +00:00
|
|
|
|
2020-02-20 16:30:24 +00:00
|
|
|
await decrement(hass, entity_id)
|
2018-11-07 08:56:32 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state.state) == 49
|
2018-11-07 08:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_mode(hass):
|
|
|
|
"""Test mode settings."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
"test_default_slider": {"min": 0, "max": 100},
|
|
|
|
"test_explicit_box": {"min": 0, "max": 100, "mode": "box"},
|
|
|
|
"test_explicit_slider": {"min": 0, "max": 100, "mode": "slider"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("input_number.test_default_slider")
|
2018-11-07 08:56:32 +00:00
|
|
|
assert state
|
2021-03-20 12:55:10 +00:00
|
|
|
assert state.attributes["mode"] == "slider"
|
2017-10-25 13:25:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("input_number.test_explicit_box")
|
2018-11-07 08:56:32 +00:00
|
|
|
assert state
|
2021-03-20 12:55:10 +00:00
|
|
|
assert state.attributes["mode"] == "box"
|
2017-10-25 13:25:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("input_number.test_explicit_slider")
|
2018-11-07 08:56:32 +00:00
|
|
|
assert state
|
2021-03-20 12:55:10 +00:00
|
|
|
assert state.attributes["mode"] == "slider"
|
2017-10-03 19:34:13 +00:00
|
|
|
|
2017-03-02 16:36:26 +00:00
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
async def test_restore_state(hass):
|
2017-03-02 16:36:26 +00:00
|
|
|
"""Ensure states are restored on startup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_restore_cache(
|
|
|
|
hass, (State("input_number.b1", "70"), State("input_number.b2", "200"))
|
|
|
|
)
|
2017-04-04 16:29:49 +00:00
|
|
|
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
await async_setup_component(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{DOMAIN: {"b1": {"min": 0, "max": 100}, "b2": {"min": 10, "max": 100}}},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("input_number.b1")
|
2017-04-04 16:29:49 +00:00
|
|
|
assert state
|
|
|
|
assert float(state.state) == 70
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("input_number.b2")
|
2017-04-04 16:29:49 +00:00
|
|
|
assert state
|
|
|
|
assert float(state.state) == 10
|
|
|
|
|
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
async def test_initial_state_overrules_restore_state(hass):
|
2017-04-04 16:29:49 +00:00
|
|
|
"""Ensure states are restored on startup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_restore_cache(
|
|
|
|
hass, (State("input_number.b1", "70"), State("input_number.b2", "200"))
|
|
|
|
)
|
2017-03-02 16:36:26 +00:00
|
|
|
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
await async_setup_component(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
"b1": {"initial": 50, "min": 0, "max": 100},
|
|
|
|
"b2": {"initial": 60, "min": 0, "max": 100},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("input_number.b1")
|
2017-03-02 16:36:26 +00:00
|
|
|
assert state
|
2017-04-04 16:29:49 +00:00
|
|
|
assert float(state.state) == 50
|
2017-03-02 16:36:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("input_number.b2")
|
2017-03-02 16:36:26 +00:00
|
|
|
assert state
|
|
|
|
assert float(state.state) == 60
|
2017-04-11 16:23:41 +00:00
|
|
|
|
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
async def test_no_initial_state_and_no_restore_state(hass):
|
2017-04-11 16:23:41 +00:00
|
|
|
"""Ensure that entity is create without initial and restore feature."""
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
|
2020-01-01 00:22:44 +00:00
|
|
|
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": {"min": 0, "max": 100}}})
|
2017-04-11 16:23:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("input_number.b1")
|
2017-04-11 16:23:41 +00:00
|
|
|
assert state
|
|
|
|
assert float(state.state) == 0
|
2018-08-20 15:39:53 +00:00
|
|
|
|
|
|
|
|
2018-11-21 11:26:08 +00:00
|
|
|
async def test_input_number_context(hass, hass_admin_user):
|
2018-08-20 15:39:53 +00:00
|
|
|
"""Test that input_number context works."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, "input_number", {"input_number": {"b1": {"min": 0, "max": 100}}}
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("input_number.b1")
|
2018-08-20 15:39:53 +00:00
|
|
|
assert state is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"input_number",
|
|
|
|
"increment",
|
|
|
|
{"entity_id": state.entity_id},
|
|
|
|
True,
|
|
|
|
Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2018-08-20 15:39:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state2 = hass.states.get("input_number.b1")
|
2018-08-20 15:39:53 +00:00
|
|
|
assert state2 is not None
|
|
|
|
assert state.state != state2.state
|
2018-11-21 11:26:08 +00:00
|
|
|
assert state2.context.user_id == hass_admin_user.id
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
|
|
|
"""Test reload service."""
|
|
|
|
count_start = len(hass.states.async_entity_ids())
|
2021-03-09 13:32:08 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(
|
2020-01-11 04:27:41 +00:00
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
"test_1": {"initial": 50, "min": 0, "max": 51},
|
|
|
|
"test_3": {"initial": 10, "min": 0, "max": 15},
|
|
|
|
}
|
|
|
|
},
|
2019-12-07 20:26:06 +00:00
|
|
|
)
|
|
|
|
|
2020-01-11 04:27:41 +00:00
|
|
|
assert count_start + 2 == len(hass.states.async_entity_ids())
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
state_1 = hass.states.get("input_number.test_1")
|
|
|
|
state_2 = hass.states.get("input_number.test_2")
|
2020-01-11 04:27:41 +00:00
|
|
|
state_3 = hass.states.get("input_number.test_3")
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
assert state_1 is not None
|
|
|
|
assert state_2 is None
|
2020-01-11 04:27:41 +00:00
|
|
|
assert state_3 is not None
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state_1.state) == 50
|
|
|
|
assert float(state_3.state) == 10
|
2020-01-11 04:27:41 +00:00
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
|
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is None
|
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is not None
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config.load_yaml_config_file",
|
|
|
|
autospec=True,
|
|
|
|
return_value={
|
|
|
|
DOMAIN: {
|
|
|
|
"test_1": {"initial": 40, "min": 0, "max": 51},
|
|
|
|
"test_2": {"initial": 20, "min": 10, "max": 30},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
):
|
2020-01-14 21:03:02 +00:00
|
|
|
with pytest.raises(Unauthorized):
|
2019-12-07 20:26:06 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_RELOAD,
|
|
|
|
blocking=True,
|
2020-01-14 21:03:02 +00:00
|
|
|
context=Context(user_id=hass_read_only_user.id),
|
2019-12-07 20:26:06 +00:00
|
|
|
)
|
2020-01-14 21:03:02 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_RELOAD,
|
|
|
|
blocking=True,
|
|
|
|
context=Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
assert count_start + 2 == len(hass.states.async_entity_ids())
|
|
|
|
|
|
|
|
state_1 = hass.states.get("input_number.test_1")
|
|
|
|
state_2 = hass.states.get("input_number.test_2")
|
2020-01-11 04:27:41 +00:00
|
|
|
state_3 = hass.states.get("input_number.test_3")
|
2019-12-07 20:26:06 +00:00
|
|
|
|
|
|
|
assert state_1 is not None
|
|
|
|
assert state_2 is not None
|
2020-01-11 04:27:41 +00:00
|
|
|
assert state_3 is None
|
2021-03-20 12:55:10 +00:00
|
|
|
assert float(state_1.state) == 50
|
|
|
|
assert float(state_2.state) == 20
|
2020-01-11 04:27:41 +00:00
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
|
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
|
|
|
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is None
|
|
|
|
|
|
|
|
|
|
|
|
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 float(state.state) == 10
|
|
|
|
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "from storage"
|
|
|
|
assert state.attributes.get(ATTR_EDITABLE)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_editable_state_attribute(hass, storage_setup):
|
|
|
|
"""Test editable attribute."""
|
|
|
|
assert await storage_setup(
|
|
|
|
config={
|
|
|
|
DOMAIN: {
|
|
|
|
"from_yaml": {
|
|
|
|
"min": 1,
|
|
|
|
"max": 10,
|
|
|
|
"initial": 5,
|
|
|
|
"step": 1,
|
|
|
|
"mode": "slider",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get(f"{DOMAIN}.from_storage")
|
|
|
|
assert float(state.state) == 10
|
|
|
|
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "from storage"
|
|
|
|
assert state.attributes.get(ATTR_EDITABLE)
|
|
|
|
|
|
|
|
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
|
|
|
assert float(state.state) == 5
|
|
|
|
assert not state.attributes.get(ATTR_EDITABLE)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_ws_list(hass, hass_ws_client, storage_setup):
|
|
|
|
"""Test listing via WS."""
|
|
|
|
assert await storage_setup(
|
|
|
|
config={
|
|
|
|
DOMAIN: {
|
|
|
|
"from_yaml": {
|
|
|
|
"min": 1,
|
|
|
|
"max": 10,
|
|
|
|
"initial": 5,
|
|
|
|
"step": 1,
|
|
|
|
"mode": "slider",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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}"
|
2021-03-09 13:32:08 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2020-01-11 04:27:41 +00:00
|
|
|
|
|
|
|
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_min_max(hass, hass_ws_client, storage_setup):
|
|
|
|
"""Test updating min/max updates the state."""
|
|
|
|
|
|
|
|
items = [
|
|
|
|
{
|
|
|
|
"id": "from_storage",
|
|
|
|
"name": "from storage",
|
|
|
|
"max": 100,
|
|
|
|
"min": 0,
|
|
|
|
"step": 1,
|
|
|
|
"mode": "slider",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
assert await storage_setup(items)
|
|
|
|
|
|
|
|
input_id = "from_storage"
|
|
|
|
input_entity_id = f"{DOMAIN}.{input_id}"
|
2021-03-09 13:32:08 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2020-01-11 04:27:41 +00:00
|
|
|
|
|
|
|
state = hass.states.get(input_entity_id)
|
|
|
|
assert state is not None
|
|
|
|
assert state.state
|
|
|
|
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}", "min": 9}
|
|
|
|
)
|
|
|
|
resp = await client.receive_json()
|
|
|
|
assert resp["success"]
|
|
|
|
|
|
|
|
state = hass.states.get(input_entity_id)
|
|
|
|
assert float(state.state) == 9
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": 7,
|
|
|
|
"type": f"{DOMAIN}/update",
|
|
|
|
f"{DOMAIN}_id": f"{input_id}",
|
|
|
|
"max": 5,
|
|
|
|
"min": 0,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
resp = await client.receive_json()
|
|
|
|
assert resp["success"]
|
|
|
|
|
|
|
|
state = hass.states.get(input_entity_id)
|
|
|
|
assert float(state.state) == 5
|
|
|
|
|
|
|
|
|
|
|
|
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}"
|
2021-03-09 13:32:08 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2020-01-11 04:27:41 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
"max": 20,
|
|
|
|
"min": 0,
|
|
|
|
"initial": 10,
|
|
|
|
"step": 1,
|
|
|
|
"mode": "slider",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
resp = await client.receive_json()
|
|
|
|
assert resp["success"]
|
|
|
|
|
|
|
|
state = hass.states.get(input_entity_id)
|
|
|
|
assert float(state.state) == 10
|
2020-01-15 03:15:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_no_config(hass, hass_admin_user):
|
|
|
|
"""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())
|