Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Jewish calendar integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 import zoneinfo
8 
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import (
18  CONF_ELEVATION,
19  CONF_LANGUAGE,
20  CONF_LATITUDE,
21  CONF_LOCATION,
22  CONF_LONGITUDE,
23  CONF_TIME_ZONE,
24 )
25 from homeassistant.core import HomeAssistant, callback
27  BooleanSelector,
28  LocationSelector,
29  SelectOptionDict,
30  SelectSelector,
31  SelectSelectorConfig,
32 )
33 
34 from .const import (
35  CONF_CANDLE_LIGHT_MINUTES,
36  CONF_DIASPORA,
37  CONF_HAVDALAH_OFFSET_MINUTES,
38  DEFAULT_CANDLE_LIGHT,
39  DEFAULT_DIASPORA,
40  DEFAULT_HAVDALAH_OFFSET_MINUTES,
41  DEFAULT_LANGUAGE,
42  DEFAULT_NAME,
43  DOMAIN,
44 )
45 
46 LANGUAGE = [
47  SelectOptionDict(value="hebrew", label="Hebrew"),
48  SelectOptionDict(value="english", label="English"),
49 ]
50 
51 OPTIONS_SCHEMA = vol.Schema(
52  {
53  vol.Optional(CONF_CANDLE_LIGHT_MINUTES, default=DEFAULT_CANDLE_LIGHT): int,
54  vol.Optional(
55  CONF_HAVDALAH_OFFSET_MINUTES, default=DEFAULT_HAVDALAH_OFFSET_MINUTES
56  ): int,
57  }
58 )
59 
60 
61 _LOGGER = logging.getLogger(__name__)
62 
63 
64 def _get_data_schema(hass: HomeAssistant) -> vol.Schema:
65  default_location = {
66  CONF_LATITUDE: hass.config.latitude,
67  CONF_LONGITUDE: hass.config.longitude,
68  }
69  return vol.Schema(
70  {
71  vol.Required(CONF_DIASPORA, default=DEFAULT_DIASPORA): BooleanSelector(),
72  vol.Required(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): SelectSelector(
73  SelectSelectorConfig(options=LANGUAGE)
74  ),
75  vol.Optional(CONF_LOCATION, default=default_location): LocationSelector(),
76  vol.Optional(CONF_ELEVATION, default=hass.config.elevation): int,
77  vol.Optional(CONF_TIME_ZONE, default=hass.config.time_zone): SelectSelector(
79  options=sorted(zoneinfo.available_timezones()),
80  )
81  ),
82  }
83  )
84 
85 
86 class JewishCalendarConfigFlow(ConfigFlow, domain=DOMAIN):
87  """Handle a config flow for Jewish calendar."""
88 
89  VERSION = 1
90 
91  @staticmethod
92  @callback
94  config_entry: ConfigEntry,
95  ) -> JewishCalendarOptionsFlowHandler:
96  """Get the options flow for this handler."""
98 
99  async def async_step_user(
100  self, user_input: dict[str, Any] | None = None
101  ) -> ConfigFlowResult:
102  """Handle the initial step."""
103  if user_input is not None:
104  if CONF_LOCATION in user_input:
105  user_input[CONF_LATITUDE] = user_input[CONF_LOCATION][CONF_LATITUDE]
106  user_input[CONF_LONGITUDE] = user_input[CONF_LOCATION][CONF_LONGITUDE]
107  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_NAME, data=user_input)
108 
109  return self.async_show_formasync_show_formasync_show_form(
110  step_id="user",
111  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
112  _get_data_schema(self.hass), user_input
113  ),
114  )
115 
117  self, user_input: dict[str, Any] | None = None
118  ) -> ConfigFlowResult:
119  """Handle a reconfiguration flow initialized by the user."""
120  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
121  if not user_input:
122  return self.async_show_formasync_show_formasync_show_form(
123  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
124  _get_data_schema(self.hass),
125  reconfigure_entry.data,
126  ),
127  step_id="reconfigure",
128  )
129 
130  return self.async_update_reload_and_abortasync_update_reload_and_abort(reconfigure_entry, data=user_input)
131 
132 
134  """Handle Jewish Calendar options."""
135 
136  async def async_step_init(
137  self, user_input: dict[str, str] | None = None
138  ) -> ConfigFlowResult:
139  """Manage the Jewish Calendar options."""
140  if user_input is not None:
141  return self.async_create_entryasync_create_entry(data=user_input)
142 
143  return self.async_show_formasync_show_form(
144  step_id="init",
145  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
146  OPTIONS_SCHEMA, self.config_entryconfig_entryconfig_entry.options
147  ),
148  )
JewishCalendarOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:95
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:118
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:101
ConfigFlowResult async_step_init(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:138
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
vol.Schema _get_data_schema(HomeAssistant hass)
Definition: config_flow.py:64