hass-core/homeassistant/components/switch/config_flow.py
Erik Montnemery 0c12914548
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>
2022-03-04 20:02:17 +01:00

45 lines
1.2 KiB
Python

"""Config flow for Switch integration."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant.core import split_entity_id
from homeassistant.helpers import (
entity_registry as er,
helper_config_entry_flow,
selector,
)
from .const import DOMAIN
STEPS = {
"init": vol.Schema(
{
vol.Required("entity_id"): selector.selector(
{"entity": {"domain": "switch"}}
),
}
)
}
class SwitchLightConfigFlowHandler(
helper_config_entry_flow.HelperConfigFlowHandler, domain=DOMAIN
):
"""Handle a config or options flow for Switch Light."""
steps = STEPS
def async_config_entry_title(self, user_input: dict[str, Any]) -> str:
"""Return config entry title."""
registry = er.async_get(self.hass)
object_id = split_entity_id(user_input["entity_id"])[1]
entry = registry.async_get(user_input["entity_id"])
if entry:
return entry.name or entry.original_name or object_id
state = self.hass.states.get(user_input["entity_id"])
if state:
return state.name or object_id
return object_id