Use SchemaOptionsFlowHandler in buienradar (#82901)

* Use SchemaOptionsFlowHandler in buienradar

* Use NumberSelector

* Add mode and unit of measurement
This commit is contained in:
epenet 2022-11-30 08:35:19 +01:00 committed by GitHub
parent fb57785ca6
commit fcf60a3b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,8 @@
"""Config flow for buienradar integration.""" """Config flow for buienradar integration."""
from __future__ import annotations from __future__ import annotations
from typing import Any import copy
from typing import Any, cast
import voluptuous as vol import voluptuous as vol
@ -10,7 +11,13 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import selector
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.schema_config_entry_flow import (
SchemaCommonFlowHandler,
SchemaFlowFormStep,
SchemaOptionsFlowHandler,
)
from .const import ( from .const import (
CONF_COUNTRY, CONF_COUNTRY,
@ -23,6 +30,47 @@ from .const import (
SUPPORTED_COUNTRY_CODES, SUPPORTED_COUNTRY_CODES,
) )
OPTIONS_SCHEMA = vol.Schema(
{
vol.Optional(CONF_COUNTRY, default=DEFAULT_COUNTRY): vol.In(
SUPPORTED_COUNTRY_CODES
),
vol.Optional(CONF_DELTA, default=DEFAULT_DELTA): selector.NumberSelector(
selector.NumberSelectorConfig(
min=0,
step=1,
mode=selector.NumberSelectorMode.BOX,
unit_of_measurement="seconds",
),
),
vol.Optional(
CONF_TIMEFRAME, default=DEFAULT_TIMEFRAME
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=5,
max=120,
step=5,
mode=selector.NumberSelectorMode.BOX,
unit_of_measurement="minutes",
),
),
}
)
def _options_suggested_values(handler: SchemaCommonFlowHandler) -> dict[str, Any]:
parent_handler = cast(SchemaOptionsFlowHandler, handler.parent_handler)
suggested_values = copy.deepcopy(dict(parent_handler.config_entry.data))
suggested_values.update(parent_handler.options)
return suggested_values
OPTIONS_FLOW = {
"init": SchemaFlowFormStep(
OPTIONS_SCHEMA, suggested_values=_options_suggested_values
),
}
class BuienradarFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): class BuienradarFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for buienradar.""" """Handle a config flow for buienradar."""
@ -33,9 +81,9 @@ class BuienradarFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
@callback @callback
def async_get_options_flow( def async_get_options_flow(
config_entry: ConfigEntry, config_entry: ConfigEntry,
) -> BuienradarOptionFlowHandler: ) -> SchemaOptionsFlowHandler:
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return BuienradarOptionFlowHandler(config_entry) return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
async def async_step_user( async def async_step_user(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
@ -66,49 +114,3 @@ class BuienradarFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
data_schema=data_schema, data_schema=data_schema,
errors={}, errors={},
) )
class BuienradarOptionFlowHandler(config_entries.OptionsFlow):
"""Handle options."""
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_COUNTRY,
default=self.config_entry.options.get(
CONF_COUNTRY,
self.config_entry.data.get(CONF_COUNTRY, DEFAULT_COUNTRY),
),
): vol.In(SUPPORTED_COUNTRY_CODES),
vol.Optional(
CONF_DELTA,
default=self.config_entry.options.get(
CONF_DELTA,
self.config_entry.data.get(CONF_DELTA, DEFAULT_DELTA),
),
): vol.All(vol.Coerce(int), vol.Range(min=0)),
vol.Optional(
CONF_TIMEFRAME,
default=self.config_entry.options.get(
CONF_TIMEFRAME,
self.config_entry.data.get(
CONF_TIMEFRAME, DEFAULT_TIMEFRAME
),
),
): vol.All(vol.Coerce(int), vol.Range(min=5, max=120)),
}
),
)