2021-11-22 14:06:42 +00:00
|
|
|
"""Test the ViCare config flow."""
|
2023-03-31 12:33:58 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
import pytest
|
2023-06-08 21:43:56 +00:00
|
|
|
from PyViCare.PyViCareUtils import PyViCareInvalidCredentialsError
|
2023-03-31 12:33:58 +00:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
from homeassistant.components import dhcp
|
2022-05-24 18:28:09 +00:00
|
|
|
from homeassistant.components.vicare.const import DOMAIN
|
2023-03-31 12:33:58 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
|
2021-11-22 14:06:42 +00:00
|
|
|
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
2023-02-07 14:01:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-03-31 12:33:58 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
from . import MOCK_MAC, MODULE
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
VALID_CONFIG = {
|
|
|
|
CONF_USERNAME: "foo@bar.com",
|
|
|
|
CONF_PASSWORD: "1234",
|
|
|
|
CONF_CLIENT_ID: "5678",
|
|
|
|
}
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
DHCP_INFO = dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
|
|
|
hostname="mock_hostname",
|
|
|
|
macaddress=MOCK_MAC,
|
|
|
|
)
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
async def test_user_create_entry(
|
|
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, snapshot: SnapshotAssertion
|
|
|
|
) -> None:
|
|
|
|
"""Test that the user step works."""
|
|
|
|
# start user flow
|
2021-11-22 14:06:42 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2023-03-31 12:33:58 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
2021-11-22 14:06:42 +00:00
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
# test PyViCareInvalidCredentialsError
|
2021-11-22 14:06:42 +00:00
|
|
|
with patch(
|
2023-03-31 12:33:58 +00:00
|
|
|
f"{MODULE}.config_flow.vicare_login",
|
2021-11-22 14:06:42 +00:00
|
|
|
side_effect=PyViCareInvalidCredentialsError,
|
|
|
|
):
|
2023-03-31 12:33:58 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-11-22 14:06:42 +00:00
|
|
|
result["flow_id"],
|
2023-03-31 12:33:58 +00:00
|
|
|
VALID_CONFIG,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
# test success
|
|
|
|
with patch(
|
|
|
|
f"{MODULE}.config_flow.vicare_login",
|
|
|
|
return_value=None,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
VALID_CONFIG,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "ViCare"
|
|
|
|
assert result["data"] == snapshot
|
|
|
|
mock_setup_entry.assert_called_once()
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
async def test_form_dhcp(
|
|
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, snapshot: SnapshotAssertion
|
|
|
|
) -> None:
|
2021-11-22 14:06:42 +00:00
|
|
|
"""Test we can setup from dhcp."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2023-03-31 12:33:58 +00:00
|
|
|
context={"source": SOURCE_DHCP},
|
|
|
|
data=DHCP_INFO,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2021-11-22 14:06:42 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
2023-03-31 12:33:58 +00:00
|
|
|
f"{MODULE}.config_flow.vicare_login",
|
2021-11-22 14:06:42 +00:00
|
|
|
return_value=None,
|
2023-03-31 12:33:58 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-11-22 14:06:42 +00:00
|
|
|
result["flow_id"],
|
2023-03-31 12:33:58 +00:00
|
|
|
VALID_CONFIG,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "ViCare"
|
|
|
|
assert result["data"] == snapshot
|
|
|
|
mock_setup_entry.assert_called_once()
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_dhcp_single_instance_allowed(hass: HomeAssistant) -> None:
|
2021-11-22 14:06:42 +00:00
|
|
|
"""Test that configuring more than one instance is rejected."""
|
|
|
|
mock_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
2023-03-31 12:33:58 +00:00
|
|
|
data=VALID_CONFIG,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2023-03-31 12:33:58 +00:00
|
|
|
context={"source": SOURCE_DHCP},
|
|
|
|
data=DHCP_INFO,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.ABORT
|
2021-11-22 14:06:42 +00:00
|
|
|
assert result["reason"] == "single_instance_allowed"
|
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_user_input_single_instance_allowed(hass: HomeAssistant) -> None:
|
2021-11-22 14:06:42 +00:00
|
|
|
"""Test that configuring more than one instance is rejected."""
|
|
|
|
mock_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
unique_id="ViCare",
|
2023-03-31 12:33:58 +00:00
|
|
|
data=VALID_CONFIG,
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2023-03-31 12:33:58 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2021-11-22 14:06:42 +00:00
|
|
|
)
|
2023-03-31 12:33:58 +00:00
|
|
|
assert result["type"] == FlowResultType.ABORT
|
2021-11-22 14:06:42 +00:00
|
|
|
assert result["reason"] == "single_instance_allowed"
|