2021-02-22 18:53:57 +00:00
|
|
|
"""Test the Litter-Robot config flow."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-08-29 04:40:28 +00:00
|
|
|
from pylitterbot import Account
|
2021-02-22 18:53:57 +00:00
|
|
|
from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
|
2025-01-28 10:21:46 +00:00
|
|
|
import pytest
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries
|
2024-08-28 13:47:35 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD
|
2022-08-29 04:40:28 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2021-02-22 18:53:57 +00:00
|
|
|
|
|
|
|
from .common import CONF_USERNAME, CONFIG, DOMAIN
|
|
|
|
|
2021-03-06 16:28:33 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2021-03-06 16:28:33 +00:00
|
|
|
|
2025-01-28 10:21:46 +00:00
|
|
|
async def test_full_flow(hass: HomeAssistant, mock_account) -> None:
|
|
|
|
"""Test full flow."""
|
2021-02-22 18:53:57 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-02-22 18:53:57 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.config_flow.Account.connect",
|
|
|
|
return_value=mock_account,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2025-01-28 10:21:46 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-02-22 18:53:57 +00:00
|
|
|
result["flow_id"], CONFIG[DOMAIN]
|
|
|
|
)
|
|
|
|
|
2025-01-28 10:21:46 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME]
|
|
|
|
assert result["data"] == CONFIG[DOMAIN]
|
2021-02-22 18:53:57 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_already_configured(hass: HomeAssistant) -> None:
|
2025-01-28 10:21:46 +00:00
|
|
|
"""Test already configured case."""
|
2021-03-06 16:28:33 +00:00
|
|
|
MockConfigEntry(
|
2025-01-23 07:17:59 +00:00
|
|
|
domain=DOMAIN,
|
|
|
|
data=CONFIG[DOMAIN],
|
2021-03-06 16:28:33 +00:00
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2025-01-23 07:17:59 +00:00
|
|
|
data=CONFIG[DOMAIN],
|
2021-03-06 16:28:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-03-06 16:28:33 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2025-01-28 10:21:46 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("side_effect", "connect_errors"),
|
|
|
|
[
|
|
|
|
(Exception, {"base": "unknown"}),
|
|
|
|
(LitterRobotLoginException, {"base": "invalid_auth"}),
|
|
|
|
(LitterRobotException, {"base": "cannot_connect"}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_create_entry(
|
|
|
|
hass: HomeAssistant, mock_account, side_effect, connect_errors
|
|
|
|
) -> None:
|
|
|
|
"""Test creating an entry."""
|
2021-02-22 18:53:57 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2022-08-29 04:40:28 +00:00
|
|
|
"homeassistant.components.litterrobot.config_flow.Account.connect",
|
2025-01-28 10:21:46 +00:00
|
|
|
side_effect=side_effect,
|
2021-02-22 18:53:57 +00:00
|
|
|
):
|
2025-01-28 10:21:46 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-02-22 18:53:57 +00:00
|
|
|
result["flow_id"], CONFIG[DOMAIN]
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:42 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2025-01-28 10:21:46 +00:00
|
|
|
assert result["errors"] == connect_errors
|
2022-08-29 04:40:28 +00:00
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.config_flow.Account.connect",
|
|
|
|
return_value=mock_account,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.async_setup_entry",
|
|
|
|
return_value=True,
|
2025-01-28 10:21:46 +00:00
|
|
|
),
|
2024-03-25 23:02:16 +00:00
|
|
|
):
|
2022-08-29 04:40:28 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2025-01-28 10:21:46 +00:00
|
|
|
result["flow_id"], CONFIG[DOMAIN]
|
2022-08-29 04:40:28 +00:00
|
|
|
)
|
2025-01-28 10:21:46 +00:00
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME]
|
|
|
|
assert result["data"] == CONFIG[DOMAIN]
|
2022-08-29 04:40:28 +00:00
|
|
|
|
|
|
|
|
2025-01-28 10:21:46 +00:00
|
|
|
async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None:
|
|
|
|
"""Test reauth flow (with fail and recover)."""
|
2022-08-29 04:40:28 +00:00
|
|
|
entry = MockConfigEntry(
|
2025-01-23 07:17:59 +00:00
|
|
|
domain=DOMAIN,
|
|
|
|
data=CONFIG[DOMAIN],
|
2022-08-29 04:40:28 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2024-08-28 13:47:35 +00:00
|
|
|
result = await entry.start_reauth_flow(hass)
|
2022-08-29 04:40:28 +00:00
|
|
|
|
2024-04-02 21:21:42 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-08-29 04:40:28 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.litterrobot.config_flow.Account.connect",
|
|
|
|
side_effect=LitterRobotLoginException,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2025-01-23 07:17:59 +00:00
|
|
|
user_input={CONF_PASSWORD: CONFIG[DOMAIN][CONF_PASSWORD]},
|
2022-08-29 04:40:28 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:42 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-08-29 04:40:28 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.config_flow.Account.connect",
|
|
|
|
return_value=mock_account,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.litterrobot.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2022-08-29 04:40:28 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2025-01-23 07:17:59 +00:00
|
|
|
user_input={CONF_PASSWORD: CONFIG[DOMAIN][CONF_PASSWORD]},
|
2022-08-29 04:40:28 +00:00
|
|
|
)
|
2024-04-02 21:21:42 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-08-29 04:40:28 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|