Skip to content

Commit fbf069c

Browse files
committed
retry forever
1 parent d243086 commit fbf069c

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

custom_components/clphk/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ async def async_setup_entry(hass: HomeAssistant, entry):
5353
}
5454
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
5555
return True
56+
57+
async def async_unload_entry(hass: HomeAssistant, entry):
58+
"""Unload a config entry."""
59+
unload_ok = await hass.config_entries.async_unload_platforms(entry, ["sensor"])
60+
if unload_ok and CONF_DOMAIN in hass.data:
61+
hass.data.pop(CONF_DOMAIN)
62+
return unload_ok
63+
64+
async def async_reload_entry(hass: HomeAssistant, entry):
65+
"""Reload config entry when options or data change."""
66+
await async_unload_entry(hass, entry)
67+
await async_setup_entry(hass, entry)

custom_components/clphk/config_flow.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from homeassistant.data_entry_flow import FlowResult
1515
from homeassistant.helpers import aiohttp_client
1616
from homeassistant.helpers import config_entry_oauth2_flow
17+
from homeassistant.helpers import config_entry_flow
1718
from homeassistant.helpers.selector import (
1819
TextSelector,
1920
TextSelectorConfig,
@@ -64,11 +65,17 @@ async def async_step_init(self, user_input=None):
6465
user_input["access_token"] = token_data["access_token"]
6566
user_input["refresh_token"] = token_data["refresh_token"]
6667
user_input["access_token_expiry_time"] = token_data.get("expires_in")
67-
self._user_input.update(user_input)
68-
return self.async_create_entry(title="", data=self._user_input)
68+
# Update config entry data
69+
self.hass.config_entries.async_update_entry(
70+
self.config_entry,
71+
data={**self.config_entry.data, **user_input},
72+
)
73+
# Trigger reload
74+
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
75+
return self.async_create_entry(title=self.config_entry.title, data=user_input)
6976
except Exception as ex:
7077
_LOGGER.exception(ex)
71-
errors["base"] = "invalid_auth"
78+
errors["base"] = "auth_failed"
7279
return self.async_show_form(
7380
step_id="init",
7481
data_schema=vol.Schema({

custom_components/clphk/sensor.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,17 @@ async def api_request(
392392
_LOGGER.error(error_message)
393393

394394
# Handle all HTTP 4xx errors (client errors)
395-
if 400 <= e.status < 500:
396-
self._4xx_error_retry = self._4xx_error_retry + 1
397-
398-
if self._4xx_error_retry > HTTP_4xx_ERROR_RETRY_LIMIT:
399-
self._account_number = None
400-
self._access_token = None
401-
self._refresh_token = None
402-
self._access_token_expiry_time = None
403-
404-
_LOGGER.error('HTTP 4xx error retry limit reached')
405-
raise Exception('HTTP 4xx error retry limit reached')
395+
# if 400 <= e.status < 500:
396+
# self._4xx_error_retry = self._4xx_error_retry + 1
397+
#
398+
# if self._4xx_error_retry > HTTP_4xx_ERROR_RETRY_LIMIT:
399+
# self._account_number = None
400+
# self._access_token = None
401+
# self._refresh_token = None
402+
# self._access_token_expiry_time = None
403+
#
404+
# _LOGGER.error('HTTP 4xx error retry limit reached')
405+
# raise Exception('HTTP 4xx error retry limit reached')
406406

407407
raise e
408408

@@ -426,7 +426,7 @@ async def api_request(
426426
async def auth(self):
427427
token_lock = self._token_state["token_lock"]
428428
async with token_lock:
429-
if self._access_token_expiry_time and datetime.datetime.now(datetime.timezone.utc) > (datetime.datetime.strptime(self._access_token_expiry_time, '%Y-%m-%dT%H:%M:%S.%fZ') + datetime.timedelta(minutes=-1)).replace(tzinfo=datetime.timezone.utc):
429+
if self._access_token_expiry_time and datetime.datetime.now(datetime.timezone.utc) > datetime.datetime.strptime(self._access_token_expiry_time, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=datetime.timezone.utc):
430430
_LOGGER.debug(f"Refreshing access_token and refresh_token")
431431
response = await self.api_request(
432432
method="POST",

0 commit comments

Comments
 (0)