Skip to content

Commit

Permalink
Provide auth token for device status updates (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk authored Jan 4, 2024
1 parent 948fb52 commit 718caaa
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions custom_components/bestway/bestway/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -401,32 +392,31 @@ 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.
# We have to disable the check to avoid an exception.
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)
Expand Down

0 comments on commit 718caaa

Please sign in to comment.