Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 4416ff8

Browse files
committed
fix: update LifeTime API and add debugging ability to API
1 parent 3d39006 commit 4416ff8

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

custom_components/lifetime_fitness/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from homeassistant.config_entries import ConfigEntry
66
from homeassistant.core import HomeAssistant
7+
from homeassistant.helpers.aiohttp_client import async_create_clientsession
78

89
from .api import Api
910
from .const import DOMAIN, VERSION, ISSUE_URL, PLATFORM, CONF_USERNAME, CONF_PASSWORD
@@ -20,7 +21,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2021
ISSUE_URL,
2122
)
2223
username, password = entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
23-
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = Api(hass, username, password)
24+
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = Api(async_create_clientsession(hass), username, password)
2425

2526
entry.async_on_unload(entry.add_update_listener(options_update_listener))
2627
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

custom_components/lifetime_fitness/api.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""API client for Life Time Fitness"""
2-
from aiohttp import ClientError, ClientResponseError, ClientConnectionError
2+
from aiohttp import ClientSession, ClientError, ClientResponseError, ClientConnectionError
33
from datetime import date
44
from http import HTTPStatus
55
import logging
66

7-
from homeassistant.helpers.aiohttp_client import async_create_clientsession
8-
97
from .const import (
108
API_AUTH_ENDPOINT,
119
API_AUTH_REQUEST_USERNAME_JSON_KEY,
@@ -54,10 +52,10 @@ def handle_authentication_response_json(response_json: dict):
5452

5553

5654
class Api:
57-
def __init__(self, hass, username: str, password: str) -> None:
55+
def __init__(self, client_session: ClientSession, username: str, password: str) -> None:
5856
self._username = username
5957
self._password = password
60-
self._clientsession = async_create_clientsession(hass)
58+
self._client_session = client_session
6159
self._sso_token = None
6260

6361
self.update_successful = True
@@ -68,7 +66,7 @@ def get_username(self):
6866

6967
async def authenticate(self):
7068
try:
71-
async with self._clientsession.post(
69+
async with self._client_session.post(
7270
API_AUTH_ENDPOINT,
7371
json={
7472
API_AUTH_REQUEST_USERNAME_JSON_KEY: self._username,
@@ -94,12 +92,15 @@ async def _get_visits_between_dates(self, start_date: date, end_date: date):
9492
raise ApiAuthRequired
9593

9694
try:
97-
async with self._clientsession.get(
95+
async with self._client_session.get(
9896
API_CLUB_VISITS_ENDPOINT_FORMATSTRING.format(
9997
start_date=start_date.strftime(API_CLUB_VISITS_ENDPOINT_DATE_FORMAT),
10098
end_date=end_date.strftime(API_CLUB_VISITS_ENDPOINT_DATE_FORMAT),
10199
),
102-
headers={API_CLUB_VISITS_AUTH_HEADER: self._sso_token},
100+
headers={
101+
API_CLUB_VISITS_AUTH_HEADER: self._sso_token,
102+
API_AUTH_REQUEST_SUBSCRIPTION_KEY_HEADER: API_AUTH_REQUEST_SUBSCRIPTION_KEY_HEADER_VALUE
103+
},
103104
) as response:
104105
response_json = await response.json()
105106
return response_json
@@ -160,3 +161,18 @@ class ApiAuthRequired(Exception):
160161

161162
class ApiAuthExpired(Exception):
162163
"""Authentication has expired"""
164+
165+
166+
# Test the API client by running this script with username and password:
167+
async def main():
168+
import sys, aiohttp
169+
username, password = sys.argv[1:]
170+
async with aiohttp.ClientSession() as client_session:
171+
api = Api(client_session, username, password)
172+
await api.authenticate()
173+
await api.update()
174+
print(api.result_json)
175+
176+
if __name__ == "__main__":
177+
import asyncio
178+
asyncio.run(main())

custom_components/lifetime_fitness/config_flow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from homeassistant.core import HomeAssistant, callback
1111
from homeassistant.data_entry_flow import FlowResult
1212
from homeassistant.exceptions import HomeAssistantError
13+
from homeassistant.helpers.aiohttp_client import async_create_clientsession
1314

1415
from .const import (
1516
DOMAIN,
@@ -37,7 +38,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
3738
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
3839
"""
3940
username, password = data[CONF_USERNAME], data[CONF_PASSWORD]
40-
api_client = Api(hass, username, password)
41+
api_client = Api(async_create_clientsession(hass), username, password)
4142

4243
try:
4344
await api_client.authenticate()

custom_components/lifetime_fitness/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
API_AUTH_STATUS_JSON_KEY = "status"
3333

3434
API_CLUB_VISITS_ENDPOINT_FORMATSTRING = \
35-
"https://myaccount.lifetimefitness.com/myaccount/api/member/clubvisits?end_date={end_date}&start_date={start_date}"
35+
"https://api.lifetimefitness.com/myaccount/member/clubvisits?end_date={end_date}&start_date={start_date}"
3636
API_CLUB_VISITS_ENDPOINT_DATE_FORMAT = "%Y-%m-%d"
3737
API_CLUB_VISITS_AUTH_HEADER = "X-LTF-SSOID"
3838
API_CLUB_VISITS_TIMESTAMP_JSON_KEY = "usageDateTime"

requirements.dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aiohttp=3

0 commit comments

Comments
 (0)