11"""Data models and parsing helpers for Spot API responses."""
2+
23from __future__ import annotations
34
45import datetime
@@ -107,10 +108,18 @@ def from_json(cls, message: Mapping[str, Any]) -> "SpotMessage":
107108 if not isinstance (message , Mapping ):
108109 raise TypeError ("message must be a mapping" )
109110
110- required_fields = ["@clientUnixTime" , "id" , "messengerId" , "messengerName" , "unixTime" ]
111+ required_fields = [
112+ "@clientUnixTime" ,
113+ "id" ,
114+ "messengerId" ,
115+ "messengerName" ,
116+ "unixTime" ,
117+ ]
111118 missing_fields = [field for field in required_fields if field not in message ]
112119 if missing_fields :
113- logger .warning ("Missing expected message fields: %s" , ", " .join (missing_fields ))
120+ logger .warning (
121+ "Missing expected message fields: %s" , ", " .join (missing_fields )
122+ )
114123
115124 message_type = None
116125 if "messageType" in message :
@@ -131,7 +140,9 @@ def from_json(cls, message: Mapping[str, Any]) -> "SpotMessage":
131140
132141 date_time = _parse_datetime (message .get ("dateTime" ))
133142 raw_battery_state = message .get ("batteryState" )
134- battery_state = SpotBatteryState .from_str ("" if raw_battery_state is None else str (raw_battery_state ))
143+ battery_state = SpotBatteryState .from_str (
144+ "" if raw_battery_state is None else str (raw_battery_state )
145+ )
135146
136147 hidden = _coerce_int (message .get ("hidden" , 0 ))
137148 altitude = _coerce_int (message .get ("altitude" , 0 ))
@@ -167,9 +178,7 @@ def get_short_datetime(self) -> str:
167178 else :
168179 return datetime .datetime .fromtimestamp (
169180 unix_time_int , tz = datetime .timezone .utc
170- ).strftime (
171- "%Y-%m-%d %H:%M:%S"
172- )
181+ ).strftime ("%Y-%m-%d %H:%M:%S" )
173182
174183 return ""
175184
@@ -193,12 +202,12 @@ def _check_longitude(longitude: Optional[float]) -> float:
193202 def __str__ (self ) -> str :
194203 msg_str = "-------------------------------\n "
195204 msg_str += f"Spot Message ({ self .id } )\n "
196- msg_str += (
197- f"'{ self .messenger_name } ' { self .messenger_id } ({ self .model_id } - { self .message_type } )\n "
198- )
205+ msg_str += f"'{ self .messenger_name } ' { self .messenger_id } ({ self .model_id } - { self .message_type } )\n "
199206 msg_str += f"Battery: { self .battery_state } \n "
200207 timestamp = (
201- self .date_time .isoformat (" " ) if self .date_time is not None else self .get_short_datetime ()
208+ self .date_time .isoformat (" " )
209+ if self .date_time is not None
210+ else self .get_short_datetime ()
202211 )
203212 msg_str += f"{ timestamp } ({ self .unix_time } )\n "
204213 msg_str += self .get_location ()
@@ -228,17 +237,22 @@ def from_json(self, response: Mapping[str, Any], *, merge: bool = False) -> None
228237 raise TypeError ("response must be a mapping" )
229238
230239 if "response" not in response :
231- logger .warning ("No response payload present in Spot feed data: %s" , response )
240+ logger .warning (
241+ "No response payload present in Spot feed data: %s" , response
242+ )
232243 return
233244
234245 if "feedMessageResponse" not in response ["response" ]:
235- logger .warning ("No feedMessageResponse present in Spot feed response: %s" , response )
246+ logger .warning (
247+ "No feedMessageResponse present in Spot feed response: %s" , response
248+ )
236249 return
237250
238251 feed_message_response = response ["response" ]["feedMessageResponse" ]
239252 if not isinstance (feed_message_response , Mapping ):
240253 logger .warning (
241- "feedMessageResponse payload was not a mapping in feed %s response" , self .id
254+ "feedMessageResponse payload was not a mapping in feed %s response" ,
255+ self .id ,
242256 )
243257 return
244258 feed_json = feed_message_response .get ("feed" , {})
@@ -249,9 +263,7 @@ def from_json(self, response: Mapping[str, Any], *, merge: bool = False) -> None
249263 self .description = feed_json .get ("description" , "" )
250264 raw_status = feed_json .get ("status" )
251265 self .status = (
252- SpotFeedStatus .from_str (str (raw_status ))
253- if raw_status is not None
254- else None
266+ SpotFeedStatus .from_str (str (raw_status )) if raw_status is not None else None
255267 )
256268 self .usage = _coerce_int (feed_json .get ("usage" , 0 ))
257269 self .days_range = _coerce_int (feed_json .get ("daysRange" , 0 ))
@@ -260,9 +272,7 @@ def from_json(self, response: Mapping[str, Any], *, merge: bool = False) -> None
260272 )
261273 raw_type = feed_json .get ("type" )
262274 self .type = (
263- SpotFeedType .from_str (str (raw_type ))
264- if raw_type is not None
265- else None
275+ SpotFeedType .from_str (str (raw_type )) if raw_type is not None else None
266276 )
267277 count = _coerce_int (feed_message_response .get ("count" , 0 ))
268278
@@ -291,7 +301,9 @@ def from_json(self, response: Mapping[str, Any], *, merge: bool = False) -> None
291301 iterable : Iterable
292302 if isinstance (message_payload , Mapping ):
293303 iterable = message_payload .values ()
294- elif isinstance (message_payload , Sequence ) and not isinstance (message_payload , (str , bytes )):
304+ elif isinstance (message_payload , Sequence ) and not isinstance (
305+ message_payload , (str , bytes )
306+ ):
295307 iterable = message_payload
296308 else :
297309 logger .warning (
@@ -311,7 +323,9 @@ def from_json(self, response: Mapping[str, Any], *, merge: bool = False) -> None
311323 continue
312324 msg_id = message_json .get ("messengerId" )
313325 if not msg_id :
314- logger .warning ("Encountered message without messengerId in feed %s" , self .id )
326+ logger .warning (
327+ "Encountered message without messengerId in feed %s" , self .id
328+ )
315329 continue
316330
317331 message = SpotMessage .from_json (message_json )
@@ -335,7 +349,8 @@ def iter_messages(self, *, newest_first: bool = True) -> Iterable[SpotMessage]:
335349 all_messages .extend (device_messages )
336350
337351 all_messages .sort (
338- key = lambda message : message .date_time or datetime .datetime .min .replace (tzinfo = datetime .timezone .utc ),
352+ key = lambda message : message .date_time
353+ or datetime .datetime .min .replace (tzinfo = datetime .timezone .utc ),
339354 reverse = newest_first ,
340355 )
341356
0 commit comments