Catch auth exception in husqvarna automower (#115365)
* Catch AuthException in Husqvarna Automower * don't use getattr * raise ConfigEntryAuthFailed
This commit is contained in:
parent
82e12052e4
commit
3fa2db84f0
2 changed files with 25 additions and 8 deletions
|
@ -4,12 +4,17 @@ import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aioautomower.exceptions import ApiException, HusqvarnaWSServerHandshakeError
|
from aioautomower.exceptions import (
|
||||||
|
ApiException,
|
||||||
|
AuthException,
|
||||||
|
HusqvarnaWSServerHandshakeError,
|
||||||
|
)
|
||||||
from aioautomower.model import MowerAttributes
|
from aioautomower.model import MowerAttributes
|
||||||
from aioautomower.session import AutomowerSession
|
from aioautomower.session import AutomowerSession
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
@ -46,6 +51,8 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[dict[str, MowerAttrib
|
||||||
return await self.api.get_status()
|
return await self.api.get_status()
|
||||||
except ApiException as err:
|
except ApiException as err:
|
||||||
raise UpdateFailed(err) from err
|
raise UpdateFailed(err) from err
|
||||||
|
except AuthException as err:
|
||||||
|
raise ConfigEntryAuthFailed(err) from err
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def callback(self, ws_data: dict[str, MowerAttributes]) -> None:
|
def callback(self, ws_data: dict[str, MowerAttributes]) -> None:
|
||||||
|
|
|
@ -5,7 +5,11 @@ import http
|
||||||
import time
|
import time
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from aioautomower.exceptions import ApiException, HusqvarnaWSServerHandshakeError
|
from aioautomower.exceptions import (
|
||||||
|
ApiException,
|
||||||
|
AuthException,
|
||||||
|
HusqvarnaWSServerHandshakeError,
|
||||||
|
)
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
@ -75,19 +79,25 @@ async def test_expired_token_refresh_failure(
|
||||||
assert mock_config_entry.state is expected_state
|
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(
|
async def test_update_failed(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_automower_client: AsyncMock,
|
mock_automower_client: AsyncMock,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
exception: Exception,
|
||||||
|
entry_state: ConfigEntryState,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test load and unload entry."""
|
"""Test update failed."""
|
||||||
getattr(mock_automower_client, "get_status").side_effect = ApiException(
|
mock_automower_client.get_status.side_effect = exception("Test error")
|
||||||
"Test error"
|
|
||||||
)
|
|
||||||
await setup_integration(hass, mock_config_entry)
|
await setup_integration(hass, mock_config_entry)
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
|
assert entry.state is entry_state
|
||||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
|
||||||
|
|
||||||
|
|
||||||
async def test_websocket_not_available(
|
async def test_websocket_not_available(
|
||||||
|
|
Loading…
Add table
Reference in a new issue