Skip to content

Commit

Permalink
remove control chars from hub response
Browse files Browse the repository at this point in the history
  • Loading branch information
msp1974 committed Dec 24, 2023
1 parent 2a1eb25 commit c29e5f5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions aioWiserHeatAPI/rest_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import enum
import json
import re
from dataclasses import dataclass
from typing import Optional

Expand Down Expand Up @@ -70,6 +71,10 @@ def __init__(
self._extra_config_file = None
self._extra_config: _WiserExtraConfig = None

def remove_control_characters(self, data: str):
"""Remove control charactwers from string."""
return re.sub(r"[\x00-\x1f]", "", data)

async def _do_hub_action(
self,
action: WiserRestActionEnum,
Expand Down Expand Up @@ -117,10 +122,10 @@ async def _do_hub_action(
else:
content = await response.read()
if len(content) > 0:
response = content.decode(
"utf-8", "ignore"
).encode("utf-8")
return json.loads(response)
response = content.decode("utf-8", "ignore")
return json.loads(
self.remove_control_characters(response),
)
else:
return {}
return {}
Expand All @@ -130,6 +135,11 @@ async def _do_hub_action(
f"Connection timeout trying to communicate with Wiser Hub "
f"{self._wiser_connection_info.host} for url {url}"
) from ex
except ConnectionResetError as ex:
raise WiserHubConnectionError(
f"Connection was reset by the hub during communication "
f"{self._wiser_connection_info.host} for url {url}. Error is {ex}"
) from ex
except aiohttp.ClientResponseError as ex:
raise WiserHubConnectionError(
f"Response error trying to communicate with Wiser Hub "
Expand Down

0 comments on commit c29e5f5

Please sign in to comment.