2018-11-28 21:20:13 +00:00
|
|
|
"""Config flow for OwnTracks."""
|
2019-12-12 15:46:33 +00:00
|
|
|
import secrets
|
|
|
|
|
2018-11-28 21:20:13 +00:00
|
|
|
from homeassistant import config_entries
|
2019-12-09 11:11:27 +00:00
|
|
|
from homeassistant.const import CONF_WEBHOOK_ID
|
2018-11-28 21:20:13 +00:00
|
|
|
|
2019-11-11 20:30:00 +00:00
|
|
|
from .const import DOMAIN # noqa pylint: disable=unused-import
|
2019-11-18 15:47:30 +00:00
|
|
|
from .helper import supports_encryption
|
2019-11-11 20:30:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SECRET = "secret"
|
|
|
|
CONF_CLOUDHOOK = "cloudhook"
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
|
2019-11-11 20:30:00 +00:00
|
|
|
class OwnTracksFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2018-11-28 21:20:13 +00:00
|
|
|
"""Set up OwnTracks."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a user initiated set up flow to create OwnTracks webhook."""
|
|
|
|
if self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="one_instance_allowed")
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
if user_input is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="user")
|
2018-11-28 21:20:13 +00:00
|
|
|
|
2019-05-14 09:59:11 +00:00
|
|
|
webhook_id, webhook_url, cloudhook = await self._get_webhook_id()
|
2018-11-28 21:20:13 +00:00
|
|
|
|
2019-12-12 15:46:33 +00:00
|
|
|
secret = secrets.token_hex(16)
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
if supports_encryption():
|
2019-11-11 20:30:00 +00:00
|
|
|
secret_desc = f"The encryption key is {secret} (on Android under preferences -> advanced)"
|
2018-11-28 21:20:13 +00:00
|
|
|
else:
|
2019-11-11 20:30:00 +00:00
|
|
|
secret_desc = "Encryption is not supported because nacl is not installed."
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title="OwnTracks",
|
|
|
|
data={
|
|
|
|
CONF_WEBHOOK_ID: webhook_id,
|
2019-05-14 09:59:11 +00:00
|
|
|
CONF_SECRET: secret,
|
|
|
|
CONF_CLOUDHOOK: cloudhook,
|
2018-11-28 21:20:13 +00:00
|
|
|
},
|
|
|
|
description_placeholders={
|
2019-07-31 19:25:30 +00:00
|
|
|
"secret": secret_desc,
|
|
|
|
"webhook_url": webhook_url,
|
2019-11-11 20:30:00 +00:00
|
|
|
"android_url": "https://play.google.com/store/apps/details?id=org.owntracks.android",
|
2019-07-31 19:25:30 +00:00
|
|
|
"ios_url": "https://itunes.apple.com/us/app/owntracks/id692424691?mt=8",
|
2019-10-02 16:34:07 +00:00
|
|
|
"docs_url": "https://www.home-assistant.io/integrations/owntracks/",
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input):
|
|
|
|
"""Import a config flow from configuration."""
|
2019-11-11 20:30:00 +00:00
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="one_instance_allowed")
|
2019-05-14 09:59:11 +00:00
|
|
|
webhook_id, _webhook_url, cloudhook = await self._get_webhook_id()
|
2019-12-12 15:46:33 +00:00
|
|
|
secret = secrets.token_hex(16)
|
2018-11-28 21:20:13 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title="OwnTracks",
|
|
|
|
data={
|
|
|
|
CONF_WEBHOOK_ID: webhook_id,
|
2019-05-14 09:59:11 +00:00
|
|
|
CONF_SECRET: secret,
|
|
|
|
CONF_CLOUDHOOK: cloudhook,
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
2019-05-14 09:59:11 +00:00
|
|
|
|
|
|
|
async def _get_webhook_id(self):
|
|
|
|
"""Generate webhook ID."""
|
|
|
|
webhook_id = self.hass.components.webhook.async_generate_id()
|
2019-12-05 08:28:56 +00:00
|
|
|
if 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-05-14 09:59:11 +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-05-14 09:59:11 +00:00
|
|
|
cloudhook = False
|
|
|
|
|
|
|
|
return webhook_id, webhook_url, cloudhook
|