yoto-api 4.4.0__tar.gz → 4.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {yoto_api-4.4.0 → yoto_api-4.4.2}/PKG-INFO +1 -1
  2. {yoto_api-4.4.0 → yoto_api-4.4.2}/setup.py +1 -1
  3. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_client.py +13 -0
  4. yoto_api-4.4.2/tests/test_concurrent_refresh.py +54 -0
  5. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/client.py +43 -35
  6. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/PKG-INFO +1 -1
  7. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/SOURCES.txt +1 -0
  8. {yoto_api-4.4.0 → yoto_api-4.4.2}/AUTHORS.rst +0 -0
  9. {yoto_api-4.4.0 → yoto_api-4.4.2}/CONTRIBUTING.rst +0 -0
  10. {yoto_api-4.4.0 → yoto_api-4.4.2}/HISTORY.rst +0 -0
  11. {yoto_api-4.4.0 → yoto_api-4.4.2}/LICENSE +0 -0
  12. {yoto_api-4.4.0 → yoto_api-4.4.2}/MANIFEST.in +0 -0
  13. {yoto_api-4.4.0 → yoto_api-4.4.2}/README.md +0 -0
  14. {yoto_api-4.4.0 → yoto_api-4.4.2}/requirements.txt +0 -0
  15. {yoto_api-4.4.0 → yoto_api-4.4.2}/setup.cfg +0 -0
  16. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/__init__.py +0 -0
  17. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/conftest.py +0 -0
  18. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/__init__.py +0 -0
  19. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/conftest.py +0 -0
  20. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_basic.py +0 -0
  21. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_mqtt.py +0 -0
  22. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_shapes.py +0 -0
  23. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_writes.py +0 -0
  24. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_account.py +0 -0
  25. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_capabilities.py +0 -0
  26. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_models.py +0 -0
  27. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_mqtt_client.py +0 -0
  28. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_mqtt_parser.py +0 -0
  29. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_status_adapter.py +0 -0
  30. {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_utils.py +0 -0
  31. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Card.py +0 -0
  32. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Group.py +0 -0
  33. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Token.py +0 -0
  34. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/__init__.py +0 -0
  35. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/_coerce.py +0 -0
  36. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/account.py +0 -0
  37. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/auth.py +0 -0
  38. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/capabilities.py +0 -0
  39. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/const.py +0 -0
  40. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/exceptions.py +0 -0
  41. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/__init__.py +0 -0
  42. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/config.py +0 -0
  43. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/device.py +0 -0
  44. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/event.py +0 -0
  45. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/info.py +0 -0
  46. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/player.py +0 -0
  47. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/status.py +0 -0
  48. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/__init__.py +0 -0
  49. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/client.py +0 -0
  50. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/parser.py +0 -0
  51. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/py.typed +0 -0
  52. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/__init__.py +0 -0
  53. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/client.py +0 -0
  54. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/endpoints.py +0 -0
  55. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/requests.py +0 -0
  56. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/status_adapter.py +0 -0
  57. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/utils.py +0 -0
  58. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/dependency_links.txt +0 -0
  59. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/not-zip-safe +0 -0
  60. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/requires.txt +0 -0
  61. {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yoto_api
3
- Version: 4.4.0
3
+ Version: 4.4.2
4
4
  Summary: A python package that makes it a bit easier to work with the yoto play API. Not associated with Yoto in any way.
5
5
  Home-page: https://github.com/cdnninja/yoto_api
6
6
  Author: cdnninja
@@ -41,6 +41,6 @@ setup(
41
41
  test_suite="tests",
42
42
  tests_require=test_requirements,
43
43
  url="https://github.com/cdnninja/yoto_api",
44
- version="4.4.0",
44
+ version="4.4.2",
45
45
  zip_safe=False,
46
46
  )
@@ -143,6 +143,19 @@ class UpdateLibraryTests(_ClientTestCase):
143
143
  self.assertEqual(card.author, "Yoto")
144
144
  self.assertEqual(card.description, "A story about fear")
145
145
 
146
+ async def test_keeps_numeric_looking_strings_as_strings(self) -> None:
147
+ client = self.make_client()
148
+ client.token = fresh_token()
149
+ client._rest.get_card_library = AsyncMock(
150
+ return_value={"cards": [{"cardId": "12345", "card": {"title": "1984"}}]}
151
+ )
152
+
153
+ await client.update_library()
154
+
155
+ card = client.library["12345"]
156
+ self.assertEqual(card.id, "12345")
157
+ self.assertEqual(card.title, "1984")
158
+
146
159
 
147
160
  class MqttSurfaceTests(_ClientTestCase):
148
161
  async def test_connect_events_passes_getter_reading_live_token(self) -> None:
@@ -0,0 +1,54 @@
1
+ """Test concurrent refreshes don't cause session invalidation"""
2
+
3
+ from yoto_api.Token import Token
4
+ import asyncio
5
+ import pytz
6
+ from datetime import datetime, timedelta
7
+ from yoto_api import YotoClient
8
+ import unittest
9
+
10
+ last_seen_refresh_token = 0
11
+
12
+
13
+ async def mock_refresh(token) -> Token:
14
+ """Generates a new refresh token to use and keeps a memory of the maximum last value of the token"""
15
+ global last_seen_refresh_token
16
+ int_refresh_token = int(token.refresh_token)
17
+ if int_refresh_token != last_seen_refresh_token + 1:
18
+ raise Exception(
19
+ f"Concurrent refresh not handled correctly: expected{last_seen_refresh_token + 1}, got {int_refresh_token}"
20
+ )
21
+ last_seen_refresh_token = int_refresh_token
22
+ await asyncio.sleep(0.5)
23
+ return Token(
24
+ access_token="test",
25
+ refresh_token=str(int_refresh_token + 1),
26
+ valid_until=datetime.now(pytz.utc) + timedelta(days=30),
27
+ )
28
+
29
+
30
+ class TestConcurrentRefresh(unittest.IsolatedAsyncioTestCase):
31
+ """Creates a new YotoClient and tries to refresh the token 100 times in parallel. The lock on the token ensure that only one token refresh can be in flight at once"""
32
+
33
+ async def test_concurrent_refresh(self):
34
+ global last_seen_refresh_token
35
+ last_seen_refresh_token = 0
36
+ token = Token(
37
+ access_token="expired",
38
+ refresh_token="1",
39
+ valid_until=datetime.now(pytz.utc) - timedelta(days=30),
40
+ )
41
+ client = YotoClient()
42
+ client.token = token
43
+ client._auth.client_id = "test_client_id" # Ensure we try to refresh
44
+
45
+ client._auth.refresh = mock_refresh
46
+
47
+ tasks = [
48
+ asyncio.create_task(client.check_and_refresh_token()) for _ in range(100)
49
+ ]
50
+ await asyncio.gather(*tasks)
51
+
52
+
53
+ if __name__ == "__main__":
54
+ unittest.main()
@@ -175,6 +175,7 @@ class YotoClient:
175
175
  self._owns_session = session is None
176
176
  self._session = session or aiohttp.ClientSession()
177
177
  self._auth = Auth(self._session, client_id=client_id)
178
+ self._token_lock = asyncio.Lock()
178
179
  self._refresh_hook = refresh_hook
179
180
  self._rest = RestClient(self._session)
180
181
  self._mqtt: Optional[YotoMqttClient] = None
@@ -215,7 +216,9 @@ class YotoClient:
215
216
  return await self._auth.device_code_flow_start()
216
217
 
217
218
  async def device_code_flow_complete(self, auth_result: dict) -> Token:
218
- self.token = await self._auth.poll_for_token(auth_result)
219
+ new_token = await self._auth.poll_for_token(auth_result)
220
+ async with self._token_lock:
221
+ self.token = new_token
219
222
  if self._refresh_hook is not None:
220
223
  try:
221
224
  await _maybe_await(self._refresh_hook(self.token))
@@ -229,25 +232,30 @@ class YotoClient:
229
232
  Without a `client_id` the caller owns the OAuth lifecycle (e.g.
230
233
  HA's OAuth2Session) and syncs a token in; trust it, don't refresh.
231
234
  """
232
- if self.token is None:
233
- raise YotoError("No token available; authenticate first")
234
- if self._auth.client_id is None:
235
- if self.token.access_token is None:
236
- raise YotoError("No access token provided")
237
- return self.token
238
- if (
239
- self.token.access_token is None
240
- or self.token.valid_until is None
241
- or self.token.valid_until - timedelta(hours=1)
242
- <= datetime.datetime.now(datetime.timezone.utc)
243
- ):
244
- _LOGGER.debug("%s - access token expired or near, refreshing", DOMAIN)
245
- self.token = await self._auth.refresh(self.token)
246
- if self._refresh_hook is not None:
247
- try:
248
- await _maybe_await(self._refresh_hook(self.token))
249
- except Exception:
250
- _LOGGER.exception("%s - refresh hook callback raised", DOMAIN)
235
+ refreshed = False
236
+ async with self._token_lock:
237
+ if self.token is None:
238
+ raise YotoError("No token available; authenticate first")
239
+ if self._auth.client_id is None:
240
+ if self.token.access_token is None:
241
+ raise YotoError("No access token provided")
242
+ return self.token
243
+ if (
244
+ self.token.access_token is None
245
+ or self.token.valid_until is None
246
+ or self.token.valid_until - timedelta(hours=1)
247
+ <= datetime.datetime.now(datetime.timezone.utc)
248
+ ):
249
+ _LOGGER.debug("%s - access token expired or near, refreshing", DOMAIN)
250
+ self.token = await self._auth.refresh(self.token)
251
+ refreshed = True
252
+
253
+ if refreshed and self._refresh_hook is not None:
254
+ try:
255
+ await _maybe_await(self._refresh_hook(self.token))
256
+ except Exception:
257
+ _LOGGER.exception("%s - refresh hook callback raised", DOMAIN)
258
+
251
259
  return self.token
252
260
 
253
261
  # ─── Inventory ────────────────────────────────────────────────
@@ -338,20 +346,20 @@ class YotoClient:
338
346
  token = await self.check_and_refresh_token()
339
347
  response = await self._rest.get_card_library(token)
340
348
  for item in response.get("cards", []):
341
- card_id = get_child_value(item, "cardId")
349
+ card_id = get_raw_value(item, "cardId")
342
350
  if card_id is None:
343
351
  continue
344
352
  card = self.library.get(card_id)
345
353
  if card is None:
346
354
  card = Card(id=card_id)
347
355
  self.library[card_id] = card
348
- card.title = get_child_value(item, "card.title")
349
- card.description = get_child_value(item, "card.metadata.description")
350
- card.author = get_child_value(item, "card.metadata.author")
351
- card.category = get_child_value(item, "card.metadata.category")
352
- card.cover_image_large = get_child_value(item, "card.metadata.cover.imageL")
356
+ card.title = get_raw_value(item, "card.title")
357
+ card.description = get_raw_value(item, "card.metadata.description")
358
+ card.author = get_raw_value(item, "card.metadata.author")
359
+ card.category = get_raw_value(item, "card.metadata.category")
360
+ card.cover_image_large = get_raw_value(item, "card.metadata.cover.imageL")
353
361
  card.series_order = get_child_value(item, "card.metadata.seriesorder")
354
- card.series_title = get_child_value(item, "card.metadata.seriestitle")
362
+ card.series_title = get_raw_value(item, "card.metadata.seriestitle")
355
363
 
356
364
  async def update_card_detail(self, card_id: str) -> None:
357
365
  """GET /card/{cardId} — populate chapters/tracks on the card."""
@@ -369,8 +377,8 @@ class YotoClient:
369
377
  if chapter is None:
370
378
  chapter = Chapter(key=key)
371
379
  card.chapters[key] = chapter
372
- chapter.icon = get_child_value(chapter_item, "display.icon16x16")
373
- chapter.title = get_child_value(chapter_item, "title")
380
+ chapter.icon = get_raw_value(chapter_item, "display.icon16x16")
381
+ chapter.title = get_raw_value(chapter_item, "title")
374
382
  chapter.duration = get_child_value(chapter_item, "duration")
375
383
  for track_item in chapter_item.get("tracks", []):
376
384
  track_key = get_raw_value(track_item, "key")
@@ -379,13 +387,13 @@ class YotoClient:
379
387
  if track_key not in chapter.tracks:
380
388
  chapter.tracks[track_key] = Track(key=track_key)
381
389
  track = chapter.tracks[track_key]
382
- track.icon = get_child_value(track_item, "display.icon16x16")
383
- track.title = get_child_value(track_item, "title")
390
+ track.icon = get_raw_value(track_item, "display.icon16x16")
391
+ track.title = get_raw_value(track_item, "title")
384
392
  track.duration = get_child_value(track_item, "duration")
385
- track.format = get_child_value(track_item, "format")
386
- track.channels = get_child_value(track_item, "channels")
387
- track.type = get_child_value(track_item, "type")
388
- track.trackUrl = get_child_value(track_item, "trackUrl")
393
+ track.format = get_raw_value(track_item, "format")
394
+ track.channels = get_raw_value(track_item, "channels")
395
+ track.type = get_raw_value(track_item, "type")
396
+ track.trackUrl = get_raw_value(track_item, "trackUrl")
389
397
 
390
398
  async def update_groups(self) -> None:
391
399
  """GET /card/family/library/groups — populate self.groups.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yoto_api
3
- Version: 4.4.0
3
+ Version: 4.4.2
4
4
  Summary: A python package that makes it a bit easier to work with the yoto play API. Not associated with Yoto in any way.
5
5
  Home-page: https://github.com/cdnninja/yoto_api
6
6
  Author: cdnninja
@@ -11,6 +11,7 @@ tests/conftest.py
11
11
  tests/test_account.py
12
12
  tests/test_capabilities.py
13
13
  tests/test_client.py
14
+ tests/test_concurrent_refresh.py
14
15
  tests/test_models.py
15
16
  tests/test_mqtt_client.py
16
17
  tests/test_mqtt_parser.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes