Add config flow tests for OwnTracks (#28644)

* Add config flow tests for OwnTracks

* Fix pylint

* Woops, uncomment test

* Woops again, logs removed

* Review from @MartinHjelmare + fix pylint
This commit is contained in:
Quentame authored and Martin Hjelmare committed 2019-11-11 21:30:00 +01:00
1 parent cfcacc2827
commit decab3e15b
5 files changed
+134 -26

No files matched your search

@@ -14,12 +14,12 @@ from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.setup import async_when_setup
from .const import DOMAIN
from .config_flow import CONF_SECRET
from .messages import async_handle_message
_LOGGER = logging.getLogger(__name__)
DOMAIN = "owntracks"
CONF_MAX_GPS_ACCURACY = "max_gps_accuracy"
CONF_WAYPOINT_IMPORT = "waypoints"
CONF_WAYPOINT_WHITELIST = "waypoint_whitelist"
@@ -3,6 +3,8 @@ from homeassistant import config_entries
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.auth.util import generate_secret
from .const import DOMAIN # noqa pylint: disable=unused-import
CONF_SECRET = "secret"
CONF_CLOUDHOOK = "cloudhook"
@@ -17,8 +19,7 @@ def supports_encryption():
return False
@config_entries.HANDLERS.register("owntracks")
class OwnTracksFlow(config_entries.ConfigFlow):
class OwnTracksFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Set up OwnTracks."""
VERSION = 1
@@ -36,14 +37,9 @@ class OwnTracksFlow(config_entries.ConfigFlow):
secret = generate_secret(16)
if supports_encryption():
secret_desc = (
"The encryption key is {} "
"(on Android under preferences -> advanced)".format(secret)
)
secret_desc = f"The encryption key is {secret} (on Android under preferences -> advanced)"
else:
secret_desc = (
"Encryption is not supported because libsodium is not " "installed."
)
secret_desc = "Encryption is not supported because nacl is not installed."
return self.async_create_entry(
title="OwnTracks",
@@ -55,8 +51,7 @@ class OwnTracksFlow(config_entries.ConfigFlow):
description_placeholders={
"secret": secret_desc,
"webhook_url": webhook_url,
"android_url": "https://play.google.com/store/apps/details?"
"id=org.owntracks.android",
"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/integrations/owntracks/",
},
@@ -64,6 +59,8 @@ class OwnTracksFlow(config_entries.ConfigFlow):
async def async_step_import(self, user_input):
"""Import a config flow from configuration."""
if self._async_current_entries():
return self.async_abort(reason="one_instance_allowed")
webhook_id, _webhook_url, cloudhook = await self._get_webhook_id()
secret = generate_secret(16)
return self.async_create_entry(
@@ -0,0 +1,3 @@
"""Constants for OwnTracks."""
DOMAIN = "owntracks"
@@ -107,7 +107,7 @@ def _decrypt_payload(secret, topic, ciphertext):
try:
keylen, decrypt = get_cipher()
except OSError:
_LOGGER.warning("Ignoring encrypted payload because libsodium not installed")
_LOGGER.warning("Ignoring encrypted payload because nacl not installed")
return None
if isinstance(secret, dict):
@@ -117,8 +117,7 @@ def _decrypt_payload(secret, topic, ciphertext):
if key is None:
_LOGGER.warning(
"Ignoring encrypted payload because no decryption key known "
"for topic %s",
"Ignoring encrypted payload because no decryption key known for topic %s",
topic,
)
return None
@@ -134,8 +133,7 @@ def _decrypt_payload(secret, topic, ciphertext):
return message
except ValueError:
_LOGGER.warning(
"Ignoring encrypted payload because unable to decrypt using "
"key for topic %s",
"Ignoring encrypted payload because unable to decrypt using key for topic %s",
topic,
)
return None