Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for World clock."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 import zoneinfo
8 
9 import voluptuous as vol
10 
11 from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
13  SchemaCommonFlowHandler,
14  SchemaConfigFlowHandler,
15  SchemaFlowFormStep,
16 )
18  SelectOptionDict,
19  SelectSelector,
20  SelectSelectorConfig,
21  SelectSelectorMode,
22  TextSelector,
23 )
24 
25 from .const import CONF_TIME_FORMAT, DEFAULT_NAME, DEFAULT_TIME_STR_FORMAT, DOMAIN
26 
27 TIME_STR_OPTIONS = [
29  value=DEFAULT_TIME_STR_FORMAT, label=f"14:05 ({DEFAULT_TIME_STR_FORMAT})"
30  ),
31  SelectOptionDict(value="%I:%M %p", label="11:05 AM (%I:%M %p)"),
32  SelectOptionDict(value="%Y-%m-%d %H:%M", label="2024-01-01 14:05 (%Y-%m-%d %H:%M)"),
34  value="%a, %b %d, %Y %I:%M %p",
35  label="Mon, Jan 01, 2024 11:05 AM (%a, %b %d, %Y %I:%M %p)",
36  ),
37 ]
38 
39 
41  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
42 ) -> dict[str, Any]:
43  """Validate already existing entry."""
44  handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001
45 
46  return user_input
47 
48 
49 async def get_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
50  """Get available timezones."""
51  get_timezones: list[str] = list(
52  await handler.parent_handler.hass.async_add_executor_job(
53  zoneinfo.available_timezones
54  )
55  )
56  return vol.Schema(
57  {
58  vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
59  vol.Required(CONF_TIME_ZONE): SelectSelector(
61  options=get_timezones, mode=SelectSelectorMode.DROPDOWN, sort=True
62  )
63  ),
64  }
65  ).extend(DATA_SCHEMA_OPTIONS.schema)
66 
67 
68 DATA_SCHEMA_OPTIONS = vol.Schema(
69  {
70  vol.Optional(CONF_TIME_FORMAT, default=DEFAULT_TIME_STR_FORMAT): SelectSelector(
72  options=TIME_STR_OPTIONS,
73  custom_value=True,
74  mode=SelectSelectorMode.DROPDOWN,
75  )
76  )
77  }
78 )
79 
80 
81 CONFIG_FLOW = {
82  "user": SchemaFlowFormStep(
83  schema=get_schema,
84  validate_user_input=validate_duplicate,
85  ),
86  "import": SchemaFlowFormStep(
87  schema=get_schema,
88  validate_user_input=validate_duplicate,
89  ),
90 }
91 OPTIONS_FLOW = {
92  "init": SchemaFlowFormStep(
93  DATA_SCHEMA_OPTIONS,
94  validate_user_input=validate_duplicate,
95  )
96 }
97 
98 
100  """Handle a config flow for Worldclock."""
101 
102  config_flow = CONFIG_FLOW
103  options_flow = OPTIONS_FLOW
104 
105  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
106  """Return config entry title."""
107  return cast(str, options[CONF_NAME])
dict[str, Any] validate_duplicate(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:42
vol.Schema get_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:49