From 65b0890fe43594e34cf31967b7dd97807e3ed604 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Sat, 1 Jun 2024 20:16:44 +0200 Subject: [PATCH 1/2] Implement match-cases --- plugwise/helper.py | 82 +++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/plugwise/helper.py b/plugwise/helper.py index f8176e2ca..b772a770c 100644 --- a/plugwise/helper.py +++ b/plugwise/helper.py @@ -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 @@ -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 ( "" in result and "Not started" not in result From 09633dde56e16d57e8dc040856f856d0208ddf5e Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Sun, 2 Jun 2024 09:12:00 +0200 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ffdae24..b0d44cd93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.