Skip to content

Commit 8d4f2e6

Browse files
authored
Merge pull request #284 from psdupvi/282-assign-gear
Add and remove gear functions
2 parents 541ce8c + a2f05de commit 8d4f2e6

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Make your selection:
4242
- **Body Composition & Weight**: 8 methods (weight tracking, body composition)
4343
- **Goals & Achievements**: 15 methods (challenges, badges, goals)
4444
- **Device & Technical**: 7 methods (device info, settings)
45-
- **Gear & Equipment**: 6 methods (gear management, tracking)
45+
- **Gear & Equipment**: 8 methods (gear management, tracking)
4646
- **Hydration & Wellness**: 9 methods (hydration, blood pressure, menstrual)
4747
- **System & Export**: 4 methods (reporting, logout, GraphQL)
4848

demo.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def __init__(self):
364364
"desc": "Track gear usage (total time used)",
365365
"key": "track_gear_usage",
366366
},
367+
"7": {
368+
"desc": "Add and remove gear to/from activity (interactive)",
369+
"key": "add_and_remove_gear_to_activity",
370+
},
367371
},
368372
},
369373
"0": {
@@ -2396,6 +2400,63 @@ def set_gear_default_data(api: Garmin) -> None:
23962400
print(f"❌ Error setting gear default: {e}")
23972401

23982402

2403+
def add_and_remove_gear_to_activity(api: Garmin) -> None:
2404+
"""Add gear to most recent activity, then remove."""
2405+
try:
2406+
device_last_used = api.get_device_last_used()
2407+
user_profile_number = device_last_used.get("userProfileNumber")
2408+
if user_profile_number:
2409+
gear_list = api.get_gear(user_profile_number)
2410+
if gear_list:
2411+
activities = api.get_activities(0, 1)
2412+
if activities:
2413+
2414+
activity_id = activities[0].get("activityId")
2415+
activity_name = activities[0].get("activityName")
2416+
for gear in gear_list:
2417+
if gear["gearStatusName"] == "active":
2418+
break
2419+
gear_uuid = gear.get("uuid")
2420+
gear_name = gear.get("displayName", "Unknown")
2421+
if gear_uuid:
2422+
# Add gear to an activity
2423+
# Correct method signature: add_gear_to_activity(gearUUID, activity_id)
2424+
call_and_display(
2425+
api.add_gear_to_activity,
2426+
gear_uuid,
2427+
activity_id,
2428+
method_name="add_gear_to_activity",
2429+
api_call_desc=f"api.add_gear_to_activity('{gear_uuid}', {activity_id}) - Add {gear_name} to {activity_name}",
2430+
)
2431+
print("✅ Gear added successfully!")
2432+
2433+
# Wait for user to check gear, then continue
2434+
input(
2435+
"Go check Garmin to confirm, then press Enter to continue"
2436+
)
2437+
2438+
# Remove gear from an activity
2439+
# Correct method signature: remove_gear_from_activity(gearUUID, activity_id)
2440+
call_and_display(
2441+
api.remove_gear_from_activity,
2442+
gear_uuid,
2443+
activity_id,
2444+
method_name="remove_gear_from_activity",
2445+
api_call_desc=f"api.remove_gear_from_activity('{gear_uuid}', {activity_id}) - Remove {gear_name} from {activity_name}",
2446+
)
2447+
print("✅ Gear removed successfully!")
2448+
else:
2449+
print("❌ No activities found")
2450+
else:
2451+
print("❌ No gear UUID found")
2452+
else:
2453+
print("ℹ️ No gear found")
2454+
else:
2455+
print("❌ Could not get user profile number")
2456+
except Exception as e:
2457+
print(f"❌ Error adding gear: {e}")
2458+
2459+
23992460
def set_activity_name_data(api: Garmin) -> None:
24002461
"""Set activity name."""
24012462
try:
@@ -3297,6 +3358,9 @@ def execute_api_call(api: Garmin, key: str) -> None:
32973358
"get_gear_activities": lambda: get_gear_activities_data(api),
32983359
"set_gear_default": lambda: set_gear_default_data(api),
32993360
"track_gear_usage": lambda: track_gear_usage_data(api),
3361+
"add_and_remove_gear_to_activity": lambda: add_and_remove_gear_to_activity(
3362+
api
3363+
),
33003364
# Hydration & Wellness
33013365
"get_hydration_data": lambda: call_and_display(
33023366
api.get_hydration_data,

garminconnect/__init__.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def __init__(
256256
self.garmin_connect_upload = "/upload-service/upload"
257257

258258
self.garmin_connect_gear = "/gear-service/gear/filterGear"
259-
self.garmin_connect_gear_baseurl = "/gear-service/gear/"
259+
self.garmin_connect_gear_baseurl = "/gear-service/gear"
260260

261261
self.garmin_request_reload_url = "/wellness-service/wellness/epoch/request"
262262

@@ -1855,13 +1855,13 @@ def get_gear(self, userProfileNumber: str) -> dict[str, Any]:
18551855
return self.connectapi(url)
18561856

18571857
def get_gear_stats(self, gearUUID: str) -> dict[str, Any]:
1858-
url = f"{self.garmin_connect_gear_baseurl}stats/{gearUUID}"
1858+
url = f"{self.garmin_connect_gear_baseurl}/stats/{gearUUID}"
18591859
logger.debug("Requesting gear stats for gearUUID %s", gearUUID)
18601860
return self.connectapi(url)
18611861

18621862
def get_gear_defaults(self, userProfileNumber: str) -> dict[str, Any]:
18631863
url = (
1864-
f"{self.garmin_connect_gear_baseurl}user/"
1864+
f"{self.garmin_connect_gear_baseurl}/user/"
18651865
f"{userProfileNumber}/activityTypes"
18661866
)
18671867
logger.debug("Requesting gear defaults for user %s", userProfileNumber)
@@ -1873,7 +1873,7 @@ def set_gear_default(
18731873
defaultGearString = "/default/true" if defaultGear else ""
18741874
method_override = "PUT" if defaultGear else "DELETE"
18751875
url = (
1876-
f"{self.garmin_connect_gear_baseurl}{gearUUID}/"
1876+
f"{self.garmin_connect_gear_baseurl}/{gearUUID}/"
18771877
f"activityType/{activityType}{defaultGearString}"
18781878
)
18791879
return self.garth.request(method_override, "connectapi", url, api=True)
@@ -2025,6 +2025,52 @@ def get_gear_activities(
20252025

20262026
return self.connectapi(url)
20272027

2028+
def add_gear_to_activity(
2029+
self, gearUUID: str, activity_id: int | str
2030+
) -> dict[str, Any]:
2031+
"""
2032+
Associates gear with an activity. Requires a gearUUID and an activity_id
2033+
2034+
Args:
2035+
gearUUID: UID for gear to add to activity. Findable though the get_gear function
2036+
activity_id: Integer ID for the activity to add the gear to
2037+
2038+
Returns:
2039+
Dictionary containing information for the added gear
2040+
"""
2041+
2042+
gearUUID = str(gearUUID)
2043+
activity_id = _validate_positive_integer(int(activity_id), "activity_id")
2044+
2045+
url = (
2046+
f"{self.garmin_connect_gear_baseurl}/link/{gearUUID}/activity/{activity_id}"
2047+
)
2048+
logger.debug("Linking gear %s to activity %s", gearUUID, activity_id)
2049+
2050+
return self.garth.put("connectapi", url).json()
2051+
2052+
def remove_gear_from_activity(
2053+
self, gearUUID: str, activity_id: int | str
2054+
) -> dict[str, Any]:
2055+
"""
2056+
Removes gear from an activity. Requires a gearUUID and an activity_id
2057+
2058+
Args:
2059+
gearUUID: UID for gear to remove from activity. Findable though the get_gear method.
2060+
activity_id: Integer ID for the activity to remove the gear from
2061+
2062+
Returns:
2063+
Dictionary containing information about the removed gear
2064+
"""
2065+
2066+
gearUUID = str(gearUUID)
2067+
activity_id = _validate_positive_integer(int(activity_id), "activity_id")
2068+
2069+
url = f"{self.garmin_connect_gear_baseurl}/unlink/{gearUUID}/activity/{activity_id}"
2070+
logger.debug("Unlinking gear %s from activity %s", gearUUID, activity_id)
2071+
2072+
return self.garth.put("connectapi", url).json()
2073+
20282074
def get_user_profile(self) -> dict[str, Any]:
20292075
"""Get all users settings."""
20302076

0 commit comments

Comments
 (0)