Fix bootstrap being fetched three times during unifiprotect startup (#112082)
We always fetch it to check if the device is online. Avoid fetching it again for migration by passing it to the migratorspull/111883/head
parent
ea9c969d15
commit
8d2fe73faa
|
@ -5,6 +5,7 @@ from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ServerDisconnectedError
|
from aiohttp.client_exceptions import ServerDisconnectedError
|
||||||
|
from pyunifiprotect.data import Bootstrap
|
||||||
from pyunifiprotect.exceptions import ClientError, NotAuthorized
|
from pyunifiprotect.exceptions import ClientError, NotAuthorized
|
||||||
|
|
||||||
# Import the test_util.anonymize module from the pyunifiprotect package
|
# Import the test_util.anonymize module from the pyunifiprotect package
|
||||||
|
@ -128,7 +129,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await _async_setup_entry(hass, entry, data_service)
|
await _async_setup_entry(hass, entry, data_service, bootstrap)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if await nvr_info.get_is_prerelease():
|
if await nvr_info.get_is_prerelease():
|
||||||
# If they are running a pre-release, its quite common for setup
|
# If they are running a pre-release, its quite common for setup
|
||||||
|
@ -156,9 +157,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
|
|
||||||
async def _async_setup_entry(
|
async def _async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, data_service: ProtectData
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
data_service: ProtectData,
|
||||||
|
bootstrap: Bootstrap,
|
||||||
) -> None:
|
) -> None:
|
||||||
await async_migrate_data(hass, entry, data_service.api)
|
await async_migrate_data(hass, entry, data_service.api, bootstrap)
|
||||||
|
|
||||||
await data_service.async_setup()
|
await data_service.async_setup()
|
||||||
if not data_service.last_update_success:
|
if not data_service.last_update_success:
|
||||||
|
|
|
@ -3,47 +3,39 @@ from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ServerDisconnectedError
|
|
||||||
from pyunifiprotect import ProtectApiClient
|
from pyunifiprotect import ProtectApiClient
|
||||||
from pyunifiprotect.data import NVR, Bootstrap, ProtectAdoptableDeviceModel
|
from pyunifiprotect.data import NVR, Bootstrap, ProtectAdoptableDeviceModel
|
||||||
from pyunifiprotect.exceptions import ClientError
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_data(
|
async def async_migrate_data(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, protect: ProtectApiClient
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
protect: ProtectApiClient,
|
||||||
|
bootstrap: Bootstrap,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run all valid UniFi Protect data migrations."""
|
"""Run all valid UniFi Protect data migrations."""
|
||||||
|
|
||||||
_LOGGER.debug("Start Migrate: async_migrate_buttons")
|
_LOGGER.debug("Start Migrate: async_migrate_buttons")
|
||||||
await async_migrate_buttons(hass, entry, protect)
|
await async_migrate_buttons(hass, entry, protect, bootstrap)
|
||||||
_LOGGER.debug("Completed Migrate: async_migrate_buttons")
|
_LOGGER.debug("Completed Migrate: async_migrate_buttons")
|
||||||
|
|
||||||
_LOGGER.debug("Start Migrate: async_migrate_device_ids")
|
_LOGGER.debug("Start Migrate: async_migrate_device_ids")
|
||||||
await async_migrate_device_ids(hass, entry, protect)
|
await async_migrate_device_ids(hass, entry, protect, bootstrap)
|
||||||
_LOGGER.debug("Completed Migrate: async_migrate_device_ids")
|
_LOGGER.debug("Completed Migrate: async_migrate_device_ids")
|
||||||
|
|
||||||
|
|
||||||
async def async_get_bootstrap(protect: ProtectApiClient) -> Bootstrap:
|
|
||||||
"""Get UniFi Protect bootstrap or raise appropriate HA error."""
|
|
||||||
|
|
||||||
try:
|
|
||||||
bootstrap = await protect.get_bootstrap()
|
|
||||||
except (TimeoutError, ClientError, ServerDisconnectedError) as err:
|
|
||||||
raise ConfigEntryNotReady from err
|
|
||||||
|
|
||||||
return bootstrap
|
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_buttons(
|
async def async_migrate_buttons(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, protect: ProtectApiClient
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
protect: ProtectApiClient,
|
||||||
|
bootstrap: Bootstrap,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Migrate existing Reboot button unique IDs from {device_id} to {deivce_id}_reboot.
|
"""Migrate existing Reboot button unique IDs from {device_id} to {deivce_id}_reboot.
|
||||||
|
|
||||||
|
@ -63,7 +55,6 @@ async def async_migrate_buttons(
|
||||||
_LOGGER.debug("No button entities need migration")
|
_LOGGER.debug("No button entities need migration")
|
||||||
return
|
return
|
||||||
|
|
||||||
bootstrap = await async_get_bootstrap(protect)
|
|
||||||
count = 0
|
count = 0
|
||||||
for button in to_migrate:
|
for button in to_migrate:
|
||||||
device = bootstrap.get_device_from_id(button.unique_id)
|
device = bootstrap.get_device_from_id(button.unique_id)
|
||||||
|
@ -94,7 +85,10 @@ async def async_migrate_buttons(
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_device_ids(
|
async def async_migrate_device_ids(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, protect: ProtectApiClient
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
protect: ProtectApiClient,
|
||||||
|
bootstrap: Bootstrap,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Migrate unique IDs from {device_id}_{name} format to {mac}_{name} format.
|
"""Migrate unique IDs from {device_id}_{name} format to {mac}_{name} format.
|
||||||
|
|
||||||
|
@ -119,7 +113,6 @@ async def async_migrate_device_ids(
|
||||||
_LOGGER.debug("No entities need migration to MAC address ID")
|
_LOGGER.debug("No entities need migration to MAC address ID")
|
||||||
return
|
return
|
||||||
|
|
||||||
bootstrap = await async_get_bootstrap(protect)
|
|
||||||
count = 0
|
count = 0
|
||||||
for entity in to_migrate:
|
for entity in to_migrate:
|
||||||
parts = entity.unique_id.split("_")
|
parts = entity.unique_id.split("_")
|
||||||
|
|
Loading…
Reference in New Issue