2022-03-07 12:05:04 +00:00
|
|
|
"""Config flow for Group integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-03-23 15:34:44 +00:00
|
|
|
from collections.abc import Callable, 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-22 11:14:34 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import entity_registry as er, selector
|
2022-03-09 12:18:19 +00:00
|
|
|
from homeassistant.helpers.helper_config_entry_flow import (
|
|
|
|
HelperConfigFlowHandler,
|
2022-03-23 21:43:11 +00:00
|
|
|
HelperFlowFormStep,
|
2022-03-23 15:34:44 +00:00
|
|
|
HelperFlowMenuStep,
|
2022-03-09 12:18:19 +00:00
|
|
|
)
|
2022-03-07 12:05:04 +00:00
|
|
|
|
|
|
|
from . import DOMAIN
|
2022-03-10 09:39:51 +00:00
|
|
|
from .binary_sensor import CONF_ALL
|
2022-03-22 11:14:34 +00:00
|
|
|
from .const import CONF_HIDE_MEMBERS
|
2022-03-07 12:05:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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}}
|
|
|
|
),
|
2022-03-22 11:14:34 +00:00
|
|
|
vol.Required(CONF_HIDE_MEMBERS, default=False): selector.selector(
|
|
|
|
{"boolean": {}}
|
|
|
|
),
|
2022-03-07 12:05:04 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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-10 09:39:51 +00:00
|
|
|
BINARY_SENSOR_OPTIONS_SCHEMA = basic_group_options_schema("binary_sensor").extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ALL, default=False): selector.selector({"boolean": {}}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-22 15:09:18 +00:00
|
|
|
LIGHT_OPTIONS_SCHEMA = basic_group_options_schema("light").extend(
|
|
|
|
{
|
2022-03-24 15:51:31 +00:00
|
|
|
vol.Required(
|
|
|
|
CONF_ALL, default=False, description={"advanced": True}
|
|
|
|
): selector.selector({"boolean": {}}),
|
2022-03-22 15:09:18 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-28 10:27:26 +00:00
|
|
|
SWITCH_OPTIONS_SCHEMA = basic_group_options_schema("switch").extend(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_ALL, default=False, description={"advanced": True}
|
|
|
|
): selector.selector({"boolean": {}}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-10 09:39:51 +00:00
|
|
|
BINARY_SENSOR_CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required("name"): selector.selector({"text": {}})}
|
|
|
|
).extend(BINARY_SENSOR_OPTIONS_SCHEMA.schema)
|
|
|
|
|
2022-03-30 00:07:23 +00:00
|
|
|
GROUP_TYPES = [
|
|
|
|
"binary_sensor",
|
|
|
|
"cover",
|
|
|
|
"fan",
|
|
|
|
"light",
|
|
|
|
"lock",
|
|
|
|
"media_player",
|
|
|
|
"switch",
|
|
|
|
]
|
2022-03-09 12:18:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2022-03-23 15:34:44 +00:00
|
|
|
def choose_options_step(options: dict[str, Any]) -> str:
|
|
|
|
"""Return next step_id for options flow according to group_type."""
|
2022-03-09 12:18:19 +00:00
|
|
|
return cast(str, options["group_type"])
|
|
|
|
|
|
|
|
|
2022-03-23 15:34:44 +00:00
|
|
|
def set_group_type(group_type: str) -> Callable[[dict[str, Any]], dict[str, Any]]:
|
|
|
|
"""Set group type."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _set_group_type(user_input: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
"""Add group type to user input."""
|
|
|
|
return {"group_type": group_type, **user_input}
|
|
|
|
|
|
|
|
return _set_group_type
|
|
|
|
|
|
|
|
|
2022-03-23 21:43:11 +00:00
|
|
|
CONFIG_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = {
|
2022-03-23 15:34:44 +00:00
|
|
|
"user": HelperFlowMenuStep(GROUP_TYPES),
|
2022-03-23 21:43:11 +00:00
|
|
|
"binary_sensor": HelperFlowFormStep(
|
2022-03-23 15:34:44 +00:00
|
|
|
BINARY_SENSOR_CONFIG_SCHEMA, set_group_type("binary_sensor")
|
|
|
|
),
|
2022-03-23 21:43:11 +00:00
|
|
|
"cover": HelperFlowFormStep(
|
2022-03-23 15:34:44 +00:00
|
|
|
basic_group_config_schema("cover"), set_group_type("cover")
|
|
|
|
),
|
2022-03-23 21:43:11 +00:00
|
|
|
"fan": HelperFlowFormStep(basic_group_config_schema("fan"), set_group_type("fan")),
|
2022-03-24 11:17:52 +00:00
|
|
|
"light": HelperFlowFormStep(
|
|
|
|
basic_group_config_schema("light"), set_group_type("light")
|
|
|
|
),
|
2022-03-30 00:07:23 +00:00
|
|
|
"lock": HelperFlowFormStep(
|
|
|
|
basic_group_config_schema("lock"), set_group_type("lock")
|
|
|
|
),
|
2022-03-23 21:43:11 +00:00
|
|
|
"media_player": HelperFlowFormStep(
|
2022-03-23 15:34:44 +00:00
|
|
|
basic_group_config_schema("media_player"), set_group_type("media_player")
|
|
|
|
),
|
2022-03-28 10:27:26 +00:00
|
|
|
"switch": HelperFlowFormStep(
|
|
|
|
basic_group_config_schema("switch"), set_group_type("switch")
|
|
|
|
),
|
2022-03-09 12:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-23 21:43:11 +00:00
|
|
|
OPTIONS_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = {
|
|
|
|
"init": HelperFlowFormStep(None, next_step=choose_options_step),
|
|
|
|
"binary_sensor": HelperFlowFormStep(BINARY_SENSOR_OPTIONS_SCHEMA),
|
|
|
|
"cover": HelperFlowFormStep(basic_group_options_schema("cover")),
|
|
|
|
"fan": HelperFlowFormStep(basic_group_options_schema("fan")),
|
|
|
|
"light": HelperFlowFormStep(LIGHT_OPTIONS_SCHEMA),
|
2022-03-30 00:07:23 +00:00
|
|
|
"lock": HelperFlowFormStep(basic_group_options_schema("lock")),
|
2022-03-23 21:43:11 +00:00
|
|
|
"media_player": HelperFlowFormStep(basic_group_options_schema("media_player")),
|
2022-03-28 10:27:26 +00:00
|
|
|
"switch": HelperFlowFormStep(SWITCH_OPTIONS_SCHEMA),
|
2022-03-07 12:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
class GroupConfigFlowHandler(HelperConfigFlowHandler, domain=DOMAIN):
|
2022-03-28 10:27:26 +00:00
|
|
|
"""Handle a config or options flow for groups."""
|
2022-03-07 12:05:04 +00:00
|
|
|
|
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-22 11:14:34 +00:00
|
|
|
@callback
|
2022-03-09 12:18:19 +00:00
|
|
|
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
|
2022-03-28 07:39:54 +00:00
|
|
|
"""Return config entry title.
|
|
|
|
|
|
|
|
The options parameter contains config entry options, which is the union of user
|
|
|
|
input from the config flow steps.
|
|
|
|
"""
|
2022-03-09 12:18:19 +00:00
|
|
|
return cast(str, options["name"]) if "name" in options else ""
|
2022-03-22 11:14:34 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_config_flow_finished(self, options: Mapping[str, Any]) -> None:
|
|
|
|
"""Hide the group members if requested."""
|
|
|
|
if options[CONF_HIDE_MEMBERS]:
|
|
|
|
_async_hide_members(
|
|
|
|
self.hass, options[CONF_ENTITIES], er.RegistryEntryHider.INTEGRATION
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@staticmethod
|
|
|
|
def async_options_flow_finished(
|
|
|
|
hass: HomeAssistant, options: Mapping[str, Any]
|
|
|
|
) -> None:
|
|
|
|
"""Hide or unhide the group members as requested."""
|
|
|
|
hidden_by = (
|
|
|
|
er.RegistryEntryHider.INTEGRATION if options[CONF_HIDE_MEMBERS] else None
|
|
|
|
)
|
|
|
|
_async_hide_members(hass, options[CONF_ENTITIES], hidden_by)
|
|
|
|
|
|
|
|
|
|
|
|
def _async_hide_members(
|
|
|
|
hass: HomeAssistant, members: list[str], hidden_by: er.RegistryEntryHider | None
|
|
|
|
) -> None:
|
|
|
|
"""Hide or unhide group members."""
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
for member in members:
|
|
|
|
if not (entity_id := er.async_resolve_entity_id(registry, member)):
|
|
|
|
continue
|
|
|
|
if entity_id not in registry.entities:
|
|
|
|
continue
|
|
|
|
registry.async_update_entity(entity_id, hidden_by=hidden_by)
|