2020-08-10 13:51:04 +00:00
|
|
|
"""Tests for eafm config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-08-10 13:51:04 +00:00
|
|
|
import pytest
|
2024-01-30 11:24:19 +00:00
|
|
|
from voluptuous.error import Invalid
|
2020-08-10 13:51:04 +00:00
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant import config_entries
|
2020-08-10 13:51:04 +00:00
|
|
|
from homeassistant.components.eafm import const
|
2023-02-12 18:39:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-03 07:21:17 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2020-08-10 13:51:04 +00:00
|
|
|
|
|
|
|
|
2023-02-12 18:39:48 +00:00
|
|
|
async def test_flow_no_discovered_stations(
|
|
|
|
hass: HomeAssistant, mock_get_stations
|
|
|
|
) -> None:
|
2020-08-10 13:51:04 +00:00
|
|
|
"""Test config flow discovers no station."""
|
|
|
|
mock_get_stations.return_value = []
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-08-10 13:51:04 +00:00
|
|
|
)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-08-10 13:51:04 +00:00
|
|
|
assert result["reason"] == "no_stations"
|
|
|
|
|
|
|
|
|
2023-02-12 18:39:48 +00:00
|
|
|
async def test_flow_invalid_station(hass: HomeAssistant, mock_get_stations) -> None:
|
2020-08-10 13:51:04 +00:00
|
|
|
"""Test config flow errors on invalid station."""
|
|
|
|
mock_get_stations.return_value = [
|
|
|
|
{"label": "My station", "stationReference": "L12345"}
|
|
|
|
]
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-08-10 13:51:04 +00:00
|
|
|
)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-08-10 13:51:04 +00:00
|
|
|
|
2024-01-30 11:24:19 +00:00
|
|
|
with pytest.raises(Invalid):
|
2020-08-10 13:51:04 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"station": "My other station"}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-12 18:39:48 +00:00
|
|
|
async def test_flow_works(
|
|
|
|
hass: HomeAssistant, mock_get_stations, mock_get_station
|
|
|
|
) -> None:
|
2020-08-10 13:51:04 +00:00
|
|
|
"""Test config flow discovers no station."""
|
|
|
|
mock_get_stations.return_value = [
|
|
|
|
{"label": "My station", "stationReference": "L12345"}
|
|
|
|
]
|
|
|
|
mock_get_station.return_value = [
|
|
|
|
{"label": "My station", "stationReference": "L12345"}
|
|
|
|
]
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-08-10 13:51:04 +00:00
|
|
|
)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-08-10 13:51:04 +00:00
|
|
|
|
|
|
|
with patch("homeassistant.components.eafm.async_setup_entry", return_value=True):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"station": "My station"}
|
|
|
|
)
|
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2020-08-10 13:51:04 +00:00
|
|
|
assert result["title"] == "My station"
|
|
|
|
assert result["data"] == {
|
|
|
|
"station": "L12345",
|
|
|
|
}
|