mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Add Retry for C4 API due to flakiness (#113857)
Co-authored-by: nalin29 <nalin29@github.com>
This commit is contained in:
parent
fd08b7281e
commit
a33aacfcaa
@ -30,6 +30,7 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
API_RETRY_TIMES,
|
||||||
CONF_ACCOUNT,
|
CONF_ACCOUNT,
|
||||||
CONF_CONFIG_LISTENER,
|
CONF_CONFIG_LISTENER,
|
||||||
CONF_CONTROLLER_UNIQUE_ID,
|
CONF_CONTROLLER_UNIQUE_ID,
|
||||||
@ -47,6 +48,18 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
PLATFORMS = [Platform.LIGHT, Platform.MEDIA_PLAYER]
|
PLATFORMS = [Platform.LIGHT, Platform.MEDIA_PLAYER]
|
||||||
|
|
||||||
|
|
||||||
|
async def call_c4_api_retry(func, *func_args):
|
||||||
|
"""Call C4 API function and retry on failure."""
|
||||||
|
for i in range(API_RETRY_TIMES):
|
||||||
|
try:
|
||||||
|
output = await func(*func_args)
|
||||||
|
return output
|
||||||
|
except client_exceptions.ClientError as exception:
|
||||||
|
_LOGGER.error("Error connecting to Control4 account API: %s", exception)
|
||||||
|
if i == API_RETRY_TIMES - 1:
|
||||||
|
raise ConfigEntryNotReady(exception) from exception
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Control4 from a config entry."""
|
"""Set up Control4 from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
@ -74,18 +87,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
controller_unique_id = config[CONF_CONTROLLER_UNIQUE_ID]
|
controller_unique_id = config[CONF_CONTROLLER_UNIQUE_ID]
|
||||||
entry_data[CONF_CONTROLLER_UNIQUE_ID] = controller_unique_id
|
entry_data[CONF_CONTROLLER_UNIQUE_ID] = controller_unique_id
|
||||||
|
|
||||||
director_token_dict = await account.getDirectorBearerToken(controller_unique_id)
|
director_token_dict = await call_c4_api_retry(
|
||||||
director_session = aiohttp_client.async_get_clientsession(hass, verify_ssl=False)
|
account.getDirectorBearerToken, controller_unique_id
|
||||||
|
)
|
||||||
|
|
||||||
|
director_session = aiohttp_client.async_get_clientsession(hass, verify_ssl=False)
|
||||||
director = C4Director(
|
director = C4Director(
|
||||||
config[CONF_HOST], director_token_dict[CONF_TOKEN], director_session
|
config[CONF_HOST], director_token_dict[CONF_TOKEN], director_session
|
||||||
)
|
)
|
||||||
entry_data[CONF_DIRECTOR] = director
|
entry_data[CONF_DIRECTOR] = director
|
||||||
|
|
||||||
# Add Control4 controller to device registry
|
controller_href = (await call_c4_api_retry(account.getAccountControllers))["href"]
|
||||||
controller_href = (await account.getAccountControllers())["href"]
|
entry_data[CONF_DIRECTOR_SW_VERSION] = await call_c4_api_retry(
|
||||||
entry_data[CONF_DIRECTOR_SW_VERSION] = await account.getControllerOSVersion(
|
account.getControllerOSVersion, controller_href
|
||||||
controller_href
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_, model, mac_address = controller_unique_id.split("_", 3)
|
_, model, mac_address = controller_unique_id.split("_", 3)
|
||||||
|
@ -5,6 +5,8 @@ DOMAIN = "control4"
|
|||||||
DEFAULT_SCAN_INTERVAL = 5
|
DEFAULT_SCAN_INTERVAL = 5
|
||||||
MIN_SCAN_INTERVAL = 1
|
MIN_SCAN_INTERVAL = 1
|
||||||
|
|
||||||
|
API_RETRY_TIMES = 5
|
||||||
|
|
||||||
CONF_ACCOUNT = "account"
|
CONF_ACCOUNT = "account"
|
||||||
CONF_DIRECTOR = "director"
|
CONF_DIRECTOR = "director"
|
||||||
CONF_DIRECTOR_SW_VERSION = "director_sw_version"
|
CONF_DIRECTOR_SW_VERSION = "director_sw_version"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user