Skip to content

Commit

Permalink
Improve qnap_qsw firmware coordinator failures (home-assistant#74288)
Browse files Browse the repository at this point in the history
qnap_qsw: update: improve firmware coordinator failures

Address late comments from @MartinHjelmare (MartinHjelmare).

Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari authored Jul 1, 2022
1 parent c78c159 commit a58301a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/qnap_qsw/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/qnap_qsw/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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

Expand Down Expand Up @@ -63,8 +63,6 @@ async def _async_update_data(self) -> 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()
25 changes: 25 additions & 0 deletions tests/components/qnap_qsw/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,6 +13,29 @@
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."""

Expand Down

0 comments on commit a58301a

Please sign in to comment.