From b85832fc658768411e384e74309047d5b826b67f Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Sat, 12 Dec 2020 12:06:21 +0100 Subject: [PATCH 1/2] Revert "Remove aiohttp-workaround" This reverts commit 9b3239a20952cbddd2befdd0a64d06e0b8787289. --- plugwise/smile.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugwise/smile.py b/plugwise/smile.py index a5471fac0..603dd73a6 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -87,6 +87,8 @@ async def _create_session() -> aiohttp.ClientSession: self.websession = websession self._auth = aiohttp.BasicAuth(username, password=password) + # Work-around for Stretchv2-aiohttp-deflate-error, can be removed for aiohttp v3.7 + self._headers = {"Accept-Encoding": "gzip"} self._timeout = timeout self._endpoint = f"http://{host}:{str(port)}" @@ -230,7 +232,10 @@ async def request( try: with async_timeout.timeout(self._timeout): if method == "get": - resp = await self.websession.get(url, auth=self._auth) + # Work-around, see above, can be removed for aiohttp v3.7: + resp = await self.websession.get( + url, auth=self._auth, headers=self._headers + ) if method == "put": resp = await self.websession.put( url, data=data, headers=headers, auth=self._auth From 4f69ca3d385d42faf72b35bbf922199502dc7852 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Fri, 22 Jan 2021 18:51:41 +0100 Subject: [PATCH 2/2] Improve code --- plugwise/smile.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/plugwise/smile.py b/plugwise/smile.py index 603dd73a6..45cee712d 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -87,8 +87,6 @@ async def _create_session() -> aiohttp.ClientSession: self.websession = websession self._auth = aiohttp.BasicAuth(username, password=password) - # Work-around for Stretchv2-aiohttp-deflate-error, can be removed for aiohttp v3.7 - self._headers = {"Accept-Encoding": "gzip"} self._timeout = timeout self._endpoint = f"http://{host}:{str(port)}" @@ -226,25 +224,21 @@ async def request( resp = None url = f"{self._endpoint}{command}" - if headers is None: - headers = {"Content-type": "text/xml"} - try: with async_timeout.timeout(self._timeout): if method == "get": - # Work-around, see above, can be removed for aiohttp v3.7: + # Work-around for Stretchv2, should not hurt the other smiles + headers = {"Accept-Encoding": "gzip"} resp = await self.websession.get( - url, auth=self._auth, headers=self._headers + url, auth=self._auth, headers=headers ) if method == "put": + headers = {"Content-type": "text/xml"} resp = await self.websession.put( url, data=data, headers=headers, auth=self._auth ) if method == "delete": resp = await self.websession.delete(url, auth=self._auth) - if resp.status == 401: - raise InvalidAuthentication - except asyncio.TimeoutError: if retry < 1: _LOGGER.error("Timed out sending command to Plugwise: %s", command) @@ -258,6 +252,9 @@ async def request( if method == "put" and resp.status == 200: return + if resp.status == 401: + raise InvalidAuthentication + result = await resp.text() if not result or "" in result: _LOGGER.error("Smile response empty or error in %s", result)