Skip to content

Commit

Permalink
Fix for jaroslawhartman#141: Support for multiple garmin users
Browse files Browse the repository at this point in the history
  • Loading branch information
jrast committed Oct 7, 2023
1 parent c0f9f8e commit a11d3e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions withings_sync/garmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
log = logging.getLogger("garmin")


HOME = os.getenv('HOME', '.')
GARMIN_SESSION = os.getenv('GARMIN_SESSION', HOME + '/.garmin_session')


class LoginSucceeded(Exception):
"""Used to raise on LoginSucceeded"""

Expand All @@ -27,8 +31,8 @@ def __init__(self) -> None:

def login(self, email=None, password=None):
logged_in = False
if os.path.exists('./garmin_session'):
self.client.load('./garmin_session')
if os.path.exists(GARMIN_SESSION):
self.client.load(GARMIN_SESSION)
try:
self.client.username
logged_in = True
Expand All @@ -38,7 +42,7 @@ def login(self, email=None, password=None):
if not logged_in:
try:
self.client.login(email, password)
self.client.dump('./garmin_session')
self.client.dump(GARMIN_SESSION)
except Exception as ex:
raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex))

Expand Down
6 changes: 5 additions & 1 deletion withings_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
from datetime import date, datetime
from importlib.metadata import version

# Load the environment variables from a .env (dotenv) file.
# This is done prior to importing other modules such that all variables,
# also the ones accessed in those modules, can be set in the dotenv file.
dotenv.load_dotenv()

from withings_sync.withings2 import WithingsAccount
from withings_sync.garmin import GarminConnect
from withings_sync.trainerroad import TrainerRoad
Expand All @@ -30,7 +35,6 @@ def load_variable(env_var, secrets_file):
# value read from the secrets file.
return os.getenv(env_var, value)

dotenv.load_dotenv()

GARMIN_USERNAME = load_variable('GARMIN_USERNAME', "/run/secrets/garmin_username")
GARMIN_PASSWORD = load_variable('GARMIN_PASSWORD', "/run/secrets/garmin_password")
Expand Down

0 comments on commit a11d3e6

Please sign in to comment.