Move imports in owntracks component (#27780)

* Move imports in owntracks component

* Fix nacl import

* Fix nacl import 2

* Fix nacl import 3

* Add helper.supports_encryption tests

* Fix tests helper 1

* Fix tests 2

* Add not_supports_encryption + get_cipher_error tests

* Code cov

* Fix nacl_not_imported test
This commit is contained in:
Quentame authored and Martin Hjelmare committed 2019-11-18 16:47:30 +01:00
1 parent f6b48dec94
commit eb23846cfe
5 files changed
+84 -13

No files matched your search

@@ -4,21 +4,12 @@ from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.auth.util import generate_secret
from .const import DOMAIN # noqa pylint: disable=unused-import
from .helper import supports_encryption
CONF_SECRET = "secret"
CONF_CLOUDHOOK = "cloudhook"
def supports_encryption():
"""Test if we support encryption."""
try:
import nacl # noqa: F401 pylint: disable=unused-import
return True
except OSError:
return False
class OwnTracksFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Set up OwnTracks."""
@@ -0,0 +1,10 @@
"""Helper for OwnTracks."""
try:
import nacl
except ImportError:
nacl = None
def supports_encryption() -> bool:
"""Test if we support encryption."""
return nacl is not None
@@ -2,6 +2,9 @@
import json
import logging
from nacl.secret import SecretBox
from nacl.encoding import Base64Encoder
from homeassistant.components import zone as zone_comp
from homeassistant.components.device_tracker import (
SOURCE_TYPE_GPS,
@@ -11,6 +14,7 @@ from homeassistant.components.device_tracker import (
from homeassistant.const import STATE_HOME
from homeassistant.util import decorator, slugify
from .helper import supports_encryption
_LOGGER = logging.getLogger(__name__)
@@ -22,8 +26,6 @@ def get_cipher():
Async friendly.
"""
from nacl.secret import SecretBox
from nacl.encoding import Base64Encoder
def decrypt(ciphertext, key):
"""Decrypt ciphertext using key."""
@@ -105,7 +107,11 @@ def _set_gps_from_zone(kwargs, location, zone):
def _decrypt_payload(secret, topic, ciphertext):
"""Decrypt encrypted payload."""
try:
keylen, decrypt = get_cipher()
if supports_encryption():
keylen, decrypt = get_cipher()
else:
_LOGGER.warning("Ignoring encrypted payload because nacl not installed")
return None
except OSError:
_LOGGER.warning("Ignoring encrypted payload because nacl not installed")
return None