2022-03-07 12:05:04 +00:00
|
|
|
"""Config flow for Group integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
from collections.abc import Mapping
|
2022-03-07 12:05:04 +00:00
|
|
|
from typing import Any, cast
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_ENTITIES
|
2022-03-09 12:18:19 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import selector
|
|
|
|
from homeassistant.helpers.helper_config_entry_flow import (
|
|
|
|
HelperConfigFlowHandler,
|
|
|
|
HelperFlowStep,
|
|
|
|
)
|
2022-03-07 12:05:04 +00:00
|
|
|
|
|
|
|
from . import DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
def basic_group_options_schema(domain: str) -> vol.Schema:
|
|
|
|
"""Generate options schema."""
|
|
|
|
return vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ENTITIES): selector.selector(
|
|
|
|
{"entity": {"domain": domain, "multiple": True}}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def basic_group_config_schema(domain: str) -> vol.Schema:
|
|
|
|
"""Generate config schema."""
|
|
|
|
return vol.Schema({vol.Required("name"): selector.selector({"text": {}})}).extend(
|
|
|
|
basic_group_options_schema(domain).schema
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
INITIAL_STEP_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required("group_type"): selector.selector(
|
|
|
|
{
|
|
|
|
"select": {
|
|
|
|
"options": [
|
|
|
|
"cover",
|
|
|
|
"fan",
|
|
|
|
"light",
|
|
|
|
"media_player",
|
|
|
|
]
|
2022-03-07 12:05:04 +00:00
|
|
|
}
|
2022-03-09 12:18:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def choose_config_step(options: dict[str, Any]) -> str:
|
|
|
|
"""Return next step_id when group_type is selected."""
|
|
|
|
return cast(str, options["group_type"])
|
|
|
|
|
|
|
|
|
|
|
|
CONFIG_FLOW = {
|
|
|
|
"user": HelperFlowStep(INITIAL_STEP_SCHEMA, next_step=choose_config_step),
|
|
|
|
"cover": HelperFlowStep(basic_group_config_schema("cover")),
|
|
|
|
"fan": HelperFlowStep(basic_group_config_schema("fan")),
|
|
|
|
"light": HelperFlowStep(basic_group_config_schema("light")),
|
|
|
|
"media_player": HelperFlowStep(basic_group_config_schema("media_player")),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS_FLOW = {
|
|
|
|
"init": HelperFlowStep(None, next_step=choose_config_step),
|
|
|
|
"cover": HelperFlowStep(basic_group_options_schema("cover")),
|
|
|
|
"fan": HelperFlowStep(basic_group_options_schema("fan")),
|
|
|
|
"light": HelperFlowStep(basic_group_options_schema("light")),
|
|
|
|
"media_player": HelperFlowStep(basic_group_options_schema("media_player")),
|
2022-03-07 12:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
class GroupConfigFlowHandler(HelperConfigFlowHandler, domain=DOMAIN):
|
2022-03-07 12:05:04 +00:00
|
|
|
"""Handle a config or options flow for Switch Light."""
|
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
config_flow = CONFIG_FLOW
|
|
|
|
options_flow = OPTIONS_FLOW
|
2022-03-07 12:05:04 +00:00
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
|
2022-03-07 12:05:04 +00:00
|
|
|
"""Return config entry title."""
|
2022-03-09 12:18:19 +00:00
|
|
|
return cast(str, options["name"]) if "name" in options else ""
|