Move imports to top for tank_utility (#29119)

This commit is contained in:
springstan 2019-11-27 01:02:36 +01:00 committed by Paulus Schoutsen
parent 06231e7ac2
commit 555190990a

View File

@ -4,14 +4,14 @@ import datetime
import logging import logging
import requests import requests
import tank_utility
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_DEVICES, CONF_EMAIL, CONF_PASSWORD from homeassistant.const import CONF_DEVICES, CONF_EMAIL, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = datetime.timedelta(hours=1) SCAN_INTERVAL = datetime.timedelta(hours=1)
@ -41,14 +41,13 @@ SENSOR_ATTRS = [
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Tank Utility sensor.""" """Set up the Tank Utility sensor."""
from tank_utility import auth
email = config.get(CONF_EMAIL) email = config.get(CONF_EMAIL)
password = config.get(CONF_PASSWORD) password = config.get(CONF_PASSWORD)
devices = config.get(CONF_DEVICES) devices = config.get(CONF_DEVICES)
try: try:
token = auth.get_token(email, password) token = tank_utility.auth.get_token(email, password)
except requests.exceptions.HTTPError as http_error: except requests.exceptions.HTTPError as http_error:
if ( if (
http_error.response.status_code http_error.response.status_code
@ -109,19 +108,20 @@ class TankUtilitySensor(Entity):
Flatten dictionary to map device to map of device data. Flatten dictionary to map device to map of device data.
""" """
from tank_utility import auth, device
data = {} data = {}
try: try:
data = device.get_device_data(self._token, self.device) data = tank_utility.device.get_device_data(self._token, self.device)
except requests.exceptions.HTTPError as http_error: except requests.exceptions.HTTPError as http_error:
if ( if (
http_error.response.status_code http_error.response.status_code
== requests.codes.unauthorized # pylint: disable=no-member == requests.codes.unauthorized # pylint: disable=no-member
): ):
_LOGGER.info("Getting new token") _LOGGER.info("Getting new token")
self._token = auth.get_token(self._email, self._password, force=True) self._token = tank_utility.auth.get_token(
data = device.get_device_data(self._token, self.device) self._email, self._password, force=True
)
data = tank_utility.device.get_device_data(self._token, self.device)
else: else:
raise http_error raise http_error
data.update(data.pop("device", {})) data.update(data.pop("device", {}))