2018-06-14 19:17:54 +00:00
|
|
|
"""Helpers for data entry flows for config entries."""
|
2020-06-15 11:38:38 +00:00
|
|
|
from typing import Any, Awaitable, Callable, Dict, Optional, Union
|
2019-12-09 15:42:10 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2019-12-09 15:42:10 +00:00
|
|
|
|
2019-08-15 15:53:25 +00:00
|
|
|
from .typing import HomeAssistantType
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
DiscoveryFunctionType = Callable[[], Union[Awaitable[bool], bool]]
|
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,
|
|
|
|
connection_class: str,
|
|
|
|
) -> 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
|
2019-11-26 06:40:08 +00:00
|
|
|
self.CONNECTION_CLASS = connection_class # pylint: disable=invalid-name
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Dict[str, Any]:
|
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(
|
|
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Dict[str, Any]:
|
2018-09-21 14:34:37 +00:00
|
|
|
"""Confirm setup."""
|
|
|
|
if user_input is None:
|
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.
|
2020-06-15 11:38:38 +00:00
|
|
|
assert self.hass is not None
|
2018-09-21 14:34:37 +00:00
|
|
|
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(
|
|
|
|
self, discovery_info: Dict[str, Any]
|
|
|
|
) -> Dict[str, Any]:
|
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
|
2019-05-31 18:58:48 +00:00
|
|
|
async_step_homekit = async_step_discovery
|
2019-05-30 21:08:05 +00:00
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_import(self, _: Optional[Dict[str, Any]]) -> Dict[str, Any]:
|
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.
|
2020-06-15 11:38:38 +00:00
|
|
|
assert self.hass is not None
|
2020-06-10 20:46:14 +00:00
|
|
|
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,
|
|
|
|
connection_class: str,
|
|
|
|
) -> None:
|
|
|
|
"""Register flow for discovered integrations that not require auth."""
|
|
|
|
|
|
|
|
class DiscoveryFlow(DiscoveryFlowHandler):
|
|
|
|
"""Discovery flow handler."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(domain, title, discovery_function, connection_class)
|
|
|
|
|
|
|
|
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(
|
|
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Dict[str, Any]:
|
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
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
assert self.hass is not None
|
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(
|
|
|
|
hass: HomeAssistantType, entry: config_entries.ConfigEntry
|
|
|
|
) -> 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"])
|