Add input_text reload service. (#29644)

* Add input_text reload service.

* Add test.
This commit is contained in:
Alexei Chetroi 2019-12-09 15:15:24 -05:00 committed by Paulus Schoutsen
parent 1222aa8c56
commit 8c1cdc0cf7
3 changed files with 102 additions and 10 deletions

View file

@ -1,10 +1,14 @@
"""The tests for the Input text component."""
# pylint: disable=protected-access
import asyncio
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
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_RELOAD
from homeassistant.core import Context, CoreState, State
from homeassistant.exceptions import Unauthorized
from homeassistant.loader import bind_hass
from homeassistant.setup import async_setup_component
@ -195,3 +199,64 @@ async def test_config_none(hass):
state = hass.states.get("input_text.b1")
assert state
assert str(state.state) == "unknown"
async def test_reload(hass, hass_admin_user, hass_read_only_user):
"""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 "test 1" == state_1.state
assert "test 2" == state_2.state
with patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value={
DOMAIN: {
"test_2": {"initial": "test reloaded"},
"test_3": {"initial": "test 3"},
}
},
):
with patch("homeassistant.config.find_config_file", return_value=""):
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 "test reloaded" == state_2.state
assert "test 3" == state_3.state