1 """Config flow for Nina integration."""
3 from __future__
import annotations
7 from pynina
import ApiError, Nina
8 import voluptuous
as vol
36 """Swap keys and values in dict."""
37 all_region_codes_swaped: dict[str, str] = {}
39 for key, value
in dict_to_sort.items():
40 if value
not in all_region_codes_swaped:
41 all_region_codes_swaped[value] = key
43 for i
in range(len(dict_to_sort)):
44 tmp_value: str = f
"{value}_{i}"
45 if tmp_value
not in all_region_codes_swaped:
46 all_region_codes_swaped[tmp_value] = key
49 return dict(sorted(all_region_codes_swaped.items(), key=
lambda ele: ele[1]))
53 _all_region_codes_sorted: dict[str, str], regions: dict[str, dict[str, Any]]
54 ) -> dict[str, dict[str, Any]]:
55 """Split regions alphabetical."""
56 for index, name
in _all_region_codes_sorted.items():
57 for region_name, grouping_letters
in CONST_REGION_MAPPING.items():
58 if name[0]
in grouping_letters:
59 regions[region_name][index] = name
65 user_input: dict[str, Any], _all_region_codes_sorted: dict[str, str]
67 """Prepare the user inputs."""
68 tmp: dict[str, Any] = {}
70 for reg
in user_input[CONF_REGIONS]:
71 tmp[_all_region_codes_sorted[reg]] = reg.split(
"_", 1)[0]
73 compact: dict[str, Any] = {}
75 for key, val
in tmp.items():
78 compact[val] = f
"{compact[val]} + {key}"
82 user_input[CONF_REGIONS] = compact
88 """Handle a config flow for NINA."""
96 self.
regionsregions: dict[str, dict[str, Any]] = {}
98 for name
in CONST_REGIONS:
103 user_input: dict[str, Any] |
None =
None,
104 ) -> ConfigFlowResult:
105 """Handle the initial step."""
106 errors: dict[str, Any] = {}
113 await nina.getAllRegionalCodes()
116 errors[
"base"] =
"cannot_connect"
117 except Exception
as err:
118 _LOGGER.exception(
"Unexpected exception: %s", err)
119 errors[
"base"] =
"unknown"
123 if user_input
is not None and not errors:
124 user_input[CONF_REGIONS] = []
126 for group
in CONST_REGIONS:
127 if group_input := user_input.get(group):
128 user_input[CONF_REGIONS] += group_input
130 if not user_input[CONF_HEADLINE_FILTER]:
131 user_input[CONF_HEADLINE_FILTER] = NO_MATCH_REGEX
133 if user_input[CONF_REGIONS]:
139 errors[
"base"] =
"no_selection"
141 regions_schema: VolDictType = {
142 vol.Optional(region): cv.multi_select(self.
regionsregions[region])
143 for region
in CONST_REGIONS
148 data_schema=vol.Schema(
151 vol.Required(CONF_MESSAGE_SLOTS, default=5): vol.All(
152 int, vol.Range(min=1, max=20)
154 vol.Optional(CONF_HEADLINE_FILTER, default=
""): cv.string,
163 config_entry: ConfigEntry,
164 ) -> OptionsFlowHandler:
165 """Get the options flow for this handler."""
170 """Handle a option flow for nut."""
172 def __init__(self, config_entry: ConfigEntry) ->
None:
173 """Initialize options flow."""
177 self.
regionsregions: dict[str, dict[str, Any]] = {}
179 for name
in CONST_REGIONS:
181 if name
not in self.
datadata:
182 self.
datadata[name] = []
185 self, user_input: dict[str, Any] |
None =
None
186 ) -> ConfigFlowResult:
187 """Handle options flow."""
188 errors: dict[str, str] = {}
195 await nina.getAllRegionalCodes()
198 errors[
"base"] =
"cannot_connect"
199 except Exception
as err:
200 _LOGGER.exception(
"Unexpected exception: %s", err)
201 errors[
"base"] =
"unknown"
205 if user_input
is not None and not errors:
206 user_input[CONF_REGIONS] = []
208 for group
in CONST_REGIONS:
209 if group_input := user_input.get(group):
210 user_input[CONF_REGIONS] += group_input
212 if user_input[CONF_REGIONS]:
217 entity_registry = er.async_get(self.hass)
219 entries = er.async_entries_for_config_entry(
223 removed_entities_slots = [
224 f
"{region}-{slot_id}"
225 for region
in self.
datadata[CONF_REGIONS]
226 for slot_id
in range(self.
datadata[CONF_MESSAGE_SLOTS] + 1)
227 if slot_id > user_input[CONF_MESSAGE_SLOTS]
230 removed_entites_area = [
231 f
"{cfg_region}-{slot_id}"
232 for slot_id
in range(1, self.
datadata[CONF_MESSAGE_SLOTS] + 1)
233 for cfg_region
in self.
datadata[CONF_REGIONS]
234 if cfg_region
not in user_input[CONF_REGIONS]
237 for entry
in entries:
238 for entity_uid
in list(
239 set(removed_entities_slots + removed_entites_area)
241 if entry.unique_id == entity_uid:
242 entity_registry.async_remove(entry.entity_id)
244 self.hass.config_entries.async_update_entry(
250 errors[
"base"] =
"no_selection"
252 schema: VolDictType = {
254 vol.Optional(region, default=self.
datadata[region]): cv.multi_select(
257 for region
in CONST_REGIONS
261 default=self.
datadata[CONF_MESSAGE_SLOTS],
262 ): vol.All(int, vol.Range(min=1, max=20)),
264 CONF_HEADLINE_FILTER,
265 default=self.
datadata[CONF_HEADLINE_FILTER],
269 default=self.
datadata[CONF_AREA_FILTER],
275 data_schema=vol.Schema(schema),
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None __init__(self, ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
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_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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
dict[str, str] swap_key_value(dict[str, str] dict_to_sort)
dict[str, dict[str, Any]] split_regions(dict[str, str] _all_region_codes_sorted, dict[str, dict[str, Any]] regions)
dict[str, Any] prepare_user_input(dict[str, Any] user_input, dict[str, str] _all_region_codes_sorted)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)