mirror of
https://github.com/home-assistant/core.git
synced 2026-09-28 02:18:10 -04:00
Start Imou reauthentication when the cloud rejects the credentials (#180989)
This commit is contained in:
@@ -6,11 +6,12 @@ from datetime import timedelta
|
||||
import logging
|
||||
from typing import override
|
||||
|
||||
from pyimouapi.exceptions import ImouException
|
||||
from pyimouapi.exceptions import ImouException, InvalidAppIdOrSecretException
|
||||
from pyimouapi.ha_device import ImouHaDevice, ImouHaDeviceManager
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
@@ -83,6 +84,11 @@ class ImouDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||
fresh_devices = await self._device_manager.async_get_devices()
|
||||
except TimeoutError as err:
|
||||
raise UpdateFailed(f"Timeout while fetching data: {err}") from err
|
||||
except InvalidAppIdOrSecretException as err:
|
||||
raise ConfigEntryAuthFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_auth",
|
||||
) from err
|
||||
except ImouException as err:
|
||||
raise UpdateFailed(f"Error fetching Imou devices: {err}") from err
|
||||
|
||||
|
||||
@@ -143,6 +143,9 @@
|
||||
"get_stream_failed": {
|
||||
"message": "Could not get the live stream URL from Imou: {error}"
|
||||
},
|
||||
"invalid_auth": {
|
||||
"message": "Imou rejected the App ID and App secret"
|
||||
},
|
||||
"press_button_failed": {
|
||||
"message": "Imou rejected the button press: {error}"
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pyimouapi.const import PARAM_STATE, PARAM_STATUS
|
||||
from pyimouapi.exceptions import ImouException
|
||||
from pyimouapi.exceptions import ImouException, InvalidAppIdOrSecretException
|
||||
from pyimouapi.ha_device import DeviceStatus, ImouHaDevice
|
||||
import pytest
|
||||
|
||||
@@ -42,22 +42,34 @@ async def test_setup_and_unload_entry(
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "expected_state"),
|
||||
[
|
||||
(
|
||||
InvalidAppIdOrSecretException("bad credentials"),
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
),
|
||||
(ImouException("cloud failure"), ConfigEntryState.SETUP_RETRY),
|
||||
(TimeoutError("timeout"), ConfigEntryState.SETUP_RETRY),
|
||||
(RuntimeError("unexpected"), ConfigEntryState.SETUP_RETRY),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_imou_openapi_client", "mock_imou_ha_device_manager")
|
||||
async def test_setup_entry_failed_on_refresh(
|
||||
async def test_setup_entry_exceptions(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_imou_ha_device_manager: AsyncMock,
|
||||
exception: Exception,
|
||||
expected_state: ConfigEntryState,
|
||||
) -> None:
|
||||
"""Device fetch failure during coordinator setup surfaces as setup retry."""
|
||||
mock_imou_ha_device_manager.async_get_devices.side_effect = RuntimeError(
|
||||
"Setup failed"
|
||||
)
|
||||
"""Test the coordinator errors while listing devices during setup."""
|
||||
mock_imou_ha_device_manager.async_get_devices.side_effect = exception
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
assert not await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
assert mock_config_entry.state is expected_state
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
|
||||
Reference in New Issue
Block a user