core/tests/components/pooldose/test_init.py

120 lines
3.5 KiB
Python

"""Test the PoolDose integration initialization."""
from unittest.mock import AsyncMock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.pooldose.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .conftest import RequestStatus
from tests.common import MockConfigEntry
async def test_devices(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test all entities."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
device = device_registry.async_get_device({(DOMAIN, "TEST123456789")})
assert device is not None
assert device == snapshot
async def test_setup_entry_success(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test successful setup of config entry."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_setup_entry_coordinator_refresh_fails(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pooldose_client: AsyncMock,
) -> None:
"""Test setup failure when coordinator first refresh fails."""
mock_config_entry.add_to_hass(hass)
mock_pooldose_client.instant_values_structured.side_effect = Exception(
"API communication failed"
)
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
@pytest.mark.parametrize(
"status",
[
RequestStatus.HOST_UNREACHABLE,
RequestStatus.PARAMS_FETCH_FAILED,
RequestStatus.API_VERSION_UNSUPPORTED,
RequestStatus.NO_DATA,
RequestStatus.UNKNOWN_ERROR,
],
)
async def test_setup_entry_various_client_failures(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pooldose_client: AsyncMock,
status: RequestStatus,
) -> None:
"""Test setup fails with various client error statuses."""
mock_pooldose_client.connect.return_value = RequestStatus.HOST_UNREACHABLE
mock_pooldose_client.is_connected = False
mock_config_entry.add_to_hass(hass)
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
@pytest.mark.parametrize(
"exception",
[
TimeoutError("Connection timeout"),
OSError("Network error"),
],
)
async def test_setup_entry_timeout_error(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pooldose_client: AsyncMock,
exception: Exception,
) -> None:
"""Test setup failure when client connection times out."""
mock_pooldose_client.connect.side_effect = exception
mock_pooldose_client.is_connected = False
mock_config_entry.add_to_hass(hass)
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