Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkastelec committed May 7, 2023
2 parents 3c9a2ff + e9c9e03 commit dcb34ad
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 9 deletions.
2 changes: 2 additions & 0 deletions custom_components/wemportal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ async def migrate_unique_ids(

change = False
for unique_id, values in data.items():
if isinstance(values, int):
continue
name_id = er.async_get_entity_id(values["platform"], DOMAIN, unique_id)
new_id = get_wemportal_unique_id(config_entry.entry_id, device_id, unique_id)
if name_id is not None:
Expand Down
25 changes: 19 additions & 6 deletions custom_components/wemportal/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" WemPortal integration coordinator """
from __future__ import annotations
from time import monotonic

import async_timeout
from homeassistant.config_entries import ConfigEntry
Expand All @@ -9,7 +10,7 @@
UpdateFailed,
)
from .exceptions import ForbiddenError, ServerError, WemPortalError
from .const import _LOGGER, DEFAULT_TIMEOUT
from .const import _LOGGER, DEFAULT_CONF_SCAN_INTERVAL_API_VALUE, DEFAULT_TIMEOUT
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from .wemportalapi import WemPortalApi

Expand All @@ -34,15 +35,22 @@ def __init__(
self.api = api
self.hass = hass
self.config_entry = config_entry
self.last_try = None
self.num_failed = 0

async def _async_update_data(self):
"""Fetch data from the wemportal api"""
if self.num_failed > 2 and monotonic() - self.last_try < DEFAULT_CONF_SCAN_INTERVAL_API_VALUE:
raise UpdateFailed("Waiting for more time to pass before retrying")
async with async_timeout.timeout(DEFAULT_TIMEOUT):
try:
return await self.hass.async_add_executor_job(self.api.fetch_data)
x = await self.hass.async_add_executor_job(self.api.fetch_data)
self.num_failed = 0
return x
except WemPortalError as exc:
self.num_failed += 1
# if isinstance(exc.__cause__, (ServerError, ForbiddenError)):
_LOGGER.error("Creating new wemportal api instance")
_LOGGER.warning("Creating new wemportal api instance", exc_info=exc)
# TODO: This is a temporary solution and should be removed when api cause from #28 is resolved
try:
new_api = WemPortalApi(
Expand All @@ -53,18 +61,23 @@ async def _async_update_data(self):
self.api = new_api
except Exception as exc2:
raise UpdateFailed from exc2
if isinstance(exc.__cause__, (ServerError, ForbiddenError)):
if isinstance(exc.__cause__, (ServerError, ForbiddenError)) and self.num_failed <= 1:
try:
return await self.hass.async_add_executor_job(
x = await self.hass.async_add_executor_job(
self.api.fetch_data
)
self.num_failed = 0
return x
except WemPortalError as exc2:
self.num_failed += 1
_LOGGER.error(
"Error fetching data from wemportal", exc_info=exc
"Error fetching data from wemportal", exc_info=exc2
)
raise UpdateFailed from exc2
else:
raise UpdateFailed from exc
# else:
# _LOGGER.error("Error fetching data from wemportal", exc_info=exc)
# raise UpdateFailed from exc
finally:
self.last_try = monotonic()
2 changes: 1 addition & 1 deletion custom_components/wemportal/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"documentation": "https://github.com/erikkastelec/hass-WEM-Portal",
"issue_tracker": "https://github.com/erikkastelec/hass-WEM-Portal/issues",
"dependencies": [],
"version": "1.5.2",
"version": "1.5.3",
"codeowners": [
"@erikkastelec"
],
Expand Down
4 changes: 4 additions & 0 deletions custom_components/wemportal/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async def async_setup_platform(
entities: list[WemPortalNumber] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "number":
entities.append(
WemPortalNumber(
Expand All @@ -46,6 +48,8 @@ async def async_setup_entry(
entities: list[WemPortalNumber] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "number":
entities.append(
WemPortalNumber(
Expand Down
4 changes: 4 additions & 0 deletions custom_components/wemportal/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ async def async_setup_platform(
entities: list[WemPortalSelect] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "select":
entities.append(
WemPortalSelect(
Expand All @@ -47,6 +49,8 @@ async def async_setup_entry(
entities: list[WemPortalSelect] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "select":
entities.append(
WemPortalSelect(
Expand Down
4 changes: 4 additions & 0 deletions custom_components/wemportal/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ async def async_setup_platform(
entities: list[WemPortalSensor] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "sensor":
entities.append(
WemPortalSensor(
Expand All @@ -51,6 +53,8 @@ async def async_setup_entry(
entities: list[WemPortalSensor] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "sensor":
entities.append(
WemPortalSensor(
Expand Down
4 changes: 4 additions & 0 deletions custom_components/wemportal/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async def async_setup_platform(
entities: list[WemPortalSwitch] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "switch":
entities.append(
WemPortalSwitch(
Expand All @@ -46,6 +48,8 @@ async def async_setup_entry(
entities: list[WemPortalSwitch] = []
for device_id, entity_data in coordinator.data.items():
for unique_id, values in entity_data.items():
if isinstance(values, int):
continue
if values["platform"] == "switch":
entities.append(
WemPortalSwitch(
Expand Down
11 changes: 9 additions & 2 deletions custom_components/wemportal/wemportalapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,17 @@ def get_devices(self):
"Type": module["Type"],
"Name": module["Name"],
}
self.data[device["ID"]]["ConnectionStatus"] = device["ConnectionStatus"]

# TODO: remove when we implement multiple device support
break

def get_parameters(self):
for device_id in self.data.keys():
_LOGGER.debug("Fetching api parameters data")
if self.data[device_id]["ConnectionStatus"] != 0:
continue
_LOGGER.debug("Fetching api parameters data for device %s", device_id)
_LOGGER.debug(self.data)
delete_candidates = []
for key, values in self.modules[device_id].items():
data = {
Expand All @@ -293,7 +300,7 @@ def get_parameters(self):
response = self.make_api_call(
"https://www.wemportal.com/app/EventType/Read", data=data
)

_LOGGER.debug(response.json())
parameters = {}
try:
for parameter in response.json()["Parameters"]:
Expand Down

0 comments on commit dcb34ad

Please sign in to comment.