Fix fetching upgrade data during setup of Synology DSM (#73507)

This commit is contained in:
Michael 2022-06-14 23:18:59 +02:00 committed by GitHub
parent 3bbb4c052c
commit 103a6266a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,6 @@ from synology_dsm.api.surveillance_station import SynoSurveillanceStation
from synology_dsm.exceptions import ( from synology_dsm.exceptions import (
SynologyDSMAPIErrorException, SynologyDSMAPIErrorException,
SynologyDSMException, SynologyDSMException,
SynologyDSMLoginFailedException,
SynologyDSMRequestException, SynologyDSMRequestException,
) )
@ -32,7 +31,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from .const import CONF_DEVICE_TOKEN from .const import CONF_DEVICE_TOKEN, SYNOLOGY_CONNECTION_EXCEPTIONS
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -72,6 +71,10 @@ class SynoApi:
async def async_setup(self) -> None: async def async_setup(self) -> None:
"""Start interacting with the NAS.""" """Start interacting with the NAS."""
await self._hass.async_add_executor_job(self._setup)
def _setup(self) -> None:
"""Start interacting with the NAS in the executor."""
self.dsm = SynologyDSM( self.dsm = SynologyDSM(
self._entry.data[CONF_HOST], self._entry.data[CONF_HOST],
self._entry.data[CONF_PORT], self._entry.data[CONF_PORT],
@ -82,7 +85,7 @@ class SynoApi:
timeout=self._entry.options.get(CONF_TIMEOUT), timeout=self._entry.options.get(CONF_TIMEOUT),
device_token=self._entry.data.get(CONF_DEVICE_TOKEN), device_token=self._entry.data.get(CONF_DEVICE_TOKEN),
) )
await self._hass.async_add_executor_job(self.dsm.login) self.dsm.login()
# check if surveillance station is used # check if surveillance station is used
self._with_surveillance_station = bool( self._with_surveillance_station = bool(
@ -94,10 +97,24 @@ class SynoApi:
self._with_surveillance_station, self._with_surveillance_station,
) )
self._async_setup_api_requests() # check if upgrade is available
try:
self.dsm.upgrade.update()
except SynologyDSMAPIErrorException as ex:
self._with_upgrade = False
LOGGER.debug("Disabled fetching upgrade data during setup: %s", ex)
await self._hass.async_add_executor_job(self._fetch_device_configuration) self._fetch_device_configuration()
await self.async_update(first_setup=True)
try:
self._update()
except SYNOLOGY_CONNECTION_EXCEPTIONS as err:
LOGGER.debug(
"Connection error during setup of '%s' with exception: %s",
self._entry.unique_id,
err,
)
raise err
@callback @callback
def subscribe(self, api_key: str, unique_id: str) -> Callable[[], None]: def subscribe(self, api_key: str, unique_id: str) -> Callable[[], None]:
@ -117,8 +134,7 @@ class SynoApi:
return unsubscribe return unsubscribe
@callback def _setup_api_requests(self) -> None:
def _async_setup_api_requests(self) -> None:
"""Determine if we should fetch each API, if one entity needs it.""" """Determine if we should fetch each API, if one entity needs it."""
# Entities not added yet, fetch all # Entities not added yet, fetch all
if not self._fetching_entities: if not self._fetching_entities:
@ -243,30 +259,23 @@ class SynoApi:
# ignore API errors during logout # ignore API errors during logout
pass pass
async def async_update(self, first_setup: bool = False) -> None: async def async_update(self) -> None:
"""Update function for updating API information.""" """Update function for updating API information."""
LOGGER.debug("Start data update for '%s'", self._entry.unique_id)
self._async_setup_api_requests()
try: try:
await self._hass.async_add_executor_job( await self._hass.async_add_executor_job(self._update)
self.dsm.update, self._with_information except SYNOLOGY_CONNECTION_EXCEPTIONS as err:
)
except (
SynologyDSMLoginFailedException,
SynologyDSMRequestException,
SynologyDSMAPIErrorException,
) as err:
LOGGER.debug( LOGGER.debug(
"Connection error during update of '%s' with exception: %s", "Connection error during update of '%s' with exception: %s",
self._entry.unique_id, self._entry.unique_id,
err, err,
) )
if first_setup:
raise err
LOGGER.warning( LOGGER.warning(
"Connection error during update, fallback by reloading the entry" "Connection error during update, fallback by reloading the entry"
) )
await self._hass.config_entries.async_reload(self._entry.entry_id) await self._hass.config_entries.async_reload(self._entry.entry_id)
return
def _update(self) -> None:
"""Update function for updating API information."""
LOGGER.debug("Start data update for '%s'", self._entry.unique_id)
self._setup_api_requests()
self.dsm.update(self._with_information)