Make setup more resilient by raising ConfigNEntryNotReady on failure (#101654)
Make setup more resilient by raising ConfigNEntryNotReady on connection failurepull/101670/head
parent
3c5772c1c6
commit
c48b724dde
|
@ -1,14 +1,17 @@
|
|||
"""The loqed integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
|
||||
import aiohttp
|
||||
from loqedAPI import loqed
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN
|
||||
|
@ -27,12 +30,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
apiclient = loqed.APIClient(websession, f"http://{host}")
|
||||
api = loqed.LoqedAPI(apiclient)
|
||||
|
||||
lock = await api.async_get_lock(
|
||||
entry.data["lock_key_key"],
|
||||
entry.data["bridge_key"],
|
||||
int(entry.data["lock_key_local_id"]),
|
||||
re.sub(r"LOQED-([a-f0-9]+)\.local", r"\1", entry.data["bridge_mdns_hostname"]),
|
||||
)
|
||||
try:
|
||||
lock = await api.async_get_lock(
|
||||
entry.data["lock_key_key"],
|
||||
entry.data["bridge_key"],
|
||||
int(entry.data["lock_key_local_id"]),
|
||||
re.sub(
|
||||
r"LOQED-([a-f0-9]+)\.local", r"\1", entry.data["bridge_mdns_hostname"]
|
||||
),
|
||||
)
|
||||
except (
|
||||
asyncio.TimeoutError,
|
||||
aiohttp.ClientError,
|
||||
) as ex:
|
||||
raise ConfigEntryNotReady(f"Unable to connect to bridge at {host}") from ex
|
||||
coordinator = LoqedDataCoordinator(hass, api, lock, entry)
|
||||
await coordinator.ensure_webhooks()
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import json
|
|||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import aiohttp
|
||||
from loqedAPI import loqed
|
||||
|
||||
from homeassistant.components.loqed.const import DOMAIN
|
||||
|
@ -58,6 +59,22 @@ async def test_setup_webhook_in_bridge(
|
|||
lock.registerWebhook.assert_called_with(f"{get_url(hass)}/api/webhook/Webhook_id")
|
||||
|
||||
|
||||
async def test_cannot_connect_to_bridge_will_retry(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, lock: loqed.Lock
|
||||
):
|
||||
"""Test webhook setup in loqed bridge."""
|
||||
config: dict[str, Any] = {DOMAIN: {}}
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"loqedAPI.loqed.LoqedAPI.async_get_lock", side_effect=aiohttp.ClientError
|
||||
):
|
||||
await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_setup_cloudhook_in_bridge(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, lock: loqed.Lock
|
||||
):
|
||||
|
|
Loading…
Reference in New Issue