Catch auth exception in husqvarna automower (#115365)

* Catch AuthException in Husqvarna Automower

* don't use getattr

* raise ConfigEntryAuthFailed
pull/117139/head^2
Thomas55555 2024-05-09 16:56:26 +02:00 committed by GitHub
parent 82e12052e4
commit 3fa2db84f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 8 deletions

View File

@ -4,12 +4,17 @@ import asyncio
from datetime import timedelta
import logging
from aioautomower.exceptions import ApiException, HusqvarnaWSServerHandshakeError
from aioautomower.exceptions import (
ApiException,
AuthException,
HusqvarnaWSServerHandshakeError,
)
from aioautomower.model import MowerAttributes
from aioautomower.session import AutomowerSession
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
@ -46,6 +51,8 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[dict[str, MowerAttrib
return await self.api.get_status()
except ApiException as err:
raise UpdateFailed(err) from err
except AuthException as err:
raise ConfigEntryAuthFailed(err) from err
@callback
def callback(self, ws_data: dict[str, MowerAttributes]) -> None:

View File

@ -5,7 +5,11 @@ import http
import time
from unittest.mock import AsyncMock
from aioautomower.exceptions import ApiException, HusqvarnaWSServerHandshakeError
from aioautomower.exceptions import (
ApiException,
AuthException,
HusqvarnaWSServerHandshakeError,
)
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
@ -75,19 +79,25 @@ async def test_expired_token_refresh_failure(
assert mock_config_entry.state is expected_state
@pytest.mark.parametrize(
("exception", "entry_state"),
[
(ApiException, ConfigEntryState.SETUP_RETRY),
(AuthException, ConfigEntryState.SETUP_ERROR),
],
)
async def test_update_failed(
hass: HomeAssistant,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
exception: Exception,
entry_state: ConfigEntryState,
) -> None:
"""Test load and unload entry."""
getattr(mock_automower_client, "get_status").side_effect = ApiException(
"Test error"
)
"""Test update failed."""
mock_automower_client.get_status.side_effect = exception("Test error")
await setup_integration(hass, mock_config_entry)
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.state is ConfigEntryState.SETUP_RETRY
assert entry.state is entry_state
async def test_websocket_not_available(