Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Switch as X integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.const import CONF_ENTITY_ID, Platform
11 from homeassistant.helpers import entity_registry as er, selector
13  SchemaConfigFlowHandler,
14  SchemaFlowFormStep,
15  wrapped_entity_config_entry_title,
16 )
17 
18 from .const import CONF_INVERT, CONF_TARGET_DOMAIN, DOMAIN
19 
20 TARGET_DOMAIN_OPTIONS = [
21  Platform.COVER,
22  Platform.FAN,
23  Platform.LIGHT,
24  Platform.LOCK,
25  Platform.SIREN,
26  Platform.VALVE,
27 ]
28 
29 CONFIG_FLOW = {
30  "user": SchemaFlowFormStep(
31  vol.Schema(
32  {
33  vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
34  selector.EntitySelectorConfig(domain=Platform.SWITCH),
35  ),
36  vol.Optional(CONF_INVERT, default=False): selector.BooleanSelector(),
37  vol.Required(CONF_TARGET_DOMAIN): selector.SelectSelector(
38  selector.SelectSelectorConfig(
39  options=TARGET_DOMAIN_OPTIONS, translation_key="target_domain"
40  ),
41  ),
42  }
43  )
44  )
45 }
46 
47 OPTIONS_FLOW = {
48  "init": SchemaFlowFormStep(
49  vol.Schema({vol.Required(CONF_INVERT): selector.BooleanSelector()})
50  ),
51 }
52 
53 
55  """Handle a config flow for Switch as X."""
56 
57  config_flow = CONFIG_FLOW
58  options_flow = OPTIONS_FLOW
59 
60  VERSION = 1
61  MINOR_VERSION = 2
62 
63  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
64  """Return config entry title and hide the wrapped entity if registered."""
65  # Hide the wrapped entry if registered
66  registry = er.async_get(self.hass)
67  entity_entry = registry.async_get(options[CONF_ENTITY_ID])
68  if entity_entry is not None and not entity_entry.hidden:
69  registry.async_update_entity(
70  options[CONF_ENTITY_ID], hidden_by=er.RegistryEntryHider.INTEGRATION
71  )
72 
73  return wrapped_entity_config_entry_title(self.hass, options[CONF_ENTITY_ID])
str wrapped_entity_config_entry_title(HomeAssistant hass, str entity_id_or_uuid)