1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00
ha-core/homeassistant/components/buienradar/config_flow.py

115 lines
3.6 KiB
Python
Raw Normal View History

Add configuration flow for Buienradar integration (#37796) * Add configuration flow for Buienradar integration * Update buienradar camera tests to work with config flow * Update buienradar weather tests to work with config flow * Update buienradar sensor tests to work with config flow * Remove buienradar config_flow tests to pass tests * Add config flow tests for buienradar integration * Increase test coverage for buienradar config_flow tests * Move data into domain * Remove forecast option * Move data to options * Remove options from config flow * Adjust tests * Adjust string * Fix pylint issues * Rework review comments * Handle import * Change config flow to setup camera or weather * Fix tests * Remove translated file * Fix pylint * Fix flake8 * Fix unload * Minor name changes * Update homeassistant/components/buienradar/config_flow.py Co-authored-by: Ties de Kock <ties@tiesdekock.nl> * Remove asynctest * Add translation * Disable sensors by default * Remove integration name from translations * Remove import method * Drop selection between platforms, disable camera by default * Minor fix in configured_instances * Bugfix in weather * Rework import * Change unique ids of camera * Fix in import * Fix camera tests * Fix sensor test * Fix sensor test 2 * Fix config flow tests * Add option flow * Add tests for option flow * Add import tests * Some cleanups * Apply suggestions from code review Apply code suggestions Co-authored-by: Franck Nijhof <git@frenck.dev> * Fix isort,black,mypy * Small tweaks and added typing to new parts * Fix review comments (1) * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix review comments (2) * Fix issues * Fix unique id * Improve tests * Extend tests * Fix issue with unload * Address review comments * Add warning when loading platform * Add load/unload test Co-authored-by: Ties de Kock <ties@tiesdekock.nl> Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-05-04 13:49:16 +02:00
"""Config flow for buienradar integration."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from .const import (
CONF_COUNTRY,
CONF_DELTA,
CONF_TIMEFRAME,
DEFAULT_COUNTRY,
DEFAULT_DELTA,
DEFAULT_TIMEFRAME,
DOMAIN,
SUPPORTED_COUNTRY_CODES,
)
class BuienradarFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for buienradar."""
VERSION = 1
@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> BuienradarOptionFlowHandler:
"""Get the options flow for this handler."""
return BuienradarOptionFlowHandler(config_entry)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
lat = user_input.get(CONF_LATITUDE)
lon = user_input.get(CONF_LONGITUDE)
await self.async_set_unique_id(f"{lat}-{lon}")
self._abort_if_unique_id_configured()
return self.async_create_entry(title=f"{lat},{lon}", data=user_input)
data_schema = vol.Schema(
{
vol.Required(
CONF_LATITUDE, default=self.hass.config.latitude
): cv.latitude,
vol.Required(
CONF_LONGITUDE, default=self.hass.config.longitude
): cv.longitude,
}
)
return self.async_show_form(
step_id="user",
data_schema=data_schema,
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)),
}
),
)