2022-12-22 21:00:17 +00:00
|
|
|
"""Tests for rainbird initialization."""
|
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
from __future__ import annotations
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-08-14 11:32:08 +00:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.rainbird import DOMAIN
|
2023-05-05 14:22:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2022-12-22 21:00:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-08-14 11:32:08 +00:00
|
|
|
from .conftest import (
|
|
|
|
CONFIG_ENTRY_DATA,
|
|
|
|
MODEL_AND_VERSION_RESPONSE,
|
|
|
|
ComponentSetup,
|
|
|
|
mock_response,
|
|
|
|
mock_response_error,
|
|
|
|
)
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-05-05 14:22:07 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMockResponse
|
2022-12-22 21:00:17 +00:00
|
|
|
|
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("yaml_config", "config_entry_data", "initial_response"),
|
2023-01-07 17:34:01 +00:00
|
|
|
[
|
|
|
|
({}, CONFIG_ENTRY_DATA, None),
|
|
|
|
],
|
2023-05-05 14:53:40 +00:00
|
|
|
ids=["config_entry"],
|
2023-01-07 17:34:01 +00:00
|
|
|
)
|
|
|
|
async def test_init_success(
|
2022-12-22 21:00:17 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
setup_integration: ComponentSetup,
|
2023-01-07 17:34:01 +00:00
|
|
|
responses: list[AiohttpClientMockResponse],
|
|
|
|
initial_response: AiohttpClientMockResponse | None,
|
2022-12-22 21:00:17 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test successful setup and unload."""
|
2023-01-07 17:34:01 +00:00
|
|
|
if initial_response:
|
|
|
|
responses.insert(0, initial_response)
|
2022-12-22 21:00:17 +00:00
|
|
|
|
|
|
|
assert await setup_integration()
|
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
entries = hass.config_entries.async_entries(DOMAIN)
|
|
|
|
assert len(entries) == 1
|
|
|
|
assert entries[0].state == ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
await hass.config_entries.async_unload(entries[0].entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert entries[0].state is ConfigEntryState.NOT_LOADED
|
|
|
|
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("yaml_config", "config_entry_data", "responses", "config_entry_states"),
|
2023-01-07 17:34:01 +00:00
|
|
|
[
|
2023-08-14 11:32:08 +00:00
|
|
|
(
|
|
|
|
{},
|
|
|
|
CONFIG_ENTRY_DATA,
|
|
|
|
[mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE)],
|
|
|
|
[ConfigEntryState.SETUP_RETRY],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
CONFIG_ENTRY_DATA,
|
|
|
|
[mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR)],
|
|
|
|
[ConfigEntryState.SETUP_RETRY],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
CONFIG_ENTRY_DATA,
|
|
|
|
[
|
|
|
|
mock_response(MODEL_AND_VERSION_RESPONSE),
|
|
|
|
mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE),
|
|
|
|
],
|
|
|
|
[ConfigEntryState.SETUP_RETRY],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
CONFIG_ENTRY_DATA,
|
|
|
|
[
|
|
|
|
mock_response(MODEL_AND_VERSION_RESPONSE),
|
|
|
|
mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR),
|
|
|
|
],
|
|
|
|
[ConfigEntryState.SETUP_RETRY],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"unavailable",
|
|
|
|
"server-error",
|
|
|
|
"coordinator-unavailable",
|
|
|
|
"coordinator-server-error",
|
2023-01-07 17:34:01 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_communication_failure(
|
2022-12-22 21:00:17 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
setup_integration: ComponentSetup,
|
2023-01-07 17:34:01 +00:00
|
|
|
config_entry_states: list[ConfigEntryState],
|
2022-12-22 21:00:17 +00:00
|
|
|
) -> None:
|
2023-08-14 11:32:08 +00:00
|
|
|
"""Test unable to talk to device on startup, which fails setup."""
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
assert await setup_integration()
|
|
|
|
|
|
|
|
assert [
|
|
|
|
entry.state for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
] == config_entry_states
|