aioamazondevices 6.1.2__tar.gz → 6.1.3__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: 6.1.2
3
+ Version: 6.1.3
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 = "6.1.2"
3
+ version = "6.1.3"
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__ = "6.1.2"
3
+ __version__ = "6.1.3"
4
4
 
5
5
 
6
6
  from .api import AmazonDevice, AmazonEchoApi
@@ -20,6 +20,7 @@ from aiohttp import (
20
20
  ClientConnectorError,
21
21
  ClientResponse,
22
22
  ClientSession,
23
+ ContentTypeError,
23
24
  )
24
25
  from bs4 import BeautifulSoup, Tag
25
26
  from langcodes import Language, standardize_tag
@@ -534,7 +535,7 @@ class AmazonEchoApi:
534
535
  input_data=body,
535
536
  json_data=True,
536
537
  )
537
- resp_json = await raw_resp.json()
538
+ resp_json = await self._response_to_json(raw_resp)
538
539
 
539
540
  if raw_resp.status != HTTPStatus.OK:
540
541
  msg = resp_json["response"]["error"]["message"]
@@ -599,7 +600,7 @@ class AmazonEchoApi:
599
600
  json_data=True,
600
601
  )
601
602
 
602
- return cast("dict", await raw_resp.json())
603
+ return await self._response_to_json(raw_resp)
603
604
 
604
605
  async def _get_sensors_states(
605
606
  self,
@@ -655,6 +656,19 @@ class AmazonEchoApi:
655
656
 
656
657
  return device_sensors
657
658
 
659
+ async def _response_to_json(self, raw_resp: ClientResponse) -> dict[str, Any]:
660
+ """Convert response to JSON, if possible."""
661
+ try:
662
+ data = await raw_resp.json(loads=orjson.loads)
663
+ if not data:
664
+ _LOGGER.warning("Empty JSON data received")
665
+ data = {}
666
+ return cast("dict[str, Any]", data)
667
+ except ContentTypeError as exc:
668
+ raise ValueError("Response not in JSON format") from exc
669
+ except orjson.JSONDecodeError as exc:
670
+ raise ValueError("Response with corrupted JSON format") from exc
671
+
658
672
  async def login_mode_interactive(self, otp_code: str) -> dict[str, Any]:
659
673
  """Login to Amazon interactively via OTP."""
660
674
  _LOGGER.debug(
@@ -756,7 +770,7 @@ class AmazonEchoApi:
756
770
  method=HTTPMethod.GET,
757
771
  url=f"https://alexa.amazon.{self._domain}/api/welcome",
758
772
  )
759
- json_data = await raw_resp.json()
773
+ json_data = await self._response_to_json(raw_resp)
760
774
  return cast(
761
775
  "str", json_data.get("alexaHostName", f"alexa.amazon.{self._domain}")
762
776
  )
@@ -802,8 +816,7 @@ class AmazonEchoApi:
802
816
  url=f"https://alexa.amazon.{self._domain}{URI_DEVICES}",
803
817
  )
804
818
 
805
- response_data = await raw_resp.text()
806
- json_data = {} if len(response_data) == 0 else await raw_resp.json()
819
+ json_data = await self._response_to_json(raw_resp)
807
820
 
808
821
  _LOGGER.debug("JSON devices data: %s", scrub_fields(json_data))
809
822
 
@@ -865,7 +878,7 @@ class AmazonEchoApi:
865
878
  )
866
879
  return False
867
880
 
868
- resp_json = await raw_resp.json()
881
+ resp_json = await self._response_to_json(raw_resp)
869
882
  if not (authentication := resp_json.get("authentication")):
870
883
  _LOGGER.debug('Session not authenticated: reply missing "authentication"')
871
884
  return False
@@ -1124,7 +1137,7 @@ class AmazonEchoApi:
1124
1137
  _LOGGER.debug("Failed to refresh data")
1125
1138
  return False, {}
1126
1139
 
1127
- json_response = await raw_resp.json()
1140
+ json_response = await self._response_to_json(raw_resp)
1128
1141
  _LOGGER.debug("Refresh data json:\n%s ", json_response)
1129
1142
 
1130
1143
  if data_type == REFRESH_ACCESS_TOKEN and (
@@ -1132,7 +1145,7 @@ class AmazonEchoApi:
1132
1145
  ):
1133
1146
  self._login_stored_data[REFRESH_ACCESS_TOKEN] = new_token
1134
1147
  self.expires_in = datetime.now(tz=UTC).timestamp() + int(
1135
- json_response.get("expires_in")
1148
+ json_response.get("expires_in", 0)
1136
1149
  )
1137
1150
  return True, json_response
1138
1151