Skip to content

Commit

Permalink
Finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed May 7, 2023
1 parent 3a4ff29 commit 263f4dd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
12 changes: 12 additions & 0 deletions homeassistant/components/trafikverket_train/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from homeassistant import config_entries
from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_WEEKDAY, WEEKDAYS
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -64,6 +65,14 @@ class TVTrainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

entry: config_entries.ConfigEntry | None

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> TVTrainOptionsFlowHandler:
"""Get the options flow for this handler."""
return TVTrainOptionsFlowHandler(config_entry)

async def validate_input(
self, api_key: str, train_from: str, train_to: str
) -> None:
Expand Down Expand Up @@ -190,6 +199,9 @@ async def async_step_init(
errors: dict[str, Any] = {}

if user_input:
product_filter: str | None = user_input.get(CONF_FILTER_PRODUCT)
if isinstance(product_filter, str) and product_filter.isspace():
user_input[CONF_FILTER_PRODUCT] = None
return self.async_create_entry(data=user_input)

return self.async_show_form(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/trafikverket_train/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"filter_product": "Filter by product description"
},
"data_description": {
"filter_product": "To filter by product description add the exact phrase here to match"
"filter_product": "To filter by product description add the exact phrase here to match, to remove leave a single space"
}
},
"reauth_confirm": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"weekday": "Days"
},
"data_description": {
"filter_product": "To filter by product description add the exact phrase here to match"
"filter_product": "To filter by product description add the exact phrase here to match, to remove leave a single space"
}
}
}
Expand All @@ -40,7 +40,7 @@
"filter_product": "Filter by product description"
},
"data_description": {
"filter_product": "To filter by product description add the exact phrase here to match"
"filter_product": "To filter by product description add the exact phrase here to match, to remove leave a single space"
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/components/trafikverket_train/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async def test_form(hass: HomeAssistant) -> None:
"time": "10:00",
"weekday": ["mon", "fri"],
}
assert result2["options"] == {"filter_product": None}
assert len(mock_setup_entry.mock_calls) == 1
assert result2["result"].unique_id == "{}-{}-{}-{}".format(
"stockholmc", "uppsalac", "10:00", "['mon', 'fri']"
Expand Down Expand Up @@ -323,3 +324,55 @@ async def test_reauth_flow_error(
"time": "10:00",
"weekday": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
}


async def test_options_flow(hass: HomeAssistant) -> None:
"""Test a reauthentication flow."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_API_KEY: "1234567890",
CONF_NAME: "Stockholm C to Uppsala C at 10:00",
CONF_FROM: "Stockholm C",
CONF_TO: "Uppsala C",
CONF_TIME: "10:00",
CONF_WEEKDAY: WEEKDAYS,
},
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
)
entry.add_to_hass(hass)

with patch(
"homeassistant.components.trafikverket_train.async_setup_entry",
return_value=True,
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

result = await hass.config_entries.options.async_init(entry.entry_id)

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "init"

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"filter_product": "SJ Regionaltåg"},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {"filter_product": "SJ Regionaltåg"}

result2 = await hass.config_entries.options.async_init(entry.entry_id)

assert result2["type"] == FlowResultType.FORM
assert result2["step_id"] == "init"

result2 = await hass.config_entries.options.async_configure(
result2["flow_id"],
user_input={"filter_product": " "},
)
await hass.async_block_till_done()

assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["data"] == {"filter_product": None}

0 comments on commit 263f4dd

Please sign in to comment.