Skip to content

Commit

Permalink
improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Dec 20, 2022
1 parent 1306d60 commit 38839c5
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 136 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.2

- Improve code quality

## 0.1.1

- Fix HACS manifest
Expand Down
14 changes: 9 additions & 5 deletions custom_components/polar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL, Platform
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_NAME,
CONF_SCAN_INTERVAL,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
Expand All @@ -20,13 +27,10 @@
ATTR_RECHARGE_DATA,
ATTR_SLEEP_DATA,
ATTR_USER_DATA,
CONF_ACCESS_TOKEN,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_USER_ID,
DOMAIN,
)
from .lib.accesslink import AccessLink
from .polaraccesslink.accesslink import AccessLink

_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR]
Expand Down
13 changes: 8 additions & 5 deletions custom_components/polar/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@

from homeassistant import config_entries
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_NAME,
CONF_SCAN_INTERVAL,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.config_entry_oauth2_flow import _decode_jwt, _encode_jwt

from .const import (
AUTH_CALLBACK_NAME,
AUTH_CALLBACK_PATH,
CONF_ACCESS_TOKEN,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_USER_ID,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
)
from .lib.accesslink import AccessLink
from .polaraccesslink.accesslink import AccessLink

_LOGGER = logging.getLogger(__name__)

Expand Down
4 changes: 0 additions & 4 deletions custom_components/polar/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

DOMAIN = "polar"

CONF_CLIENT_ID = "client_id"
CONF_CLIENT_SECRET = "client_secret"
CONF_USER_ID = "user_id"
CONF_ACCESS_TOKEN = "access_token"
DEFAULT_SCAN_INTERVAL = 30


ATTR_EXERCISE_DATA = "exercisedata"
ATTR_SLEEP_DATA = "sleepdata"
ATTR_RECHARGE_DATA = "rechargedata"
Expand Down
5 changes: 0 additions & 5 deletions custom_components/polar/lib/endpoints/__init__.py

This file was deleted.

47 changes: 0 additions & 47 deletions custom_components/polar/lib/endpoints/users.py

This file was deleted.

5 changes: 3 additions & 2 deletions custom_components/polar/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"codeowners": ["@Aohzan"],
"config_flow": true,
"requirements": ["isodate==0.6.1"],
"dependencies": ["application_credentials", "http"],
"dependencies": ["http"],
"iot_class": "cloud_polling",
"version": "0.1.0"
"integration_type": "service",
"version": "0.1.2"
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
from datetime import datetime
import json
import logging
from os import path

import isodate

from . import endpoints
from .endpoints.daily_activity import DailyActivity
from .endpoints.physical_info import PhysicalInfo
from .endpoints.pull_notifications import PullNotifications
from .endpoints.training_data import TrainingData
from .endpoints.users import Users
from .oauth2 import OAuth2Client

AUTHORIZATION_URL = "https://flow.polar.com/oauth2/authorization"
Expand Down Expand Up @@ -37,11 +42,11 @@ def __init__(self, client_id, client_secret, redirect_url=None):
client_secret=client_secret,
)

self.users = endpoints.Users(oauth=self.oauth)
self.pull_notifications = endpoints.PullNotifications(oauth=self.oauth)
self.training_data = endpoints.TrainingData(oauth=self.oauth)
self.physical_info = endpoints.PhysicalInfo(oauth=self.oauth)
self.daily_activity = endpoints.DailyActivity(oauth=self.oauth)
self.users = Users(oauth=self.oauth)
self.pull_notifications = PullNotifications(oauth=self.oauth)
self.training_data = TrainingData(oauth=self.oauth)
self.physical_info = PhysicalInfo(oauth=self.oauth)
self.daily_activity = DailyActivity(oauth=self.oauth)

def get_authorization_url(self, state=None):
"""Get the authorization url for the client."""
Expand Down Expand Up @@ -99,10 +104,17 @@ def get_daily_activities(self, user_id, access_token, state_file_path):
)

if not transaction:
_LOGGER.debug("No new daily activity available, get from backup file")
try:
state_file = open(state_file_path, encoding="utf-8")
activities = json.loads(state_file.read())
if path.isfile(state_file_path):
_LOGGER.debug(
"No new daily activity available, get from backup file"
)
with open(state_file_path, encoding="utf-8") as state_file:
activities = json.loads(state_file.read())
else:
_LOGGER.debug(
"No daily activity available, will try for the next sync"
)
except OSError as exc:
_LOGGER.error(
"Unable to get daily activities from backup file %s: %s",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Endpoints for Polar Access Link."""
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
"""Training data."""
from .resource import Resource
from .training_data_transaction import TrainingDataTransaction


class TrainingData(Resource):
"""This resource allows partners to access their users' training data.
https://www.polar.com/accesslink-api/?http#training-data
"""
"""This resource allows partners to access their users' training data."""

def create_transaction(self, user_id, access_token):
"""Initiate exercise transaction
Check for new training data and create a new transaction if data is available.
:param user_id: id of the user
:param access_token: access token of the user
"""
"""Initiate exercise transaction."""
response = self._post(
endpoint=f"/users/{user_id}/exercise-transactions",
access_token=access_token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@


class TrainingDataTransaction(Transaction):
"""Training data transaction."""

def list_exercises(self):
"""Retrieve list of urls to available exercises."""
return self._get(
endpoint=None, url=self.transaction_url, access_token=self.access_token
)

def get_exercise_summary(self, url):
"""Retrieve training session summary data
:param url: url of the exercise entity
"""
"""Retrieve training session summary data."""
return self._get(endpoint=None, url=url, access_token=self.access_token)

def get_gpx(self, url):
"""Retrieve training session summary data in GPX format
:param url: url of the exercise entity
"""
"""Retrieve training session summary data in GPX format."""
return self._get(
endpoint=None,
url=url + "/gpx",
Expand All @@ -29,10 +25,7 @@ def get_gpx(self, url):
)

def get_tcx(self, url):
"""Retrieve training session summary data in TCX format
:param url: url of the exercise entity
"""
"""Retrieve training session summary data in TCX format."""
return self._get(
endpoint=None,
url=url + "/tcx",
Expand All @@ -41,26 +34,17 @@ def get_tcx(self, url):
)

def get_heart_rate_zones(self, url):
"""Retrieve heart rate zones in training session
:param url: url of the exercise entity
"""
"""Retrieve heart rate zones in training session."""
return self._get(
endpoint=None, url=url + "/heart-rate-zones", access_token=self.access_token
)

def get_available_samples(self, url):
"""Retrieve list of urls to available samples in training session
:param url: url of the exercise entity
"""
"""Retrieve list of urls to available samples in training session."""
return self._get(
endpoint=None, url=url + "/samples", access_token=self.access_token
)

def get_samples(self, url):
"""Retrieve sample data of given type
:param url: url pointing to single sample type data
"""
"""Retrieve sample data of given type."""
return self._get(endpoint=None, url=url, access_token=self.access_token)
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""Generic transaction."""
from .resource import Resource


class Transaction(Resource):
"""Generic transaction."""

def __init__(self, oauth, transaction_url, user_id, access_token):
"""Init the transaction object."""
super().__init__(oauth)
self.transaction_url = transaction_url
self.user_id = user_id
self.access_token = access_token

def commit(self):
"""Commit the transaction
This should be done after retrieving data from the transaction.
"""
"""Commit the transaction."""
return self._put(
endpoint=None, url=self.transaction_url, access_token=self.access_token
)
28 changes: 28 additions & 0 deletions custom_components/polar/polaraccesslink/endpoints/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Users."""
import uuid

from .resource import Resource


class Users(Resource):
"""This resource provides all the necessary functions to manage users."""

def register(self, access_token, member_id=uuid.uuid4().hex):
"""Registration."""
return self._post(
endpoint="/users", access_token=access_token, json={"member-id": member_id}
)

def delete(self, user_id, access_token):
"""De-registration."""
return self._delete(
endpoint=f"/users/{user_id}",
access_token=access_token,
)

def get_information(self, user_id, access_token):
"""List user's basic information."""
return self._get(
endpoint=f"/users/{user_id}",
access_token=access_token,
)
Loading

0 comments on commit 38839c5

Please sign in to comment.