Make setup more resilient by raising ConfigNEntryNotReady on failure (#101654)

Make setup more resilient by raising ConfigNEntryNotReady on connection failure
pull/101670/head
Mike Woudenberg 2023-10-08 19:00:06 +02:00 committed by GitHub
parent 3c5772c1c6
commit c48b724dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View File

@ -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()

View File

@ -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
):