hass-core/tests/components/input_boolean/test_init.py

235 lines
6.6 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the input_boolean component."""
# pylint: disable=protected-access
import logging
from unittest.mock import patch
2016-01-14 22:51:28 -08:00
from homeassistant.components.input_boolean import CONF_INITIAL, DOMAIN, is_on
2016-01-14 22:51:28 -08:00
from homeassistant.const import (
2019-07-31 12:25:30 -07:00
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
SERVICE_RELOAD,
2019-07-31 12:25:30 -07:00
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
2019-07-31 12:25:30 -07:00
)
from homeassistant.core import Context, CoreState, State
from homeassistant.setup import async_setup_component
from tests.common import mock_component, mock_restore_cache
2016-01-14 22:51:28 -08:00
_LOGGER = logging.getLogger(__name__)
2016-01-14 22:51:28 -08:00
async def test_config(hass):
"""Test config."""
2019-07-31 12:25:30 -07:00
invalid_configs = [None, 1, {}, {"name with space": None}]
for cfg in invalid_configs:
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
async def test_methods(hass):
"""Test is_on, turn_on, turn_off methods."""
2019-07-31 12:25:30 -07:00
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {"test_1": None}})
entity_id = "input_boolean.test_1"
assert not is_on(hass, entity_id)
2019-07-31 12:25:30 -07:00
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
await hass.async_block_till_done()
assert is_on(hass, entity_id)
await hass.services.async_call(
2019-07-31 12:25:30 -07:00
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}
)
await hass.async_block_till_done()
2016-01-14 22:51:28 -08:00
assert not is_on(hass, entity_id)
2016-01-14 22:51:28 -08:00
2019-07-31 12:25:30 -07:00
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
2016-01-14 22:51:28 -08:00
await hass.async_block_till_done()
2016-01-14 22:51:28 -08:00
assert is_on(hass, entity_id)
2016-01-14 22:51:28 -08:00
async def test_config_options(hass):
"""Test configuration options."""
count_start = len(hass.states.async_entity_ids())
2016-01-14 22:51:28 -08:00
2019-07-31 12:25:30 -07:00
_LOGGER.debug("ENTITIES @ start: %s", hass.states.async_entity_ids())
2016-01-14 22:51:28 -08:00
2019-07-31 12:25:30 -07:00
assert await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
"test_1": None,
"test_2": {"name": "Hello World", "icon": "mdi:work", "initial": True},
}
},
2019-07-31 12:25:30 -07:00
)
2019-07-31 12:25:30 -07:00
_LOGGER.debug("ENTITIES: %s", hass.states.async_entity_ids())
2016-01-14 22:51:28 -08:00
assert count_start + 2 == len(hass.states.async_entity_ids())
2016-01-14 22:51:28 -08:00
2019-07-31 12:25:30 -07:00
state_1 = hass.states.get("input_boolean.test_1")
state_2 = hass.states.get("input_boolean.test_2")
2016-01-14 22:51:28 -08:00
assert state_1 is not None
assert state_2 is not None
2016-01-14 22:51:28 -08:00
assert STATE_OFF == state_1.state
assert ATTR_ICON not in state_1.attributes
assert ATTR_FRIENDLY_NAME not in state_1.attributes
2016-01-14 22:51:28 -08:00
assert STATE_ON == state_2.state
2019-07-31 12:25:30 -07:00
assert "Hello World" == state_2.attributes.get(ATTR_FRIENDLY_NAME)
assert "mdi:work" == state_2.attributes.get(ATTR_ICON)
async def test_restore_state(hass):
"""Ensure states are restored on startup."""
2019-07-31 12:25:30 -07:00
mock_restore_cache(
hass,
(
State("input_boolean.b1", "on"),
State("input_boolean.b2", "off"),
State("input_boolean.b3", "on"),
),
)
hass.state = CoreState.starting
2019-07-31 12:25:30 -07:00
mock_component(hass, "recorder")
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": None, "b2": None}})
2019-07-31 12:25:30 -07:00
state = hass.states.get("input_boolean.b1")
assert state
2019-07-31 12:25:30 -07:00
assert state.state == "on"
2019-07-31 12:25:30 -07:00
state = hass.states.get("input_boolean.b2")
assert state
2019-07-31 12:25:30 -07:00
assert state.state == "off"
async def test_initial_state_overrules_restore_state(hass):
"""Ensure states are restored on startup."""
2019-07-31 12:25:30 -07:00
mock_restore_cache(
hass, (State("input_boolean.b1", "on"), State("input_boolean.b2", "off"))
)
hass.state = CoreState.starting
await async_setup_component(
2019-07-31 12:25:30 -07:00
hass,
DOMAIN,
{DOMAIN: {"b1": {CONF_INITIAL: False}, "b2": {CONF_INITIAL: True}}},
)
2019-07-31 12:25:30 -07:00
state = hass.states.get("input_boolean.b1")
assert state
2019-07-31 12:25:30 -07:00
assert state.state == "off"
2019-07-31 12:25:30 -07:00
state = hass.states.get("input_boolean.b2")
assert state
2019-07-31 12:25:30 -07:00
assert state.state == "on"
async def test_input_boolean_context(hass, hass_admin_user):
"""Test that input_boolean context works."""
2019-07-31 12:25:30 -07:00
assert await async_setup_component(
hass, "input_boolean", {"input_boolean": {"ac": {CONF_INITIAL: True}}}
)
2019-07-31 12:25:30 -07:00
state = hass.states.get("input_boolean.ac")
assert state is not None
2019-07-31 12:25:30 -07:00
await hass.services.async_call(
"input_boolean",
"turn_off",
{"entity_id": state.entity_id},
True,
Context(user_id=hass_admin_user.id),
)
state2 = hass.states.get("input_boolean.ac")
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):
"""Test reload service."""
count_start = len(hass.states.async_entity_ids())
_LOGGER.debug("ENTITIES @ start: %s", hass.states.async_entity_ids())
assert await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
"test_1": None,
"test_2": {"name": "Hello World", "icon": "mdi:work", "initial": True},
}
},
)
_LOGGER.debug("ENTITIES: %s", hass.states.async_entity_ids())
assert count_start + 2 == len(hass.states.async_entity_ids())
state_1 = hass.states.get("input_boolean.test_1")
state_2 = hass.states.get("input_boolean.test_2")
state_3 = hass.states.get("input_boolean.test_3")
assert state_1 is not None
assert state_2 is not None
assert state_3 is None
assert STATE_ON == state_2.state
with patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value={
DOMAIN: {
"test_2": {
"name": "Hello World reloaded",
"icon": "mdi:work_reloaded",
"initial": False,
},
"test_3": None,
}
},
):
with patch("homeassistant.config.find_config_file", 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 + 2 == len(hass.states.async_entity_ids())
state_1 = hass.states.get("input_boolean.test_1")
state_2 = hass.states.get("input_boolean.test_2")
state_3 = hass.states.get("input_boolean.test_3")
assert state_1 is None
assert state_2 is not None
assert state_3 is not None
assert STATE_OFF == state_2.state
assert "Hello World reloaded" == state_2.attributes.get(ATTR_FRIENDLY_NAME)
assert "mdi:work_reloaded" == state_2.attributes.get(ATTR_ICON)