"""Tests for init module.""" import http import time from unittest.mock import AsyncMock import pytest from homeassistant.components.husqvarna_automower.const import DOMAIN, OAUTH2_TOKEN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from . import setup_integration from tests.common import MockConfigEntry from tests.test_util.aiohttp import AiohttpClientMocker async def test_load_unload_entry( hass: HomeAssistant, mock_automower_client: AsyncMock, mock_config_entry: MockConfigEntry, ) -> None: """Test load and unload entry.""" await setup_integration(hass, mock_config_entry) entry = hass.config_entries.async_entries(DOMAIN)[0] assert entry.state == ConfigEntryState.LOADED await hass.config_entries.async_remove(entry.entry_id) await hass.async_block_till_done() assert entry.state == ConfigEntryState.NOT_LOADED @pytest.mark.parametrize( ("expires_at", "status", "expected_state"), [ ( time.time() - 3600, http.HTTPStatus.UNAUTHORIZED, ConfigEntryState.SETUP_RETRY, # Will trigger reauth in the future ), ( time.time() - 3600, http.HTTPStatus.INTERNAL_SERVER_ERROR, ConfigEntryState.SETUP_RETRY, ), ], ids=["unauthorized", "internal_server_error"], ) async def test_expired_token_refresh_failure( hass: HomeAssistant, mock_config_entry: MockConfigEntry, aioclient_mock: AiohttpClientMocker, status: http.HTTPStatus, expected_state: ConfigEntryState, ) -> None: """Test failure while refreshing token with a transient error.""" aioclient_mock.clear_requests() aioclient_mock.post( OAUTH2_TOKEN, status=status, ) await setup_integration(hass, mock_config_entry) assert mock_config_entry.state is expected_state