2018-06-14 19:17:54 +00:00
|
|
|
"""Helpers for data entry flows for config entries."""
|
2021-03-17 17:34:19 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-29 21:12:58 +00:00
|
|
|
import logging
|
2021-03-17 17:34:19 +00:00
|
|
|
from typing import Any, Awaitable, Callable, Union
|
2019-12-09 15:42:10 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2021-03-27 11:55:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2021-04-29 21:12:58 +00:00
|
|
|
from homeassistant.helpers.typing import UNDEFINED, DiscoveryInfoType, UndefinedType
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2021-05-23 12:08:21 +00:00
|
|
|
DiscoveryFunctionType = Callable[[HomeAssistant], Union[Awaitable[bool], bool]]
|
2018-10-23 09:14:46 +00:00
|
|
|
|
2021-04-29 21:12:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
class DiscoveryFlowHandler(config_entries.ConfigFlow):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Handle a discovery config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
domain: str,
|
|
|
|
title: str,
|
|
|
|
discovery_function: DiscoveryFunctionType,
|
|
|
|
) -> None:
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Initialize the discovery config flow."""
|
|
|
|
self._domain = domain
|
|
|
|
self._title = title
|
|
|
|
self._discovery_function = discovery_function
|
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_user(
|
2021-03-17 17:34:19 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
if self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-04-27 06:35:04 +00:00
|
|
|
await self.async_set_unique_id(self._domain, raise_on_progress=False)
|
|
|
|
|
2018-09-21 14:34:37 +00:00
|
|
|
return await self.async_step_confirm()
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_confirm(
|
2021-03-17 17:34:19 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-09-21 14:34:37 +00:00
|
|
|
"""Confirm setup."""
|
|
|
|
if user_input is None:
|
2021-03-08 18:54:51 +00:00
|
|
|
self._set_confirm_only()
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="confirm")
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-04-27 06:35:04 +00:00
|
|
|
if self.source == config_entries.SOURCE_USER:
|
2018-09-21 14:34:37 +00:00
|
|
|
# Get current discovered entries.
|
|
|
|
in_progress = self._async_in_progress()
|
|
|
|
|
|
|
|
has_devices = in_progress
|
|
|
|
if not has_devices:
|
2020-06-15 11:38:38 +00:00
|
|
|
has_devices = await self.hass.async_add_job( # type: ignore
|
2019-07-31 19:25:30 +00:00
|
|
|
self._discovery_function, self.hass
|
|
|
|
)
|
2018-09-21 14:34:37 +00:00
|
|
|
|
|
|
|
if not has_devices:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="no_devices_found")
|
2018-09-21 14:34:37 +00:00
|
|
|
|
|
|
|
# Cancel the discovered one.
|
|
|
|
for flow in in_progress:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config_entries.flow.async_abort(flow["flow_id"])
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-06-10 20:46:14 +00:00
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=self._title, data={})
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_discovery(
|
2021-04-15 17:17:07 +00:00
|
|
|
self, discovery_info: DiscoveryInfoType
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Handle a flow initialized by discovery."""
|
|
|
|
if self._async_in_progress() or self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-04-27 06:35:04 +00:00
|
|
|
await self.async_set_unique_id(self._domain)
|
|
|
|
|
2018-06-14 19:17:54 +00:00
|
|
|
return await self.async_step_confirm()
|
|
|
|
|
2019-05-30 21:08:05 +00:00
|
|
|
async_step_zeroconf = async_step_discovery
|
|
|
|
async_step_ssdp = async_step_discovery
|
2020-10-07 16:30:51 +00:00
|
|
|
async_step_mqtt = async_step_discovery
|
2019-05-31 18:58:48 +00:00
|
|
|
async_step_homekit = async_step_discovery
|
2021-01-14 08:09:08 +00:00
|
|
|
async_step_dhcp = async_step_discovery
|
2019-05-30 21:08:05 +00:00
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
async def async_step_import(self, _: dict[str, Any] | None) -> FlowResult:
|
2018-07-23 13:08:03 +00:00
|
|
|
"""Handle a flow initialized by import."""
|
2020-06-10 20:46:14 +00:00
|
|
|
if self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2018-07-23 13:08:03 +00:00
|
|
|
|
2020-06-10 20:46:14 +00:00
|
|
|
# Cancel other flows.
|
|
|
|
in_progress = self._async_in_progress()
|
|
|
|
for flow in in_progress:
|
|
|
|
self.hass.config_entries.flow.async_abort(flow["flow_id"])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=self._title, data={})
|
2018-10-23 09:14:46 +00:00
|
|
|
|
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
def register_discovery_flow(
|
|
|
|
domain: str,
|
|
|
|
title: str,
|
|
|
|
discovery_function: DiscoveryFunctionType,
|
2021-04-29 21:12:58 +00:00
|
|
|
connection_class: str | UndefinedType = UNDEFINED,
|
2019-08-16 23:19:19 +00:00
|
|
|
) -> None:
|
|
|
|
"""Register flow for discovered integrations that not require auth."""
|
2021-04-29 21:12:58 +00:00
|
|
|
if connection_class is not UNDEFINED:
|
|
|
|
_LOGGER.warning(
|
|
|
|
(
|
|
|
|
"The %s (%s) integration is setting a connection_class"
|
|
|
|
" when calling the 'register_discovery_flow()' method in its"
|
|
|
|
" config flow. The connection class has been deprecated and will"
|
|
|
|
" be removed in a future release of Home Assistant."
|
|
|
|
" If '%s' is a custom integration, please contact the author"
|
|
|
|
" of that integration about this warning.",
|
|
|
|
),
|
|
|
|
title,
|
|
|
|
domain,
|
|
|
|
domain,
|
|
|
|
)
|
2019-08-16 23:19:19 +00:00
|
|
|
|
|
|
|
class DiscoveryFlow(DiscoveryFlowHandler):
|
|
|
|
"""Discovery flow handler."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2021-04-29 21:12:58 +00:00
|
|
|
super().__init__(domain, title, discovery_function)
|
2019-08-16 23:19:19 +00:00
|
|
|
|
|
|
|
config_entries.HANDLERS.register(domain)(DiscoveryFlow)
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
class WebhookFlowHandler(config_entries.ConfigFlow):
|
|
|
|
"""Handle a webhook config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
domain: str,
|
|
|
|
title: str,
|
|
|
|
description_placeholder: dict,
|
|
|
|
allow_multiple: bool,
|
|
|
|
) -> None:
|
2018-10-23 09:14:46 +00:00
|
|
|
"""Initialize the discovery config flow."""
|
|
|
|
self._domain = domain
|
|
|
|
self._title = title
|
|
|
|
self._description_placeholder = description_placeholder
|
|
|
|
self._allow_multiple = allow_multiple
|
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_user(
|
2021-03-17 17:34:19 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-10-23 09:14:46 +00:00
|
|
|
"""Handle a user initiated set up flow to create a webhook."""
|
|
|
|
if not self._allow_multiple and self._async_current_entries():
|
2020-10-01 18:44:13 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2018-10-23 09:14:46 +00:00
|
|
|
|
|
|
|
if user_input is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="user")
|
2018-10-23 09:14:46 +00:00
|
|
|
|
|
|
|
webhook_id = self.hass.components.webhook.async_generate_id()
|
2019-04-01 12:07:12 +00:00
|
|
|
|
2019-10-19 01:08:54 +00:00
|
|
|
if (
|
|
|
|
"cloud" in self.hass.config.components
|
|
|
|
and self.hass.components.cloud.async_active_subscription()
|
|
|
|
):
|
2019-07-31 19:25:30 +00:00
|
|
|
webhook_url = await self.hass.components.cloud.async_create_cloudhook(
|
|
|
|
webhook_id
|
|
|
|
)
|
2019-04-01 12:07:12 +00:00
|
|
|
cloudhook = True
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
webhook_url = self.hass.components.webhook.async_generate_url(webhook_id)
|
2019-04-01 12:07:12 +00:00
|
|
|
cloudhook = False
|
2018-10-23 09:14:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._description_placeholder["webhook_url"] = webhook_url
|
2018-10-23 09:14:46 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=self._title,
|
2019-07-31 19:25:30 +00:00
|
|
|
data={"webhook_id": webhook_id, "cloudhook": cloudhook},
|
|
|
|
description_placeholders=self._description_placeholder,
|
2018-10-23 09:14:46 +00:00
|
|
|
)
|
2019-04-01 12:07:12 +00:00
|
|
|
|
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
def register_webhook_flow(
|
|
|
|
domain: str, title: str, description_placeholder: dict, allow_multiple: bool = False
|
|
|
|
) -> None:
|
|
|
|
"""Register flow for webhook integrations."""
|
|
|
|
|
|
|
|
class WebhookFlow(WebhookFlowHandler):
|
|
|
|
"""Webhook flow handler."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(domain, title, description_placeholder, allow_multiple)
|
|
|
|
|
|
|
|
config_entries.HANDLERS.register(domain)(WebhookFlow)
|
|
|
|
|
|
|
|
|
2019-08-15 15:53:25 +00:00
|
|
|
async def webhook_async_remove_entry(
|
2021-03-27 11:55:24 +00:00
|
|
|
hass: HomeAssistant, entry: config_entries.ConfigEntry
|
2019-08-15 15:53:25 +00:00
|
|
|
) -> None:
|
2019-04-01 12:07:12 +00:00
|
|
|
"""Remove a webhook config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not entry.data.get("cloudhook") or "cloud" not in hass.config.components:
|
2019-04-01 12:07:12 +00:00
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.components.cloud.async_delete_cloudhook(entry.data["webhook_id"])
|