Add config flow for switch.light (#67447)
* Add config flow for switch.light * Refactor according to code review * Setup light switch from config entry * Improve async_resolve_entity * Prepare for multiple steps * Remove name and options flow from switch light * Check type before adding description to schema keys * Remove options flow enabler * Copy name from the switch * Move helper flows to new file * Improve test coverage * Fix name * Remove dead code from abstract method * Remove manifest 'helper' option * Validate registry entry id before forwarding to light platform * Improve test * Add translations * Improve config entry setup * Log when config entry fails setup * Update homeassistant/components/switch/__init__.py Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
3f9a6bbaa7
commit
0c12914548
11 changed files with 454 additions and 21 deletions
|
@ -5,8 +5,12 @@ from homeassistant.components.light import (
|
|||
ATTR_SUPPORTED_COLOR_MODES,
|
||||
COLOR_MODE_ONOFF,
|
||||
)
|
||||
from homeassistant.components.switch.const import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.light import common
|
||||
from tests.components.switch import common as switch_common
|
||||
|
||||
|
@ -96,3 +100,69 @@ async def test_switch_service_calls(hass):
|
|||
|
||||
assert hass.states.get("switch.decorative_lights").state == "on"
|
||||
assert hass.states.get("light.light_switch").state == "on"
|
||||
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue