Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas55555 authored and github-actions[bot] committed Mar 9, 2021
1 parent c005239 commit f006320
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
30 changes: 21 additions & 9 deletions custom_components/husqvarna_automower/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""The Husqvarna Automower integration."""
import asyncio
import logging
import time
Expand Down Expand Up @@ -60,6 +61,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):


class AuthenticationUpdateCoordinator(DataUpdateCoordinator):
"""Update Coordinator."""

def __init__(self, hass, username, password, api_key):
"""Initialize."""
_LOGGER.info("Inizialising UpdateCoordiantor")
Expand All @@ -69,44 +72,53 @@ def __init__(self, hass, username, password, api_key):
self.api_key = api_key
self.access_token = None
self.token_expires_at = 0
self.access_token_raw = None
self.provider = None
self.token_type = None
self.mower_api = None
self.get_token = GetAccessToken(self.api_key, self.username, self.password)
self.refresh_token = None

super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)

async def _async_update_data(self):
"""Update data via library."""
_LOGGER.info("Updating data")
if (self.access_token is None):
self.auth_api = GetAccessToken(self.api_key, self.username, self.password)
if self.access_token is None:
_LOGGER.debug("Getting new token, because Null")
try:
self.access_token_raw = await self.auth_api.async_get_access_token()
self.access_token_raw = await self.get_token.async_get_access_token()
self.access_token = self.access_token_raw["access_token"]
self.provider = self.access_token_raw["provider"]
self.token_type = self.access_token_raw["token_type"]
self.refresh_token = self.access_token_raw["refresh_token"]
self.token_expires_at = (
self.access_token_raw["expires_in"] + time.time()
)
_LOGGER.debug(f"Token expires at {self.token_expires_at} UTC")
_LOGGER.debug("Token expires at %i UTC", self.token_expires_at)
except Exception:
_LOGGER.debug(
f"Error message for UpdateFailed: {self.access_token_raw['status']}"
"Error message for UpdateFailed: %i",
self.access_token_raw["status"],
)
raise UpdateFailed("Error communicating with API")
elif self.token_expires_at < time.time():
self.auth_api = RefreshAccessToken(self.api_key, self.refresh_token)
_LOGGER.debug("Getting new token, because expired")
self.refresh_token = RefreshAccessToken(self.api_key, self.refresh_token)
try:
self.access_token_raw = await self.auth_api.async_refresh_access_token()
self.access_token_raw = (
await self.refresh_token.async_refresh_access_token()
)
self.access_token = self.access_token_raw["access_token"]
self.refresh_token = self.access_token_raw["refresh_token"]
self.token_expires_at = (
self.access_token_raw["expires_in"] + time.time()
)
_LOGGER.debug(f"Token expires at {self.token_expires_at} UTC")
_LOGGER.debug("Token expires at %i UTC", self.token_expires_at)
except Exception:
_LOGGER.debug(
f"Error message for UpdateFailed: {self.access_token_raw['status']}"
"Error message for UpdateFailed: %i",
self.access_token_raw["status"],
)
raise UpdateFailed("Error communicating with API")

Expand Down
14 changes: 8 additions & 6 deletions custom_components/husqvarna_automower/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ async def async_step_user(self, user_input=None):


async def try_connection(username, password, api_key):
"""Try to connect to Husqvarna with the given inputs."""
_LOGGER.debug("Trying to connect to Husqvarna")
auth_api = GetAccessToken(api_key, username, password)
access_token_raw = await auth_api.async_get_access_token()
_LOGGER.debug(f"Access token raw: {access_token_raw}")
_LOGGER.debug(f"Access token status: {access_token_raw['status']}")
_LOGGER.debug("Access token raw: %s", access_token_raw)
_LOGGER.debug("Access token status: %s", access_token_raw["status"])
if access_token_raw["status"] == 200:
_LOGGER.debug("Connected with the Authentication API")
access_token = access_token_raw["access_token"]
Expand All @@ -92,20 +93,21 @@ async def try_connection(username, password, api_key):
_LOGGER.error("Error 401 - Unauthorized check your credentials")
raise Exception
else:
_LOGGER.error(f"Error {access_token_raw['status']}")
_LOGGER.error("Error %s", access_token_raw["status"])
raise Exception
automower_api = GetMowerData(api_key, access_token, provider, token_type)
mower_data = await automower_api.async_mower_state()
if mower_data["status"] == 200:
_LOGGER.debug("Connected with the Automower Connect API")
elif mower_data["status"] == 403:
_LOGGER.error(
f"Error 403 - Make sure that you are connected to the Authentication API and the Automower Connect API on {HUSQVARNA_URL}"
"Error 403 - Make sure that you are connected to the Authentication API and the Automower Connect API on %s",
HUSQVARNA_URL,
)
raise Exception
else:
_LOGGER.error(f"Error {mower_data['status']}")
_LOGGER.error("Error %s", mower_data["status"])
raise Exception
_LOGGER.debug(f"Mower data: {mower_data}")
_LOGGER.debug("Mower data: %s", mower_data)
_LOGGER.debug("Successfully connected Authentication and Automower Connect API")
time.sleep(5)

0 comments on commit f006320

Please sign in to comment.