mirror of
https://github.com/home-assistant/core.git
synced 2026-07-03 12:46:09 +01:00
3570cd0ee3
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
"""Tests for the WattWächter Plus integration setup."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from unittest.mock import AsyncMock
|
|
|
|
from aio_wattwaechter import (
|
|
WattwaechterAuthenticationError,
|
|
WattwaechterConnectionError,
|
|
WattwaechterNoDataError,
|
|
)
|
|
import pytest
|
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_setup_and_unload_entry(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_client: AsyncMock,
|
|
) -> None:
|
|
"""Test successful integration setup and unload."""
|
|
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_connection_error(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_client: AsyncMock,
|
|
) -> None:
|
|
"""Test setup when device is unreachable."""
|
|
mock_client.alive.side_effect = WattwaechterConnectionError("Connection refused")
|
|
|
|
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(
|
|
("attribute", "value"),
|
|
[
|
|
("side_effect", WattwaechterNoDataError("No data")),
|
|
("side_effect", WattwaechterConnectionError("Connection refused")),
|
|
("return_value", None),
|
|
],
|
|
ids=["no_data", "connection_error", "returns_none"],
|
|
)
|
|
async def test_setup_entry_retries_on_meter_data_failure(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_client: AsyncMock,
|
|
attribute: str,
|
|
value: Any,
|
|
) -> None:
|
|
"""Test setup retries when meter_data fails or returns no data."""
|
|
setattr(mock_client.meter_data, attribute, value)
|
|
|
|
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
|
|
|
|
|
|
async def test_setup_entry_auth_error_starts_reauth(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_client: AsyncMock,
|
|
) -> None:
|
|
"""Test an authentication error during setup starts the reauth flow."""
|
|
mock_client.meter_data.side_effect = WattwaechterAuthenticationError(
|
|
"Invalid token"
|
|
)
|
|
|
|
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_ERROR
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
assert len(flows) == 1
|
|
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
|
assert flows[0]["context"]["entry_id"] == mock_config_entry.entry_id
|