Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Ongoing

- Implementing code improvements as suggested in #567

## v0.38.0

- Add a reboot_gateway() function for actual Plugwise devices.
Expand Down
82 changes: 41 additions & 41 deletions plugwise/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,31 @@ async def _request(
use_headers = headers

try:
if method == "delete":
resp = await self._websession.delete(url, auth=self._auth)
if method == "get":
# Work-around for Stretchv2, should not hurt the other smiles
use_headers = {"Accept-Encoding": "gzip"}
resp = await self._websession.get(
url, headers=use_headers, auth=self._auth
)
if method == "post":
use_headers = {"Content-type": "text/xml"}
resp = await self._websession.post(
url,
headers=use_headers,
data=data,
auth=self._auth,
)
if method == "put":
use_headers = {"Content-type": "text/xml"}
resp = await self._websession.put(
url,
headers=use_headers,
data=data,
auth=self._auth,
)
match method:
case "delete":
resp = await self._websession.delete(url, auth=self._auth)
case "get":
# Work-around for Stretchv2, should not hurt the other smiles
use_headers = {"Accept-Encoding": "gzip"}
resp = await self._websession.get(
url, headers=use_headers, auth=self._auth
)
case "post":
use_headers = {"Content-type": "text/xml"}
resp = await self._websession.post(
url,
headers=use_headers,
data=data,
auth=self._auth,
)
case "put":
use_headers = {"Content-type": "text/xml"}
resp = await self._websession.put(
url,
headers=use_headers,
data=data,
auth=self._auth,
)
except (
ClientError
) as exc: # ClientError is an ancestor class of ServerTimeoutError
Expand Down Expand Up @@ -167,23 +168,22 @@ async def _request(

async def _request_validate(self, resp: ClientResponse, method: str) -> etree:
"""Helper-function for _request(): validate the returned data."""
# Command accepted gives empty body with status 202
if resp.status == 202:
return

# Cornercase for server not responding with 202
if method in ("post", "put") and resp.status == 200:
return

if resp.status == 401:
msg = "Invalid Plugwise login, please retry with the correct credentials."
LOGGER.error("%s", msg)
raise InvalidAuthentication

if resp.status == 405:
msg = "405 Method not allowed."
LOGGER.error("%s", msg)
raise ConnectionFailedError
match resp.status:
case 200:
# Cornercases for server not responding with 202
if method in ("post", "put"):
return
case 202:
# Command accepted gives empty body with status 202
return
case 401:
msg = "Invalid Plugwise login, please retry with the correct credentials."
LOGGER.error("%s", msg)
raise InvalidAuthentication
case 405:
msg = "405 Method not allowed."
LOGGER.error("%s", msg)
raise ConnectionFailedError

if not (result := await resp.text()) or (
"<error>" in result and "Not started" not in result
Expand Down