aioamazondevices 3.1.23__tar.gz → 3.2.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: aioamazondevices
3
- Version: 3.1.23
3
+ Version: 3.2.1
4
4
  Summary: Python library to control Amazon devices
5
5
  License: Apache-2.0
6
6
  Author: Simone Chemelli
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "aioamazondevices"
3
- version = "3.1.23"
3
+ version = "3.2.1"
4
4
  requires-python = ">=3.12"
5
5
  description = "Python library to control Amazon devices"
6
6
  authors = [
@@ -1,6 +1,6 @@
1
1
  """aioamazondevices library."""
2
2
 
3
- __version__ = "3.1.23"
3
+ __version__ = "3.2.1"
4
4
 
5
5
 
6
6
  from .api import AmazonDevice, AmazonEchoApi
@@ -32,6 +32,7 @@ from .const import (
32
32
  AMAZON_DEVICE_TYPE,
33
33
  BIN_EXTENSION,
34
34
  CSRF_COOKIE,
35
+ DEFAULT_AGENT,
35
36
  DEFAULT_ASSOC_HANDLE,
36
37
  DEFAULT_HEADERS,
37
38
  DEVICE_TO_IGNORE,
@@ -56,6 +57,7 @@ from .exceptions import (
56
57
  CannotConnect,
57
58
  CannotRegisterDevice,
58
59
  CannotRetrieveData,
60
+ WrongCountry,
59
61
  WrongMethod,
60
62
  )
61
63
  from .utils import obfuscate_email, scrub_fields
@@ -160,7 +162,17 @@ class AmazonEchoApi:
160
162
  if not self._login_stored_data:
161
163
  return {}
162
164
 
163
- return cast("dict", self._login_stored_data["website_cookies"])
165
+ website_cookies: dict[str, Any] = self._login_stored_data["website_cookies"]
166
+ website_cookies.update(
167
+ {
168
+ "session-token": self._login_stored_data["store_authentication_cookie"][
169
+ "cookie"
170
+ ]
171
+ }
172
+ )
173
+ website_cookies.update({"lc-acbit": self._language})
174
+
175
+ return website_cookies
164
176
 
165
177
  def _serial_number(self) -> str:
166
178
  """Get or calculate device serial number."""
@@ -324,6 +336,7 @@ class AmazonEchoApi:
324
336
  url: str,
325
337
  input_data: dict[str, Any] | None = None,
326
338
  json_data: bool = False,
339
+ amazon_user_agent: bool = True,
327
340
  ) -> tuple[BeautifulSoup, ClientResponse]:
328
341
  """Return request response context data."""
329
342
  _LOGGER.debug(
@@ -336,6 +349,9 @@ class AmazonEchoApi:
336
349
 
337
350
  headers = DEFAULT_HEADERS
338
351
  headers.update({"Accept-Language": self._language})
352
+ if not amazon_user_agent:
353
+ _LOGGER.debug("Changing User-Agent to %s", DEFAULT_AGENT)
354
+ headers.update({"User-Agent": DEFAULT_AGENT})
339
355
  if self._csrf_cookie and CSRF_COOKIE not in headers:
340
356
  csrf = {CSRF_COOKIE: self._csrf_cookie}
341
357
  _LOGGER.debug("Adding <%s> to headers", csrf)
@@ -525,11 +541,37 @@ class AmazonEchoApi:
525
541
  await self._save_to_file(login_data, "login_data", JSON_EXTENSION)
526
542
  return login_data
527
543
 
544
+ async def _check_country(self) -> None:
545
+ """Check if user selected country matches Amazon account country."""
546
+ url = f"https://alexa.amazon.{self._domain}/api/users/me"
547
+ _, resp_me = await self._session_request(HTTPMethod.GET, url)
548
+
549
+ if resp_me.status != HTTPStatus.OK:
550
+ raise CannotAuthenticate
551
+
552
+ resp_me_json = await resp_me.json()
553
+ market = resp_me_json["marketPlaceDomainName"]
554
+ language = resp_me_json["marketPlaceLocale"]
555
+
556
+ _domain = f"https://www.amazon.{self._domain}"
557
+
558
+ if market != _domain or language != self._language:
559
+ _LOGGER.debug(
560
+ "Selected country <%s> doesn't matches Amazon account:\n%s\n vs \n%s",
561
+ self._login_country_code.upper(),
562
+ {"site ": _domain, "locale": self._language},
563
+ {"market": market, "locale": language},
564
+ )
565
+ raise WrongCountry
566
+
567
+ _LOGGER.debug("User selected country matches Amazon account one")
568
+
528
569
  async def _get_devices_ids(self) -> list[dict[str, str]]:
529
570
  """Retrieve devices entityId and applianceId."""
530
571
  _, raw_resp = await self._session_request(
531
572
  "GET",
532
573
  url=f"https://alexa.amazon.{self._domain}{URI_IDS}",
574
+ amazon_user_agent=False,
533
575
  )
534
576
  json_data = await raw_resp.json()
535
577
 
@@ -698,6 +740,9 @@ class AmazonEchoApi:
698
740
  self._login_stored_data = register_device
699
741
 
700
742
  _LOGGER.info("Register device: %s", scrub_fields(register_device))
743
+
744
+ await self._check_country()
745
+
701
746
  return register_device
702
747
 
703
748
  async def login_mode_stored_data(self) -> dict[str, Any]:
@@ -716,6 +761,8 @@ class AmazonEchoApi:
716
761
 
717
762
  self._client_session()
718
763
 
764
+ await self._check_country()
765
+
719
766
  return self._login_stored_data
720
767
 
721
768
  async def close(self) -> None:
@@ -86,6 +86,7 @@ DEFAULT_HEADERS = {
86
86
  "Accept-Encoding": "gzip",
87
87
  "Connection": "keep-alive",
88
88
  }
89
+ DEFAULT_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0" # noqa: E501
89
90
  CSRF_COOKIE = "csrf"
90
91
 
91
92
  NODE_DEVICES = "devices"
@@ -25,3 +25,7 @@ class CannotRegisterDevice(AmazonError):
25
25
 
26
26
  class WrongMethod(AmazonError):
27
27
  """Exception raised when the wrong login metho is used."""
28
+
29
+
30
+ class WrongCountry(AmazonError):
31
+ """Exceptio nraised when Amazon country."""