2020-07-01 05:44:10 +00:00
|
|
|
"""Test the PoolSense config flow."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
from unittest.mock import AsyncMock
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-07-01 05:44:10 +00:00
|
|
|
from homeassistant.components.poolsense.const import DOMAIN
|
|
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-02 21:21:50 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
|
|
|
|
async def test_full_form(
|
|
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_poolsense_client: AsyncMock
|
|
|
|
) -> None:
|
|
|
|
"""Test full flow."""
|
2020-07-01 05:44:10 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-04-11 08:00:17 +00:00
|
|
|
assert result["step_id"] == "user"
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "test@test.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_EMAIL: "test@test.com",
|
|
|
|
CONF_PASSWORD: "test",
|
|
|
|
}
|
|
|
|
assert result["result"].unique_id == "test@test.com"
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
async def test_invalid_credentials(
|
|
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_poolsense_client: AsyncMock
|
|
|
|
) -> None:
|
2020-07-01 05:44:10 +00:00
|
|
|
"""Test we handle invalid credentials."""
|
2024-05-16 15:51:57 +00:00
|
|
|
mock_poolsense_client.test_poolsense_credentials.return_value = False
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"},
|
|
|
|
)
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-07-03 22:38:05 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
mock_poolsense_client.test_poolsense_credentials.return_value = True
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"},
|
|
|
|
)
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2020-07-01 05:44:10 +00:00
|
|
|
|
2024-05-16 15:51:57 +00:00
|
|
|
|
|
|
|
async def test_duplicate_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_poolsense_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Test we can't add the same entry twice."""
|
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|