Improve qnap_qsw firmware coordinator failures (#74288)

qnap_qsw: update: improve firmware coordinator failures

Address late comments from @MartinHjelmare (MartinHjelmare).

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas 2022-07-01 09:48:59 +02:00 committed by GitHub
parent c78c159d72
commit a58301a97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 4 deletions

View File

@ -1,16 +1,21 @@
"""The QNAP QSW integration."""
from __future__ import annotations
import logging
from aioqsw.localapi import ConnectionOptions, QnapQswApi
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from .const import DOMAIN, QSW_COORD_DATA, QSW_COORD_FW
from .coordinator import QswDataCoordinator, QswFirmwareCoordinator
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
@ -33,7 +38,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coord_data.async_config_entry_first_refresh()
coord_fw = QswFirmwareCoordinator(hass, qsw)
await coord_fw.async_config_entry_first_refresh()
try:
await coord_fw.async_config_entry_first_refresh()
except ConfigEntryNotReady as error:
_LOGGER.warning(error)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
QSW_COORD_DATA: coord_data,

View File

@ -5,7 +5,7 @@ from datetime import timedelta
import logging
from typing import Any
from aioqsw.exceptions import APIError, QswError
from aioqsw.exceptions import QswError
from aioqsw.localapi import QnapQswApi
import async_timeout
@ -63,8 +63,6 @@ class QswFirmwareCoordinator(DataUpdateCoordinator[dict[str, Any]]):
async with async_timeout.timeout(QSW_TIMEOUT_SEC):
try:
await self.qsw.check_firmware()
except APIError as error:
_LOGGER.warning(error)
except QswError as error:
raise UpdateFailed(error) from error
return self.qsw.data()

View File

@ -2,6 +2,8 @@
from unittest.mock import patch
from aioqsw.exceptions import APIError
from homeassistant.components.qnap_qsw.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
@ -11,6 +13,29 @@ from .util import CONFIG
from tests.common import MockConfigEntry
async def test_firmware_check_error(hass: HomeAssistant) -> None:
"""Test firmware update check error."""
config_entry = MockConfigEntry(
domain=DOMAIN, unique_id="qsw_unique_id", data=CONFIG
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.qnap_qsw.QnapQswApi.check_firmware",
side_effect=APIError,
), patch(
"homeassistant.components.qnap_qsw.QnapQswApi.validate",
return_value=None,
), patch(
"homeassistant.components.qnap_qsw.QnapQswApi.update",
return_value=None,
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test unload."""