2024-01-25 11:54:47 +00:00
|
|
|
"""Test the Teslemetry config flow."""
|
|
|
|
|
2024-01-28 20:04:44 +00:00
|
|
|
from unittest.mock import patch
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
from aiohttp import ClientConnectionError
|
|
|
|
import pytest
|
2024-03-06 08:09:15 +00:00
|
|
|
from tesla_fleet_api.exceptions import (
|
|
|
|
InvalidToken,
|
|
|
|
SubscriptionRequired,
|
|
|
|
TeslaFleetError,
|
|
|
|
)
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.components.teslemetry.const import DOMAIN
|
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
|
|
|
|
from .const import CONFIG
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-01-28 20:04:44 +00:00
|
|
|
def mock_test():
|
2024-01-25 11:54:47 +00:00
|
|
|
"""Mock Teslemetry api class."""
|
|
|
|
with patch(
|
2024-01-28 20:04:44 +00:00
|
|
|
"homeassistant.components.teslemetry.Teslemetry.test", return_value=True
|
|
|
|
) as mock_test:
|
|
|
|
yield mock_test
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_form(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test we get the form."""
|
|
|
|
|
|
|
|
result1 = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result1["type"] is FlowResultType.FORM
|
2024-01-25 11:54:47 +00:00
|
|
|
assert not result1["errors"]
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.teslemetry.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result1["flow_id"],
|
|
|
|
CONFIG,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2024-01-25 11:54:47 +00:00
|
|
|
assert result2["data"] == CONFIG
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("side_effect", "error"),
|
|
|
|
[
|
|
|
|
(InvalidToken, {CONF_ACCESS_TOKEN: "invalid_access_token"}),
|
2024-03-06 08:09:15 +00:00
|
|
|
(SubscriptionRequired, {"base": "subscription_required"}),
|
2024-01-25 11:54:47 +00:00
|
|
|
(ClientConnectionError, {"base": "cannot_connect"}),
|
|
|
|
(TeslaFleetError, {"base": "unknown"}),
|
|
|
|
],
|
|
|
|
)
|
2024-01-28 20:04:44 +00:00
|
|
|
async def test_form_errors(hass: HomeAssistant, side_effect, error, mock_test) -> None:
|
2024-01-25 11:54:47 +00:00
|
|
|
"""Test errors are handled."""
|
|
|
|
|
|
|
|
result1 = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-01-28 20:04:44 +00:00
|
|
|
mock_test.side_effect = side_effect
|
2024-01-25 11:54:47 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result1["flow_id"],
|
|
|
|
CONFIG,
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2024-01-25 11:54:47 +00:00
|
|
|
assert result2["errors"] == error
|
|
|
|
|
|
|
|
# Complete the flow
|
2024-01-28 20:04:44 +00:00
|
|
|
mock_test.side_effect = None
|
2024-01-25 11:54:47 +00:00
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"],
|
|
|
|
CONFIG,
|
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result3["type"] is FlowResultType.CREATE_ENTRY
|