1- from python_snoo .containers import BabyData
1+ from datetime import datetime
2+
3+ from python_snoo .containers import Activity , BabyData , BreastfeedingActivity , DiaperActivity , DiaperTypes
24from python_snoo .exceptions import SnooBabyError
35from python_snoo .snoo import Snoo
46
@@ -8,6 +10,7 @@ def __init__(self, baby_id: str, snoo: Snoo):
810 self .baby_id = baby_id
911 self .snoo = snoo
1012 self .baby_url = f"https://api-us-east-1-prod.happiestbaby.com/us/me/v10/babies/{ self .baby_id } "
13+ self .activity_base_url = "https://api-us-east-1-prod.happiestbaby.com/cs/me/v11"
1114
1215 @property
1316 def session (self ):
@@ -21,3 +24,93 @@ async def get_status(self) -> BabyData:
2124 except Exception as ex :
2225 raise SnooBabyError from ex
2326 return BabyData .from_dict (resp )
27+
28+ async def get_activity_data (self , from_date : datetime , to_date : datetime ) -> list [Activity ]:
29+ """Get activity data for this baby including feeding and diaper changes
30+
31+ Args:
32+ from_date: Start date for activity range
33+ to_date: End date for activity range
34+
35+ Returns:
36+ List of typed Activity objects (DiaperActivity or BreastfeedingActivity)
37+ """
38+ hdrs = self .snoo .generate_snoo_auth_headers (self .snoo .tokens .aws_id )
39+
40+ url = f"{ self .activity_base_url } /babies/{ self .baby_id } /journals/grouped-tracking"
41+
42+ params = {
43+ "group" : "activity" ,
44+ "fromDateTime" : from_date .astimezone ().isoformat (timespec = "milliseconds" ),
45+ "toDateTime" : to_date .astimezone ().isoformat (timespec = "milliseconds" ),
46+ }
47+
48+ try :
49+ r = await self .session .get (url , headers = hdrs , params = params )
50+ resp = await r .json ()
51+ if r .status < 200 or r .status >= 300 :
52+ raise SnooBabyError (f"Failed to get activity data: { r .status } : { resp } . Payload: { params } " )
53+
54+ activities : list [Activity ] = []
55+ if isinstance (resp , list ):
56+ for activity in resp :
57+ activity_type = activity .get ("type" , "" ).lower ()
58+
59+ if activity_type == "diaper" :
60+ activities .append (DiaperActivity .from_dict (activity ))
61+ elif activity_type == "breastfeeding" :
62+ activities .append (BreastfeedingActivity .from_dict (activity ))
63+ else :
64+ # Other activity types exist but aren't supported yet
65+ raise SnooBabyError (f"Unknown activity type: { activity_type } " )
66+ else :
67+ raise SnooBabyError (f"Unexpected response format: { type (resp )} " )
68+
69+ return activities
70+
71+ except Exception as ex :
72+ raise SnooBabyError from ex
73+
74+ async def log_diaper_change (
75+ self ,
76+ diaper_types : list [DiaperTypes ],
77+ note : str | None = None ,
78+ start_time : datetime | None = None ,
79+ ) -> DiaperActivity :
80+ """Log a diaper change for this baby
81+
82+ Args:
83+ diaper_types (list): List of diaper types. e.g. ['pee'], ['poo'], or ['pee', 'poo']
84+ note (str, optional): Optional note about the diaper change
85+ start_time (datetime, optional): Diaper change timestamp, doesn't allow length.
86+ Defaults to current local time if not provided.
87+ """
88+
89+ if not start_time :
90+ start_time = datetime .now ()
91+
92+ # Always include the timezone indicator in the ISO string - seems to be required by the API
93+ if start_time .tzinfo is None :
94+ start_time = start_time .astimezone ()
95+
96+ hdrs = self .snoo .generate_snoo_auth_headers (self .snoo .tokens .aws_id )
97+ url = f"{ self .activity_base_url } /journals"
98+
99+ payload = {
100+ "babyId" : self .baby_id ,
101+ "data" : {"types" : [dt .value for dt in diaper_types ]},
102+ "type" : "diaper" ,
103+ "startTime" : start_time .isoformat (timespec = "milliseconds" ),
104+ }
105+
106+ if note :
107+ payload ["note" ] = note
108+
109+ try :
110+ r = await self .session .post (url , headers = hdrs , json = payload )
111+ resp = await r .json ()
112+ if r .status < 200 or r .status >= 300 :
113+ raise SnooBabyError (f"Failed to log diaper change: { r .status } : { resp } . Payload: { payload } " )
114+ return DiaperActivity .from_dict (resp )
115+ except Exception as ex :
116+ raise SnooBabyError from ex
0 commit comments