Files
home-assistant-core/homeassistant/components/owntracks/config_flow.py
T
Robbie Trencheny 655ada1374 mobile_app component (#21475)
* Initial pass of a mobile_app component

* Fully support encryption, validation for the webhook payloads, and other general improvements

* Return same format as original API calls

* Minor encryption fixes, logging improvements

* Migrate Owntracks to use the superior PyNaCl instead of libnacl, mark it as a requirement in mobile_app

* Add mobile_app to .coveragerc

* Dont manually b64decode on OT

* Initial requested changes

* Round two of fixes

* Initial mobile_app tests

* Dont allow making registration requests for same/existing device

* Test formatting fixes

* Add mobile_app to default_config

* Add some more keys allowed in registration payloads

* Add support for getting a single device, updating a device, getting all devices. Also change from /api/mobile_app/register to /api/mobile_app/devices

* Change device_id to fingerprint

* Next round of changes

* Add keyword args and pass context on all relevant calls

* Remove SingleDeviceView in favor of webhook type to update registration

* Only allow some properties to be updated on registrations, rename integration_data to app_data

* Add call service test, ensure events actually fire, only run the encryption tests if sodium is installed

* pylint

* Fix OwnTracks test

* Fix iteration of devices and remove device_for_webhook_id
2019-03-01 23:08:20 -08:00

79 lines
2.5 KiB
Python

"""Config flow for OwnTracks."""
from homeassistant import config_entries
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.auth.util import generate_secret
CONF_SECRET = 'secret'
def supports_encryption():
"""Test if we support encryption."""
try:
import nacl # noqa pylint: disable=unused-import
return True
except OSError:
return False
@config_entries.HANDLERS.register('owntracks')
class OwnTracksFlow(config_entries.ConfigFlow):
"""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():
return self.async_abort(reason='one_instance_allowed')
if user_input is None:
return self.async_show_form(
step_id='user',
)
webhook_id = self.hass.components.webhook.async_generate_id()
webhook_url = \
self.hass.components.webhook.async_generate_url(webhook_id)
secret = generate_secret(16)
if supports_encryption():
secret_desc = (
"The encryption key is {} "
"(on Android under preferences -> advanced)".format(secret))
else:
secret_desc = (
"Encryption is not supported because libsodium is not "
"installed.")
return self.async_create_entry(
title="OwnTracks",
data={
CONF_WEBHOOK_ID: webhook_id,
CONF_SECRET: secret
},
description_placeholders={
'secret': secret_desc,
'webhook_url': webhook_url,
'android_url':
'https://play.google.com/store/apps/details?'
'id=org.owntracks.android',
'ios_url':
'https://itunes.apple.com/us/app/owntracks/id692424691?mt=8',
'docs_url':
'https://www.home-assistant.io/components/owntracks/'
}
)
async def async_step_import(self, user_input):
"""Import a config flow from configuration."""
webhook_id = self.hass.components.webhook.async_generate_id()
secret = generate_secret(16)
return self.async_create_entry(
title="OwnTracks",
data={
CONF_WEBHOOK_ID: webhook_id,
CONF_SECRET: secret
}
)