Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Wake on lan integration."""
2 
3 from collections.abc import Mapping
4 from typing import Any
5 
6 import voluptuous as vol
7 
8 from homeassistant.const import CONF_BROADCAST_ADDRESS, CONF_BROADCAST_PORT, CONF_MAC
9 from homeassistant.helpers import device_registry as dr
11  SchemaCommonFlowHandler,
12  SchemaConfigFlowHandler,
13  SchemaFlowFormStep,
14 )
16  NumberSelector,
17  NumberSelectorConfig,
18  NumberSelectorMode,
19  TextSelector,
20 )
21 
22 from .const import DEFAULT_NAME, DOMAIN
23 
24 
25 async def validate(
26  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
27 ) -> dict[str, Any]:
28  """Validate input setup."""
29  user_input = await validate_options(handler, user_input)
30 
31  user_input[CONF_MAC] = dr.format_mac(user_input[CONF_MAC])
32 
33  # Mac address needs to be unique
34  handler.parent_handler._async_abort_entries_match({CONF_MAC: user_input[CONF_MAC]}) # noqa: SLF001
35 
36  return user_input
37 
38 
39 async def validate_options(
40  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
41 ) -> dict[str, Any]:
42  """Validate input options."""
43  if CONF_BROADCAST_PORT in user_input:
44  # Convert float to int for broadcast port
45  user_input[CONF_BROADCAST_PORT] = int(user_input[CONF_BROADCAST_PORT])
46  return user_input
47 
48 
49 DATA_SCHEMA = {vol.Required(CONF_MAC): TextSelector()}
50 OPTIONS_SCHEMA = {
51  vol.Optional(CONF_BROADCAST_ADDRESS): TextSelector(),
52  vol.Optional(CONF_BROADCAST_PORT): NumberSelector(
53  NumberSelectorConfig(min=0, max=65535, step=1, mode=NumberSelectorMode.BOX)
54  ),
55 }
56 
57 
58 CONFIG_FLOW = {
59  "user": SchemaFlowFormStep(
60  schema=vol.Schema(DATA_SCHEMA).extend(OPTIONS_SCHEMA),
61  validate_user_input=validate,
62  )
63 }
64 OPTIONS_FLOW = {
65  "init": SchemaFlowFormStep(
66  vol.Schema(OPTIONS_SCHEMA), validate_user_input=validate_options
67  ),
68 }
69 
70 
72  """Handle a config flow for Wake on Lan."""
73 
74  config_flow = CONFIG_FLOW
75  options_flow = OPTIONS_FLOW
76 
77  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
78  """Return config entry title."""
79  mac: str = options[CONF_MAC]
80  return f"{DEFAULT_NAME} {mac}"
dict[str, Any] validate_options(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:41
dict[str, Any] validate(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:27