|
1 | | -"""Define a SimpliSafe account.""" |
| 1 | +"""Define functionality for interacting with the SimpliSafe API.""" |
2 | 2 | import base64 |
3 | 3 | from json.decoder import JSONDecodeError |
4 | 4 | import sys |
@@ -83,7 +83,7 @@ def __init__( |
83 | 83 | # These will get filled in after initial authentication: |
84 | 84 | self._access_token: Optional[str] = None |
85 | 85 | self._refresh_token: Optional[str] = None |
86 | | - self.subscription_data: Dict[int, dict] = {} |
| 86 | + self.subscription_data: Dict[int, Any] = {} |
87 | 87 | self.user_id: Optional[int] = None |
88 | 88 |
|
89 | 89 | # Implement a version of the request coroutine, but with backoff/retry logic: |
@@ -168,7 +168,7 @@ async def _request( |
168 | 168 |
|
169 | 169 | assert session |
170 | 170 |
|
171 | | - data: Dict[str, Any] = {} |
| 171 | + data: Union[Dict[str, Any], str] = {} |
172 | 172 | async with session.request( |
173 | 173 | method, f"{API_URL_BASE}/{endpoint}", **kwargs |
174 | 174 | ) as resp: |
@@ -268,52 +268,49 @@ async def login(self) -> None: |
268 | 268 | async def get_systems(self) -> Dict[int, Union[SystemV2, SystemV3]]: |
269 | 269 | """Get systems associated to the associated SimpliSafe account. |
270 | 270 |
|
271 | | - In the dict that is returned, the keys are the system ID and the values are |
272 | | - actual ``System`` objects. |
| 271 | + In the dict that is returned, the keys are the subscription ID and the values |
| 272 | + are actual ``System`` objects. |
273 | 273 |
|
274 | 274 | :rtype: ``Dict[int, simplipy.system.System]`` |
275 | 275 | """ |
| 276 | + systems: Dict[int, Union[SystemV2, SystemV3]] = {} |
| 277 | + |
276 | 278 | await self.update_subscription_data() |
277 | 279 |
|
278 | | - systems = {} |
| 280 | + for sid, subscription in self.subscription_data.items(): |
| 281 | + if not subscription["activated"] != 0: |
| 282 | + LOGGER.info("Skipping inactive subscription: %s", sid) |
| 283 | + continue |
279 | 284 |
|
280 | | - for system_id, subscription in self.subscription_data.items(): |
281 | | - version = subscription["location"]["system"]["version"] |
| 285 | + # if "system" not in subscription["location"]: |
| 286 | + if not subscription["location"].get("system"): |
| 287 | + LOGGER.error("Skipping subscription with missing system data: %s", sid) |
| 288 | + continue |
282 | 289 |
|
283 | 290 | system: Union[SystemV2, SystemV3] |
| 291 | + version = subscription["location"]["system"]["version"] |
284 | 292 | if version == 2: |
285 | | - system = SystemV2(self, system_id) |
| 293 | + system = SystemV2(self, sid) |
286 | 294 | else: |
287 | | - system = SystemV3(self, system_id) |
| 295 | + system = SystemV3(self, sid) |
288 | 296 |
|
289 | | - # Skip deactivated systems: |
290 | | - if not system.active: |
291 | | - LOGGER.info("Skipping deactivated system: %s", system_id) |
292 | | - continue |
293 | | - |
294 | | - # Update the system, but don't include system data itself, since it will |
295 | | - # already have been fetched when the API was first queried: |
296 | | - await system.update(include_system=False) |
297 | | - await system.generate_entities() |
298 | | - systems[system_id] = system |
| 297 | + # Update the system, but don't include subscription data itself, since it |
| 298 | + # will already have been fetched when the API was first queried: |
| 299 | + await system.update(include_subscription=False) |
| 300 | + system.generate_device_objects() |
| 301 | + systems[sid] = system |
299 | 302 |
|
300 | 303 | return systems |
301 | 304 |
|
302 | 305 | async def update_subscription_data(self) -> None: |
303 | | - """Update our internal "raw data" listing of subscriptions.""" |
| 306 | + """Get the latest subscription data.""" |
304 | 307 | subscription_resp = await self.request( |
305 | 308 | "get", f"users/{self.user_id}/subscriptions", params={"activeOnly": "true"} |
306 | 309 | ) |
307 | | - |
308 | | - for subscription in subscription_resp["subscriptions"]: |
309 | | - if "version" not in subscription["location"]["system"]: |
310 | | - LOGGER.error( |
311 | | - "Skipping location with missing system data: %s", |
312 | | - subscription["location"]["sid"], |
313 | | - ) |
314 | | - continue |
315 | | - |
316 | | - self.subscription_data[subscription["sid"]] = subscription |
| 310 | + self.subscription_data = { |
| 311 | + subscription["sid"]: subscription |
| 312 | + for subscription in subscription_resp["subscriptions"] |
| 313 | + } |
317 | 314 |
|
318 | 315 |
|
319 | 316 | async def get_api( |
|
0 commit comments