From 718caaad024a03596e3b481fdbb2c584bab7790d Mon Sep 17 00:00:00 2001 From: cdpuk <24318424+cdpuk@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:50:08 +0000 Subject: [PATCH] Provide auth token for device status updates (#45) --- custom_components/bestway/bestway/api.py | 28 ++++++++---------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/custom_components/bestway/bestway/api.py b/custom_components/bestway/bestway/api.py index d013f49..a0c7cf1 100644 --- a/custom_components/bestway/bestway/api.py +++ b/custom_components/bestway/bestway/api.py @@ -142,9 +142,7 @@ async def refresh_bindings(self) -> None: async def _get_devices(self) -> list[BestwayDevice]: """Get the list of devices available in the account.""" - headers = dict(_HEADERS) - headers["X-Gizwits-User-token"] = self._user_token - api_data = await self._do_get(f"{self._api_root}/app/bindings", headers) + api_data = await self._do_get(f"{self._api_root}/app/bindings") return [ BestwayDevice( raw["protoc"], @@ -164,7 +162,7 @@ async def fetch_data(self) -> BestwayApiResults: """Fetch the latest data for all devices.""" for did, device_info in self.devices.items(): latest_data = await self._do_get( - f"{self._api_root}/app/devdata/{did}/latest", _HEADERS + f"{self._api_root}/app/devdata/{did}/latest" ) # Get the age of the data according to the API @@ -274,7 +272,6 @@ async def spa_set_power(self, device_id: str, power: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"power": 1 if power else 0}}, ) cached_state.timestamp = int(time()) @@ -299,7 +296,6 @@ async def spa_set_heat(self, device_id: str, heat: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"heat_power": 1 if heat else 0}}, ) cached_state.timestamp = int(time()) @@ -318,7 +314,6 @@ async def spa_set_filter(self, device_id: str, filtering: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"filter_power": 1 if filtering else 0}}, ) cached_state.timestamp = int(time()) @@ -339,7 +334,6 @@ async def spa_set_locked(self, device_id: str, locked: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"locked": 1 if locked else 0}}, ) cached_state.timestamp = int(time()) @@ -355,7 +349,6 @@ async def spa_set_bubbles(self, device_id: str, bubbles: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"wave_power": 1 if bubbles else 0}}, ) cached_state.timestamp = int(time()) @@ -373,7 +366,6 @@ async def spa_set_target_temp(self, device_id: str, target_temp: int) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"temp_set": target_temp}}, ) cached_state.timestamp = int(time()) @@ -389,7 +381,6 @@ async def pool_filter_set_power(self, device_id: str, power: bool) -> None: headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"power": 1 if power else 0}}, ) cached_state.timestamp = int(time()) @@ -401,21 +392,20 @@ async def pool_filter_set_time(self, device_id: str, hours: int) -> None: raise BestwayException(f"Device '{device_id}' is not recognised") _LOGGER.debug("Setting filter timeout to %d hours", hours) - headers = dict(_HEADERS) - headers["X-Gizwits-User-token"] = self._user_token await self._do_post( f"{self._api_root}/app/control/{device_id}", - headers, {"attrs": {"time": hours}}, ) cached_state.timestamp = int(time()) cached_state.time = hours - async def _do_get(self, url: str, headers: dict[str, str]) -> dict[str, Any]: + async def _do_get(self, url: str) -> dict[str, Any]: """Make an API call to the specified URL, returning the response as a JSON object.""" + headers = dict(_HEADERS) + headers["X-Gizwits-User-token"] = self._user_token async with async_timeout.timeout(_TIMEOUT): response = await self._session.get(url, headers=headers) - response.raise_for_status() + await _raise_for_status(response) # All API responses are encoded using JSON, however the headers often incorrectly # state 'text/html' as the content type. @@ -423,10 +413,10 @@ async def _do_get(self, url: str, headers: dict[str, str]) -> dict[str, Any]: response_json: dict[str, Any] = await response.json(content_type=None) return response_json - async def _do_post( - self, url: str, headers: dict[str, str], body: dict[str, Any] - ) -> dict[str, Any]: + async def _do_post(self, url: str, body: dict[str, Any]) -> dict[str, Any]: """Make an API call to the specified URL, returning the response as a JSON object.""" + headers = dict(_HEADERS) + headers["X-Gizwits-User-token"] = self._user_token async with async_timeout.timeout(_TIMEOUT): response = await self._session.post(url, headers=headers, json=body) await _raise_for_status(response)