2018-11-20 10:51:34 +00:00
|
|
|
"""The tests for the Light Switch platform."""
|
|
|
|
|
2021-12-31 10:43:59 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_COLOR_MODE,
|
|
|
|
ATTR_SUPPORTED_COLOR_MODES,
|
|
|
|
COLOR_MODE_ONOFF,
|
|
|
|
)
|
2022-03-04 19:02:17 +00:00
|
|
|
from homeassistant.components.switch.const import DOMAIN as SWITCH_DOMAIN
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
2018-11-20 10:51:34 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-12-08 17:50:17 +00:00
|
|
|
|
2022-03-04 19:02:17 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-11-20 10:51:34 +00:00
|
|
|
from tests.components.light import common
|
|
|
|
from tests.components.switch import common as switch_common
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_state(hass):
|
|
|
|
"""Test light switch default state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "switch",
|
|
|
|
"entity_id": "switch.test",
|
|
|
|
"name": "Christmas Tree Lights",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-20 10:51:34 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.christmas_tree_lights")
|
2018-11-20 10:51:34 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "unavailable"
|
|
|
|
assert state.attributes["supported_features"] == 0
|
|
|
|
assert state.attributes.get("brightness") is None
|
|
|
|
assert state.attributes.get("hs_color") is None
|
|
|
|
assert state.attributes.get("color_temp") is None
|
|
|
|
assert state.attributes.get("white_value") is None
|
|
|
|
assert state.attributes.get("effect_list") is None
|
|
|
|
assert state.attributes.get("effect") is None
|
2021-12-31 10:43:59 +00:00
|
|
|
assert state.attributes.get(ATTR_SUPPORTED_COLOR_MODES) == [COLOR_MODE_ONOFF]
|
|
|
|
assert state.attributes.get(ATTR_COLOR_MODE) is None
|
2018-11-20 10:51:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_light_service_calls(hass):
|
|
|
|
"""Test service calls to light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "switch", {"switch": [{"platform": "demo"}]})
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"light",
|
|
|
|
{"light": [{"platform": "switch", "entity_id": "switch.decorative_lights"}]},
|
|
|
|
)
|
2018-11-20 10:51:34 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("light.light_switch").state == "on"
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_toggle(hass, "light.light_switch")
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("switch.decorative_lights").state == "off"
|
|
|
|
assert hass.states.get("light.light_switch").state == "off"
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_on(hass, "light.light_switch")
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("switch.decorative_lights").state == "on"
|
|
|
|
assert hass.states.get("light.light_switch").state == "on"
|
2021-12-31 10:43:59 +00:00
|
|
|
assert (
|
|
|
|
hass.states.get("light.light_switch").attributes.get(ATTR_COLOR_MODE)
|
|
|
|
== COLOR_MODE_ONOFF
|
|
|
|
)
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_off(hass, "light.light_switch")
|
2020-10-05 13:28:15 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("switch.decorative_lights").state == "off"
|
|
|
|
assert hass.states.get("light.light_switch").state == "off"
|
2018-11-20 10:51:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_service_calls(hass):
|
|
|
|
"""Test service calls to switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "switch", {"switch": [{"platform": "demo"}]})
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"light",
|
|
|
|
{"light": [{"platform": "switch", "entity_id": "switch.decorative_lights"}]},
|
|
|
|
)
|
2018-11-20 10:51:34 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("light.light_switch").state == "on"
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await switch_common.async_turn_off(hass, "switch.decorative_lights")
|
2020-10-05 13:28:15 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("switch.decorative_lights").state == "off"
|
|
|
|
assert hass.states.get("light.light_switch").state == "off"
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await switch_common.async_turn_on(hass, "switch.decorative_lights")
|
2020-10-05 13:28:15 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-11-20 10:51:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("switch.decorative_lights").state == "on"
|
|
|
|
assert hass.states.get("light.light_switch").state == "on"
|
2022-03-04 19:02:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_config_entry(hass: HomeAssistant):
|
|
|
|
"""Test light switch setup from config entry."""
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={},
|
|
|
|
domain=SWITCH_DOMAIN,
|
|
|
|
options={"entity_id": "switch.abc"},
|
|
|
|
title="ABC",
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert SWITCH_DOMAIN in hass.config.components
|
|
|
|
|
|
|
|
state = hass.states.get("light.abc")
|
|
|
|
assert state.state == "unavailable"
|
|
|
|
# Name copied from config entry title
|
|
|
|
assert state.name == "ABC"
|
|
|
|
|
|
|
|
# Check the light is added to the entity registry
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
entity_entry = registry.async_get("light.abc")
|
|
|
|
assert entity_entry.unique_id == config_entry.entry_id
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_entry_uuid(hass: HomeAssistant):
|
|
|
|
"""Test light switch setup from config entry with entity registry id."""
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
registry_entry = registry.async_get_or_create("switch", "test", "unique")
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={},
|
|
|
|
domain=SWITCH_DOMAIN,
|
|
|
|
options={"entity_id": registry_entry.id},
|
|
|
|
title="ABC",
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("light.abc")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_entry_unregistered_uuid(hass: HomeAssistant):
|
|
|
|
"""Test light switch setup from config entry with unknown entity registry id."""
|
|
|
|
fake_uuid = "a266a680b608c32770e6c45bfe6b8411"
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={},
|
|
|
|
domain=SWITCH_DOMAIN,
|
|
|
|
options={"entity_id": fake_uuid},
|
|
|
|
title="ABC",
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
assert not await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.states.async_all()) == 0
|