Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize periodic calls to aguaiot. Improve error logging. #58

Merged
merged 1 commit into from
Oct 14, 2023
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
2 changes: 1 addition & 1 deletion custom_components/aguaiot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_update_data():
"""Get the latest data."""
try:
await agua.fetch_device_information()
await agua.update()
except UnauthorizedError as e:
_LOGGER.error("Agua IOT Unauthorized: %s", e)
return False
Expand Down
24 changes: 23 additions & 1 deletion custom_components/aguaiot/aguaiot.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ async def register_app_id(self):
raise ConnectionError(str.format("Connection to {0} not possible", url))

if response.status_code != 201:
_LOGGER.error(
"Failed to register app id. Code: %s, Response: %s",
response.status_code,
response.text,
)
raise UnauthorizedError("Failed to register app id")

return True
Expand Down Expand Up @@ -198,6 +203,11 @@ async def login(self):
raise ConnectionError(str.format("Connection to {0} not possible", url))

if response.status_code != 200:
_LOGGER.error(
"Failed to login. Code: %s, Response: %s",
response.status_code,
response.text,
)
raise UnauthorizedError("Failed to login, please check credentials")

res = response.json()
Expand Down Expand Up @@ -283,6 +293,11 @@ async def fetch_devices(self):

async def fetch_device_information(self):
"""Fetch device information of heating devices"""
for dev in self.devices:
await dev.update_mapping()
await dev.update()

async def update(self):
for dev in self.devices:
await dev.update()

Expand Down Expand Up @@ -321,6 +336,11 @@ async def handle_webcall(self, method, url, payload):
await self.do_refresh_token()
return await self.handle_webcall(method, url, payload)
elif response.status_code != 200:
_LOGGER.error(
"Webcall failed. Code: %s, Response: %s",
response.status_code,
response.text,
)
return False

return response.json()
Expand Down Expand Up @@ -353,8 +373,10 @@ def __init__(
self.__register_map_dict = dict()
self.__information_dict = dict()

async def update(self):
async def update_mapping(self):
await self.__update_device_registers_mapping()

async def update(self):
await self.__update_device_information()

async def __update_device_registers_mapping(self):
Expand Down
Loading