1 """Config flow for Plaato."""
3 from __future__
import annotations
7 from pyplaato.plaato
import PlaatoDeviceType
8 import voluptuous
as vol
26 DEFAULT_SCAN_INTERVAL,
29 PLACEHOLDER_DEVICE_NAME,
30 PLACEHOLDER_DEVICE_TYPE,
32 PLACEHOLDER_WEBHOOK_URL,
37 """Handles a Plaato config flow."""
43 self._init_info: dict[str, Any] = {}
46 self, user_input: dict[str, Any] |
None =
None
47 ) -> ConfigFlowResult:
48 """Handle user step."""
50 if user_input
is not None:
51 self._init_info[CONF_DEVICE_TYPE] = PlaatoDeviceType(
52 user_input[CONF_DEVICE_TYPE]
54 self._init_info[CONF_DEVICE_NAME] = user_input[CONF_DEVICE_NAME]
60 data_schema=vol.Schema(
64 default=self._init_info.
get(CONF_DEVICE_NAME,
None),
68 default=self._init_info.
get(CONF_DEVICE_TYPE,
None),
69 ): vol.In(
list(PlaatoDeviceType)),
75 self, user_input: dict[str, Any] |
None =
None
76 ) -> ConfigFlowResult:
77 """Handle device type step."""
79 device_type = self._init_info[CONF_DEVICE_TYPE]
81 if user_input
is not None:
82 token = user_input.get(CONF_TOKEN,
None)
83 use_webhook = user_input.get(CONF_USE_WEBHOOK,
False)
85 if not token
and not use_webhook:
86 errors = {
"base": PlaatoConfigFlow._get_error(device_type)}
89 self._init_info[CONF_USE_WEBHOOK] = use_webhook
90 self._init_info[CONF_TOKEN] = token
96 self, user_input: dict[str, Any] |
None =
None
97 ) -> ConfigFlowResult:
98 """Validate config step."""
100 use_webhook = self._init_info[CONF_USE_WEBHOOK]
102 if use_webhook
and user_input
is None:
104 webhook_id, webhook_url, cloudhook = await self.
_get_webhook_id_get_webhook_id()
107 self._init_info[CONF_WEBHOOK_ID] = webhook_id
108 self._init_info[CONF_CLOUDHOOK] = cloudhook
112 description_placeholders={
113 PLACEHOLDER_WEBHOOK_URL: webhook_url,
114 PLACEHOLDER_DOCS_URL: DOCS_URL,
121 """Create the entry step."""
123 webhook_id = self._init_info.
get(CONF_WEBHOOK_ID,
None)
124 auth_token = self._init_info[CONF_TOKEN]
125 device_name = self._init_info[CONF_DEVICE_NAME]
126 device_type = self._init_info[CONF_DEVICE_TYPE]
128 unique_id = auth_token
if auth_token
else webhook_id
134 title=device_type.name,
135 data=self._init_info,
136 description_placeholders={
137 PLACEHOLDER_DEVICE_TYPE: device_type.name,
138 PLACEHOLDER_DEVICE_NAME: device_name,
143 self, device_type: PlaatoDeviceType, errors: dict[str, str] |
None =
None
144 ) -> ConfigFlowResult:
145 data_schema = vol.Schema({vol.Optional(CONF_TOKEN, default=
""): str})
147 if device_type == PlaatoDeviceType.Airlock:
148 data_schema = data_schema.extend(
149 {vol.Optional(CONF_USE_WEBHOOK, default=
False): bool}
153 step_id=
"api_method",
154 data_schema=data_schema,
156 description_placeholders={PLACEHOLDER_DEVICE_TYPE: device_type.name},
160 """Generate webhook ID."""
161 webhook_id = webhook.async_generate_id()
162 if cloud.async_active_subscription(self.hass):
163 webhook_url = await cloud.async_create_cloudhook(self.hass, webhook_id)
166 webhook_url = webhook.async_generate_url(self.hass, webhook_id)
169 return webhook_id, webhook_url, cloudhook
173 if device_type == PlaatoDeviceType.Airlock:
174 return "no_api_method"
175 return "no_auth_token"
180 config_entry: ConfigEntry,
181 ) -> PlaatoOptionsFlowHandler:
182 """Get the options flow for this handler."""
187 """Handle Plaato options."""
190 """Manage the options."""
198 self, user_input: dict[str, Any] |
None =
None
199 ) -> ConfigFlowResult:
200 """Manage the options."""
201 if user_input
is not None:
206 data_schema=vol.Schema(
211 CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
219 self, user_input: dict[str, Any] |
None =
None
220 ) -> ConfigFlowResult:
221 """Manage the options for webhook device."""
222 if user_input
is not None:
228 if webhook_id
is None
229 else webhook.async_generate_url(self.hass, webhook_id)
234 description_placeholders={PLACEHOLDER_WEBHOOK_URL: webhook_url},
def _get_error(PlaatoDeviceType device_type)
ConfigFlowResult _show_api_method_form(self, PlaatoDeviceType device_type, dict[str, str]|None errors=None)
def _get_webhook_id(self)
ConfigFlowResult async_step_webhook(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
def _async_create_entry(self)
ConfigFlowResult async_step_api_method(self, dict[str, Any]|None user_input=None)
PlaatoOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_webhook(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(self, None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
web.Response get(self, web.Request request, str config_key)