Skip to content

Commit

Permalink
Adds options flow
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jan 2, 2024
1 parent 92be432 commit 32746d2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions custom_components/dwd_weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
return True


async def update_listener(hass, entry):
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up DWD Weather as config entry."""
_LOGGER.debug("Setup with data {}".format(entry.data))
entry.async_on_unload(entry.add_update_listener(update_listener))

dwd_weather_data = DWDWeatherData(hass, entry)

Expand Down
81 changes: 81 additions & 0 deletions custom_components/dwd_weather/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import (
BooleanSelector,
SelectSelector,
Expand Down Expand Up @@ -231,3 +233,82 @@ async def async_step_station_configure(self, user_input=None):
return self.async_show_form(
step_id="station_configure", data_schema=data_schema, errors=errors
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return OptionsFlowHandler(config_entry)


# 'data_type': 'mixed_data', 'station_name': 'Mittelnkirchen-Hohen', 'wind_direction_type': 'degrees', 'interpolate': True, 'hourly_update': False


class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
_LOGGER.debug(
"OptionsFlowHandler: init for {} with data {}".format(
self.config_entry.title, self.config_entry.data
)
)

async def async_step_init(self, user_input: dict[str] | None = None) -> FlowResult:
"""Manage the options."""
if user_input is not None:
_LOGGER.debug("OptionsFlowHandler: user_input {}".format(user_input))

user_input["station_id"] = self.config_entry.data["station_id"]
user_input["station_name"] = self.config_entry.data["station_name"]
user_input["custom_location"] = self.config_entry.data["custom_location"]
self.hass.config_entries.async_update_entry(
self.config_entry, data=user_input, options=self.config_entry.options
)
return self.async_create_entry(title="", data={})

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_DATA_TYPE,
default=self.config_entry.data["data_type"],
): SelectSelector(
{
"options": list(
[
CONF_DATA_TYPE_MIXED,
CONF_DATA_TYPE_REPORT,
CONF_DATA_TYPE_FORECAST,
]
),
"custom_value": False,
"mode": "list",
"translation_key": CONF_DATA_TYPE,
}
),
vol.Required(
CONF_WIND_DIRECTION_TYPE,
default=self.config_entry.data["wind_direction_type"],
): SelectSelector(
{
"options": list(["degrees", "direction"]),
"custom_value": False,
"mode": "list",
"translation_key": CONF_WIND_DIRECTION_TYPE,
}
),
vol.Required(
CONF_INTERPOLATE,
default=self.config_entry.data["interpolate"],
): BooleanSelector({}),
vol.Required(
CONF_HOURLY_UPDATE,
default=self.config_entry.data["hourly_update"],
): BooleanSelector({}),
}
),
)
20 changes: 20 additions & 0 deletions custom_components/dwd_weather/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@
"unknown": "Unbekannter Fehler"
}
},
"options": {
"step": {
"init": {
"description": "Anpassen der Station",
"title": "Deutscher Wetterdienst",
"data": {
"station_name": "Stationsname",
"wind_direction_type": "Art der Windrichtungsangabe",
"interpolate": "Interpolierung der Werte",
"hourly_update": "Erzwinge die stündliche Aktualisierung der Daten (siehe Hinweis unten)"
},
"data_description": {
"station_name": "Der angezeigte Stationsname",
"wind_direction_type": "Betrifft den Sensorwert für die Windrichtung",
"interpolate": "Da die Daten nur stündlich verfügbar sind, kann es sein, dass 'Sprünge' in den angezeigten Werten auftreten. Mit dieser Option werden die Daten linear interpoliert.",
"hourly_update": "Normalerweise werden die Wetterdaten alle 6 Stunden aktualisiert. Der DWD bietet aber auch eine stündliche Aktualisierung der Daten an, allerdings erhöht das die Datennutzung erheblich (~37MB vs. ~0.17MB an Download-Daten), da die Sensorwerte für alle verfügbaren Stationen in der stündlichen Aktualisierung enthalten sind."
}
}
}
},
"selector": {
"entity_type": {
"options": {
Expand Down
20 changes: 20 additions & 0 deletions custom_components/dwd_weather/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@
"unknown": "Unknown Error"
}
},
"options": {
"step": {
"init": {
"description": "Reconfigure the station",
"title": "Deutscher Wetterdienst",
"data": {
"station_name": "Station name",
"wind_direction_type": "Wind direction value type",
"interpolate": "Interpolate values",
"hourly_update": "Force hourly data update (see warning below)"
},
"data_description": {
"station_name": "This is the displayed name of the station.",
"wind_direction_type": "This affects the sensor value for the wind direction.",
"interpolate": "As the values are only provided on an hourly basis, there might be jumps in the displayed values. With this option, the data will be smoothed.",
"hourly_update": "Normally the weather is updated every 6 hours. DWD provides an hourly data update as well, this will however increase data usage significantly as the sensor values are bundled in the hourly update (~37MB vs. ~0.17MB of download per update). Keep in mind, that this obviously also slows down the update of new values."
}
}
}
},
"selector": {
"entity_type": {
"options": {
Expand Down

0 comments on commit 32746d2

Please sign in to comment.