2023-12-09 08:45:40 +00:00
|
|
|
"""Tests for the Sun WEG server config flow."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from sunweg.api import APIHelper
|
|
|
|
|
|
|
|
from homeassistant import config_entries, data_entry_flow
|
|
|
|
from homeassistant.components.sunweg.const import CONF_PLANT_ID, DOMAIN
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
from .common import SUNWEG_USER_INPUT
|
2023-12-09 08:45:40 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
async def test_show_authenticate_form(hass: HomeAssistant) -> None:
|
|
|
|
"""Test that the setup form is served."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_incorrect_login(hass: HomeAssistant) -> None:
|
|
|
|
"""Test that it shows the appropriate error when an incorrect username/password/server is entered."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch.object(APIHelper, "authenticate", return_value=False):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2023-12-14 15:51:57 +00:00
|
|
|
result["flow_id"], SUNWEG_USER_INPUT
|
2023-12-09 08:45:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_no_plants_on_account(hass: HomeAssistant) -> None:
|
2023-12-14 15:51:57 +00:00
|
|
|
"""Test registering an integration with no plants available."""
|
2023-12-09 08:45:40 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
with patch.object(APIHelper, "authenticate", return_value=True), patch.object(
|
|
|
|
APIHelper, "listPlants", return_value=[]
|
|
|
|
):
|
2023-12-09 08:45:40 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2023-12-14 15:51:57 +00:00
|
|
|
result["flow_id"], SUNWEG_USER_INPUT
|
2023-12-09 08:45:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "no_plants"
|
|
|
|
|
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
async def test_multiple_plant_ids(hass: HomeAssistant, plant_fixture) -> None:
|
|
|
|
"""Test registering an integration and finishing flow with an selected plant_id."""
|
2023-12-09 08:45:40 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2023-12-14 15:51:57 +00:00
|
|
|
user_input = SUNWEG_USER_INPUT.copy()
|
|
|
|
plant_list = [plant_fixture, plant_fixture]
|
2023-12-09 08:45:40 +00:00
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
with patch.object(APIHelper, "authenticate", return_value=True), patch.object(
|
|
|
|
APIHelper, "listPlants", return_value=plant_list
|
|
|
|
):
|
2023-12-09 08:45:40 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "plant"
|
|
|
|
|
|
|
|
user_input = {CONF_PLANT_ID: 123456}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2023-12-14 15:51:57 +00:00
|
|
|
assert result["data"][CONF_USERNAME] == SUNWEG_USER_INPUT[CONF_USERNAME]
|
|
|
|
assert result["data"][CONF_PASSWORD] == SUNWEG_USER_INPUT[CONF_PASSWORD]
|
2023-12-09 08:45:40 +00:00
|
|
|
assert result["data"][CONF_PLANT_ID] == 123456
|
|
|
|
|
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
async def test_one_plant_on_account(hass: HomeAssistant, plant_fixture) -> None:
|
|
|
|
"""Test registering an integration and finishing flow with current plant_id."""
|
2023-12-09 08:45:40 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2023-12-14 15:51:57 +00:00
|
|
|
user_input = SUNWEG_USER_INPUT.copy()
|
2023-12-09 08:45:40 +00:00
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
with patch.object(APIHelper, "authenticate", return_value=True), patch.object(
|
2023-12-09 08:45:40 +00:00
|
|
|
APIHelper,
|
|
|
|
"listPlants",
|
2023-12-14 15:51:57 +00:00
|
|
|
return_value=[plant_fixture],
|
2023-12-09 08:45:40 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2023-12-14 15:51:57 +00:00
|
|
|
assert result["data"][CONF_USERNAME] == SUNWEG_USER_INPUT[CONF_USERNAME]
|
|
|
|
assert result["data"][CONF_PASSWORD] == SUNWEG_USER_INPUT[CONF_PASSWORD]
|
2023-12-09 08:45:40 +00:00
|
|
|
assert result["data"][CONF_PLANT_ID] == 123456
|
|
|
|
|
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
async def test_existing_plant_configured(hass: HomeAssistant, plant_fixture) -> None:
|
2023-12-09 08:45:40 +00:00
|
|
|
"""Test entering an existing plant_id."""
|
|
|
|
entry = MockConfigEntry(domain=DOMAIN, unique_id=123456)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2023-12-14 15:51:57 +00:00
|
|
|
user_input = SUNWEG_USER_INPUT.copy()
|
2023-12-09 08:45:40 +00:00
|
|
|
|
2023-12-14 15:51:57 +00:00
|
|
|
with patch.object(APIHelper, "authenticate", return_value=True), patch.object(
|
2023-12-09 08:45:40 +00:00
|
|
|
APIHelper,
|
|
|
|
"listPlants",
|
2023-12-14 15:51:57 +00:00
|
|
|
return_value=[plant_fixture],
|
2023-12-09 08:45:40 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|