Add input_select reload service. (#29647)
* Add input_select reload service. * Add test.
This commit is contained in:
parent
8c1cdc0cf7
commit
454cc684e4
3 changed files with 125 additions and 12 deletions
|
@ -1,6 +1,9 @@
|
|||
"""The tests for the Input select component."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.input_select import (
|
||||
ATTR_OPTION,
|
||||
|
@ -11,8 +14,14 @@ from homeassistant.components.input_select import (
|
|||
SERVICE_SELECT_PREVIOUS,
|
||||
SERVICE_SET_OPTIONS,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_ICON,
|
||||
SERVICE_RELOAD,
|
||||
)
|
||||
from homeassistant.core import Context, State
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
@ -322,3 +331,81 @@ async def test_input_select_context(hass, hass_admin_user):
|
|||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
|
||||
|
||||
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": {
|
||||
"options": ["first option", "middle option", "last option"],
|
||||
"initial": "middle option",
|
||||
},
|
||||
"test_2": {
|
||||
"options": ["an option", "not an option"],
|
||||
"initial": "an option",
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
state_1 = hass.states.get("input_select.test_1")
|
||||
state_2 = hass.states.get("input_select.test_2")
|
||||
state_3 = hass.states.get("input_select.test_3")
|
||||
|
||||
assert state_1 is not None
|
||||
assert state_2 is not None
|
||||
assert state_3 is None
|
||||
assert "middle option" == state_1.state
|
||||
assert "an option" == state_2.state
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
autospec=True,
|
||||
return_value={
|
||||
DOMAIN: {
|
||||
"test_2": {
|
||||
"options": ["an option", "reloaded option"],
|
||||
"initial": "reloaded option",
|
||||
},
|
||||
"test_3": {
|
||||
"options": ["new option", "newer option"],
|
||||
"initial": "newer option",
|
||||
},
|
||||
}
|
||||
},
|
||||
):
|
||||
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_select.test_1")
|
||||
state_2 = hass.states.get("input_select.test_2")
|
||||
state_3 = hass.states.get("input_select.test_3")
|
||||
|
||||
assert state_1 is None
|
||||
assert state_2 is not None
|
||||
assert state_3 is not None
|
||||
assert "reloaded option" == state_2.state
|
||||
assert "newer option" == state_3.state
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue