Skip to content

Commit f40c4e7

Browse files
committed
Entity migration bug fix #28
1 parent f30bc86 commit f40c4e7

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

custom_components/wemportal/__init__.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
104104

105105
await coordinator.async_config_entry_first_refresh()
106106

107-
try:
108-
version = entry.version
109-
if version < 2:
110-
await migrate_unique_ids(hass, entry, coordinator)
111-
except Exception:
112-
await migrate_unique_ids(hass, entry, coordinator)
107+
# try:
108+
# version = entry.version
109+
# if version < 2:
110+
# await migrate_unique_ids(hass, entry, coordinator)
111+
# except Exception:
112+
# await migrate_unique_ids(hass, entry, coordinator)
113+
114+
# Is there an on_update function that we can add listener to?
115+
_LOGGER.debug("Migrating entity names for wemportal")
116+
await migrate_unique_ids(hass, entry, coordinator)
113117

114118
hass.data[DOMAIN][entry.entry_id] = {
115119
"api": api,
116120
# "config": entry.data,
117121
"coordinator": coordinator,
118122
}
119-
120123
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
121124
entry.async_on_unload(entry.add_update_listener(_async_entry_updated))
122125

@@ -129,6 +132,10 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
129132

130133
async def _async_entry_updated(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
131134
"""Handle entry updates."""
135+
_LOGGER.debug("Migrating entity names for wemportal because of config entry update")
136+
await migrate_unique_ids(
137+
hass, config_entry, hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
138+
)
132139
await hass.config_entries.async_reload(config_entry.entry_id)
133140

134141

custom_components/wemportal/const.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
CONF_MODE: Final = "mode"
1313
PLATFORMS = ["sensor", "number", "select", "switch"]
1414
REFRESH_WAIT_TIME: Final = 360
15-
DATA_GATHERING_ERROR: Final = "An error occurred while gathering parameters data. \
15+
DATA_GATHERING_ERROR: Final = "An error occurred while gathering data. \
1616
This issue should resolve by itself. If this problem persists, \
17-
open an issue at https://github.com/erikkastelec/hass-WEM-Portal/issues "
17+
open an issue at https://github.com/erikkastelec/hass-WEM-Portal/issues"
1818
DEFAULT_CONF_SCAN_INTERVAL_API_VALUE: Final = 300
1919
DEFAULT_CONF_SCAN_INTERVAL_VALUE: Final = 1800
2020
DEFAULT_CONF_LANGUAGE_VALUE: Final = "en"

custom_components/wemportal/wemportalapi.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ def api_login(self):
163163
f"Authentication Error: Encountered an unknown authentication error. Received response code: {response.status_code}, response: {response.content}"
164164
) from exc
165165

166-
def make_api_call(self, url: str, headers=None, data=None) -> reqs.Response:
166+
def make_api_call(
167+
self, url: str, headers=None, data=None, login_retry=False, delay=1
168+
) -> reqs.Response:
169+
response = None
167170
try:
168171
if not headers:
169172
headers = self.headers
@@ -175,23 +178,31 @@ def make_api_call(self, url: str, headers=None, data=None) -> reqs.Response:
175178
url, headers=headers, data=json.dumps(data)
176179
)
177180
response.raise_for_status()
178-
except reqs.exceptions.HTTPError as exc:
179-
if response.status_code == 401:
181+
except Exception as exc:
182+
if response.status_code == 401 and not login_retry:
180183
self.api_login()
181-
if not headers:
182-
headers = self.headers
183-
if not data:
184-
response = self.session.get(url, headers=headers)
185-
else:
186-
headers["Content-Type"] = "application/json"
187-
response = self.session.post(
188-
url, headers=headers, data=json.dumps(data)
189-
)
190-
response.raise_for_status()
184+
headers = headers or self.headers
185+
time.sleep(delay)
186+
response = self.make_api_call(
187+
url,
188+
headers=headers,
189+
data=data,
190+
login_retry=True,
191+
delay=delay,
192+
)
191193
else:
192-
raise WemPortalError(DATA_GATHERING_ERROR) from exc
193-
except Exception as exc:
194-
raise WemPortalError(DATA_GATHERING_ERROR) from exc
194+
try:
195+
response_data = response.json()
196+
# Status we get back from server
197+
server_status = response_data["Status"]
198+
server_message = response_data["Message"]
199+
raise WemPortalError(
200+
f"{DATA_GATHERING_ERROR} Server returned status code: {server_status} and message: {server_message}"
201+
) from exc
202+
# If there is no Status or Message in response
203+
except KeyError:
204+
raise WemPortalError(DATA_GATHERING_ERROR) from exc
205+
195206
return response
196207

197208
def get_devices(self):

0 commit comments

Comments
 (0)