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-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,
|
|
|
|
HelperFlowStep,
|
|
|
|
)
|
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(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ALL, default=False): 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-22 15:09:18 +00:00
|
|
|
LIGHT_CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required("name"): selector.selector({"text": {}})}
|
|
|
|
).extend(LIGHT_OPTIONS_SCHEMA.schema)
|
|
|
|
|
2022-03-10 09:39:51 +00:00
|
|
|
|
2022-03-09 12:18:19 +00:00
|
|
|
INITIAL_STEP_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required("group_type"): selector.selector(
|
|
|
|
{
|
|
|
|
"select": {
|
|
|
|
"options": [
|
2022-03-10 09:39:51 +00:00
|
|
|
"binary_sensor",
|
2022-03-09 12:18:19 +00:00
|
|
|
"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),
|
2022-03-10 09:39:51 +00:00
|
|
|
"binary_sensor": HelperFlowStep(BINARY_SENSOR_CONFIG_SCHEMA),
|
2022-03-09 12:18:19 +00:00
|
|
|
"cover": HelperFlowStep(basic_group_config_schema("cover")),
|
|
|
|
"fan": HelperFlowStep(basic_group_config_schema("fan")),
|
2022-03-22 15:09:18 +00:00
|
|
|
"light": HelperFlowStep(LIGHT_CONFIG_SCHEMA),
|
2022-03-09 12:18:19 +00:00
|
|
|
"media_player": HelperFlowStep(basic_group_config_schema("media_player")),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS_FLOW = {
|
|
|
|
"init": HelperFlowStep(None, next_step=choose_config_step),
|
2022-03-10 09:39:51 +00:00
|
|
|
"binary_sensor": HelperFlowStep(BINARY_SENSOR_OPTIONS_SCHEMA),
|
2022-03-09 12:18:19 +00:00
|
|
|
"cover": HelperFlowStep(basic_group_options_schema("cover")),
|
|
|
|
"fan": HelperFlowStep(basic_group_options_schema("fan")),
|
2022-03-22 15:09:18 +00:00
|
|
|
"light": HelperFlowStep(LIGHT_OPTIONS_SCHEMA),
|
2022-03-09 12:18:19 +00:00
|
|
|
"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-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-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 ""
|
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)
|