hass-core/tests/components/nextbus/test_config_flow.py
Ian 3e8d3083ac
Refactor NextBus integration to use new API (#121133)
* Refactor NextBus integration to use new API

This removes the `messages`, `directions`, and `attribution` attributes
from the sensor. Those may be added back in the future with additional
refactoring.

Some existing sensors may be broken today because of deprecated Agency
names. This patch will not migrate them as the migration path is
ambiguous. Setting up again should work though.

* Move result indexing outside of try/except
2024-07-24 18:18:21 +02:00

83 lines
2.4 KiB
Python

"""Test the NextBus config flow."""
from collections.abc import Generator
from unittest.mock import MagicMock, patch
import pytest
from homeassistant import config_entries, setup
from homeassistant.components.nextbus.const import CONF_AGENCY, CONF_ROUTE, DOMAIN
from homeassistant.const import CONF_STOP
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@pytest.fixture
def mock_setup_entry() -> Generator[MagicMock]:
"""Create a mock for the nextbus component setup."""
with patch(
"homeassistant.components.nextbus.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_nextbus() -> Generator[MagicMock]:
"""Create a mock py_nextbus module."""
with patch("homeassistant.components.nextbus.config_flow.NextBusClient") as client:
yield client
async def test_user_config(
hass: HomeAssistant, mock_setup_entry: MagicMock, mock_nextbus_lists: MagicMock
) -> None:
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "agency"
# Select agency
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_AGENCY: "sfmta-cis",
},
)
await hass.async_block_till_done()
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "route"
# Select route
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_ROUTE: "F",
},
)
await hass.async_block_till_done()
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "stop"
# Select stop
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_STOP: "5184",
},
)
await hass.async_block_till_done()
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result.get("data") == {
"agency": "sfmta-cis",
"route": "F",
"stop": "5184",
}
assert len(mock_setup_entry.mock_calls) == 1