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.
- {yoto_api-4.4.0 → yoto_api-4.4.2}/PKG-INFO +1 -1
- {yoto_api-4.4.0 → yoto_api-4.4.2}/setup.py +1 -1
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_client.py +13 -0
- yoto_api-4.4.2/tests/test_concurrent_refresh.py +54 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/client.py +43 -35
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/PKG-INFO +1 -1
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/SOURCES.txt +1 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/AUTHORS.rst +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/CONTRIBUTING.rst +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/HISTORY.rst +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/LICENSE +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/MANIFEST.in +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/README.md +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/requirements.txt +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/setup.cfg +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/conftest.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/conftest.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_basic.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_mqtt.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_shapes.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/e2e/test_writes.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_account.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_capabilities.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_models.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_mqtt_client.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_mqtt_parser.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_status_adapter.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/tests/test_utils.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Card.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Group.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/Token.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/_coerce.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/account.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/auth.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/capabilities.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/const.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/exceptions.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/config.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/device.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/event.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/info.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/player.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/models/status.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/client.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/mqtt/parser.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/py.typed +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/__init__.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/client.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/endpoints.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/rest/requests.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/status_adapter.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api/utils.py +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/dependency_links.txt +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/not-zip-safe +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/requires.txt +0 -0
- {yoto_api-4.4.0 → yoto_api-4.4.2}/yoto_api.egg-info/top_level.txt +0 -0
|
@@ -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
|
-
|
|
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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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 =
|
|
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 =
|
|
349
|
-
card.description =
|
|
350
|
-
card.author =
|
|
351
|
-
card.category =
|
|
352
|
-
card.cover_image_large =
|
|
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 =
|
|
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 =
|
|
373
|
-
chapter.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 =
|
|
383
|
-
track.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 =
|
|
386
|
-
track.channels =
|
|
387
|
-
track.type =
|
|
388
|
-
track.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.
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|