Manage stations via integrations configuration in Tankerkoenig (#72654)

pull/72665/head
Michael 2022-05-28 22:31:03 +02:00 committed by GitHub
parent a598cdfeb3
commit a4f678e7c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 25 deletions

View File

@ -17,7 +17,7 @@ from homeassistant.const import (
CONF_SHOW_ON_MAP,
LENGTH_KILOMETERS,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.selector import (
@ -29,6 +29,24 @@ from homeassistant.helpers.selector import (
from .const import CONF_FUEL_TYPES, CONF_STATIONS, DEFAULT_RADIUS, DOMAIN, FUEL_TYPES
async def async_get_nearby_stations(
hass: HomeAssistant, data: dict[str, Any]
) -> dict[str, Any]:
"""Fetch nearby stations."""
try:
return await hass.async_add_executor_job(
getNearbyStations,
data[CONF_API_KEY],
data[CONF_LOCATION][CONF_LATITUDE],
data[CONF_LOCATION][CONF_LONGITUDE],
data[CONF_RADIUS],
"all",
"dist",
)
except customException as err:
return {"ok": False, "message": err, "exception": True}
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""
@ -57,7 +75,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
selected_station_ids: list[str] = []
# add all nearby stations
nearby_stations = await self._get_nearby_stations(config)
nearby_stations = await async_get_nearby_stations(self.hass, config)
for station in nearby_stations.get("stations", []):
selected_station_ids.append(station["id"])
@ -91,7 +109,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
)
self._abort_if_unique_id_configured()
data = await self._get_nearby_stations(user_input)
data = await async_get_nearby_stations(self.hass, user_input)
if not data.get("ok"):
return self._show_form_user(
user_input, errors={CONF_API_KEY: "invalid_auth"}
@ -182,21 +200,6 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
options=options,
)
async def _get_nearby_stations(self, data: dict[str, Any]) -> dict[str, Any]:
"""Fetch nearby stations."""
try:
return await self.hass.async_add_executor_job(
getNearbyStations,
data[CONF_API_KEY],
data[CONF_LOCATION][CONF_LATITUDE],
data[CONF_LOCATION][CONF_LONGITUDE],
data[CONF_RADIUS],
"all",
"dist",
)
except customException as err:
return {"ok": False, "message": err, "exception": True}
class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle an options flow."""
@ -204,14 +207,36 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
self._stations: dict[str, str] = {}
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle options flow."""
if user_input is not None:
self.hass.config_entries.async_update_entry(
self.config_entry,
data={
**self.config_entry.data,
CONF_STATIONS: user_input.pop(CONF_STATIONS),
},
)
return self.async_create_entry(title="", data=user_input)
nearby_stations = await async_get_nearby_stations(
self.hass, dict(self.config_entry.data)
)
if stations := nearby_stations.get("stations"):
for station in stations:
self._stations[
station["id"]
] = f"{station['brand']} {station['street']} {station['houseNumber']} - ({station['dist']}km)"
# add possible extra selected stations from import
for selected_station in self.config_entry.data[CONF_STATIONS]:
if selected_station not in self._stations:
self._stations[selected_station] = f"id: {selected_station}"
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
@ -220,6 +245,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
CONF_SHOW_ON_MAP,
default=self.config_entry.options[CONF_SHOW_ON_MAP],
): bool,
vol.Required(
CONF_STATIONS, default=self.config_entry.data[CONF_STATIONS]
): cv.multi_select(self._stations),
}
),
)

View File

@ -32,7 +32,7 @@
"init": {
"title": "Tankerkoenig options",
"data": {
"scan_interval": "Update Interval",
"stations": "Stations",
"show_on_map": "Show stations on map"
}
}

View File

@ -31,8 +31,8 @@
"step": {
"init": {
"data": {
"scan_interval": "Update Interval",
"show_on_map": "Show stations on map"
"show_on_map": "Show stations on map",
"stations": "Stations"
},
"title": "Tankerkoenig options"
}

View File

@ -42,6 +42,15 @@ MOCK_STATIONS_DATA = {
],
}
MOCK_OPTIONS_DATA = {
**MOCK_USER_DATA,
CONF_STATIONS: [
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
"36b4b812-xxxx-xxxx-xxxx-c51735325858",
"54e2b642-xxxx-xxxx-xxxx-87cd4e9867f1",
],
}
MOCK_IMPORT_DATA = {
CONF_API_KEY: "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx",
CONF_FUEL_TYPES: ["e5"],
@ -217,7 +226,7 @@ async def test_options_flow(hass: HomeAssistant):
mock_config = MockConfigEntry(
domain=DOMAIN,
data=MOCK_USER_DATA,
data=MOCK_OPTIONS_DATA,
options={CONF_SHOW_ON_MAP: True},
unique_id=f"{DOMAIN}_{MOCK_USER_DATA[CONF_LOCATION][CONF_LATITUDE]}_{MOCK_USER_DATA[CONF_LOCATION][CONF_LONGITUDE]}",
)
@ -225,17 +234,23 @@ async def test_options_flow(hass: HomeAssistant):
with patch(
"homeassistant.components.tankerkoenig.async_setup_entry"
) as mock_setup_entry:
) as mock_setup_entry, patch(
"homeassistant.components.tankerkoenig.config_flow.getNearbyStations",
return_value=MOCK_NEARVY_STATIONS_OK,
):
await mock_config.async_setup(hass)
await hass.async_block_till_done()
assert mock_setup_entry.called
result = await hass.config_entries.options.async_init(mock_config.entry_id)
result = await hass.config_entries.options.async_init(mock_config.entry_id)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_SHOW_ON_MAP: False},
user_input={
CONF_SHOW_ON_MAP: False,
CONF_STATIONS: MOCK_OPTIONS_DATA[CONF_STATIONS],
},
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert not mock_config.options[CONF_SHOW_ON_MAP]