Skip to content

Commit

Permalink
Add reconfigure step to trafikverket weather (#127140)
Browse files Browse the repository at this point in the history
* Add reconfigure step to trafikverket weather

* Use helper

* Fix

* Fix review comments
  • Loading branch information
gjohansson-ST authored Oct 2, 2024
1 parent f8b192b commit a3b1a30
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
from homeassistant.const import CONF_API_KEY
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.selector import (
TextSelector,
TextSelectorConfig,
TextSelectorType,
)

from .const import CONF_STATION, DOMAIN

Expand Down Expand Up @@ -118,3 +123,56 @@ async def async_step_reauth_confirm(
data_schema=vol.Schema({vol.Required(CONF_API_KEY): cv.string}),
errors=errors,
)

async def async_step_reconfigure(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle re-configuration with Trafikverket."""

self.entry = self._get_reconfigure_entry()
return await self.async_step_reconfigure_confirm()

async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm re-configuration with Trafikverket."""
errors: dict[str, str] = {}

if user_input:
try:
await self.validate_input(
user_input[CONF_API_KEY], user_input[CONF_STATION]
)
except InvalidAuthentication:
errors["base"] = "invalid_auth"
except NoWeatherStationFound:
errors["base"] = "invalid_station"
except MultipleWeatherStationsFound:
errors["base"] = "more_stations"
except Exception: # noqa: BLE001
errors["base"] = "cannot_connect"
else:
return self.async_update_reload_and_abort(
self.entry,
title=user_input[CONF_STATION],
data=user_input,
reason="reconfigure_successful",
)

schema = self.add_suggested_values_to_schema(
vol.Schema(
{
vol.Required(CONF_API_KEY): TextSelector(
TextSelectorConfig(type=TextSelectorType.PASSWORD)
),
vol.Required(CONF_STATION): TextSelector(),
}
),
{**self.entry.data, **(user_input or {})},
)

return self.async_show_form(
step_id="reconfigure_confirm",
data_schema=schema,
errors=errors,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"config": {
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
Expand All @@ -21,6 +22,12 @@
"data": {
"api_key": "[%key:common::config_flow::data::api_key%]"
}
},
"reconfigure_confirm": {
"data": {
"api_key": "[%key:common::config_flow::data::api_key%]",
"station": "[%key:component::trafikverket_weatherstation::config::step::user::data::station%]"
}
}
}
},
Expand Down
108 changes: 108 additions & 0 deletions tests/components/trafikverket_weatherstation/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,111 @@ async def test_reauth_flow_fails(

assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": base_error}


async def test_reconfigure_flow(hass: HomeAssistant) -> None:
"""Test a reconfigure flow."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_API_KEY: "1234567890",
CONF_STATION: "Vallby",
},
)
entry.add_to_hass(hass)

result = await entry.start_reconfigure_flow(hass)
assert result["step_id"] == "reconfigure_confirm"
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}

with (
patch(
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
),
patch(
"homeassistant.components.trafikverket_weatherstation.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {"api_key": "1234567891", "station": "Vallby_new"}


@pytest.mark.parametrize(
("side_effect", "base_error"),
[
(
InvalidAuthentication,
"invalid_auth",
),
(
NoWeatherStationFound,
"invalid_station",
),
(
MultipleWeatherStationsFound,
"more_stations",
),
(
Exception,
"cannot_connect",
),
],
)
async def test_reconfigure_flow_fails(
hass: HomeAssistant, side_effect: Exception, base_error: str
) -> None:
"""Test a reauthentication flow."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_API_KEY: "1234567890",
CONF_STATION: "Vallby",
},
)
entry.add_to_hass(hass)

result = await entry.start_reconfigure_flow(hass)
assert result["step_id"] == "reconfigure_confirm"
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}

with patch(
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
side_effect=side_effect(),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": base_error}

with (
patch(
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
),
patch(
"homeassistant.components.trafikverket_weatherstation.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {"api_key": "1234567891", "station": "Vallby_new"}

0 comments on commit a3b1a30

Please sign in to comment.