1 """Config flow for Cloudflare integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 import voluptuous
as vol
20 from .const
import CONF_RECORDS, DOMAIN
21 from .helpers
import get_zone_id
23 _LOGGER = logging.getLogger(__name__)
25 DATA_SCHEMA = vol.Schema(
27 vol.Required(CONF_API_TOKEN): str,
32 def _zone_schema(zones: list[pycfdns.ZoneModel] |
None =
None) -> vol.Schema:
33 """Zone selection schema."""
37 zones_list = [zones[
"name"]
for zones
in zones]
39 return vol.Schema({vol.Required(CONF_ZONE): vol.In(zones_list)})
42 def _records_schema(records: list[pycfdns.RecordModel] |
None =
None) -> vol.Schema:
43 """Zone records selection schema."""
47 records_dict = {name[
"name"]: name[
"name"]
for name
in records}
49 return vol.Schema({vol.Required(CONF_RECORDS): cv.multi_select(records_dict)})
56 """Validate the user input allows us to connect.
58 Data has the keys from DATA_SCHEMA with values provided by the user.
60 zone = data.get(CONF_ZONE)
61 records: list[pycfdns.RecordModel] = []
63 client = pycfdns.Client(
64 api_token=data[CONF_API_TOKEN],
68 zones = await client.list_zones()
69 if zone
and (zone_id :=
get_zone_id(zone, zones))
is not None:
70 records = await client.list_dns_records(zone_id=zone_id, type=
"A")
72 return {
"zones": zones,
"records": records}
76 """Handle a config flow for Cloudflare."""
81 """Initialize the Cloudflare config flow."""
82 self.cloudflare_config: dict[str, Any] = {}
83 self.
zoneszones: list[pycfdns.ZoneModel] |
None =
None
84 self.
recordsrecords: list[pycfdns.RecordModel] |
None =
None
87 self, entry_data: Mapping[str, Any]
88 ) -> ConfigFlowResult:
89 """Handle initiation of re-authentication with Cloudflare."""
93 self, user_input: dict[str, Any] |
None =
None
94 ) -> ConfigFlowResult:
95 """Handle re-authentication with Cloudflare."""
96 errors: dict[str, str] = {}
98 if user_input
is not None:
107 CONF_API_TOKEN: user_input[CONF_API_TOKEN],
112 step_id=
"reauth_confirm",
113 data_schema=DATA_SCHEMA,
118 self, user_input: dict[str, Any] |
None =
None
119 ) -> ConfigFlowResult:
120 """Handle a flow initiated by the user."""
121 persistent_notification.async_dismiss(self.hass,
"cloudflare_setup")
123 errors: dict[str, str] = {}
125 if user_input
is not None:
129 self.cloudflare_config.
update(user_input)
134 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
138 self, user_input: dict[str, Any] |
None =
None
139 ) -> ConfigFlowResult:
140 """Handle the picking the zone."""
141 errors: dict[str, str] = {}
143 if user_input
is not None:
144 self.cloudflare_config.
update(user_input)
160 self, user_input: dict[str, Any] |
None =
None
161 ) -> ConfigFlowResult:
162 """Handle the picking the zone records."""
164 if user_input
is not None:
165 self.cloudflare_config.
update(user_input)
166 title = self.cloudflare_config[CONF_ZONE]
175 self, config: dict[str, Any]
176 ) -> tuple[dict[str, list[Any]], dict[str, str]]:
177 errors: dict[str, str] = {}
182 except pycfdns.ComunicationException:
183 errors[
"base"] =
"cannot_connect"
184 except pycfdns.AuthenticationException:
185 errors[
"base"] =
"invalid_auth"
187 _LOGGER.exception(
"Unexpected exception")
188 errors[
"base"] =
"unknown"
194 """Error to indicate we cannot connect."""
198 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_records(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
tuple[dict[str, list[Any]], dict[str, str]] _async_validate_or_error(self, dict[str, Any] config)
ConfigFlowResult async_step_zone(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigEntry _get_reauth_entry(self)
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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)
_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, Any] _validate_input(HomeAssistant hass, dict[str, Any] data)
vol.Schema _zone_schema(list[pycfdns.ZoneModel]|None zones=None)
vol.Schema _records_schema(list[pycfdns.RecordModel]|None records=None)
str|None get_zone_id(str target_zone_name, list[pycfdns.ZoneModel] zones)
IssData update(pyiss.ISS iss)
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)