Fix config deprecation

Updated config flow to comply with new changes in HA
pull/45/head
schizza 2025-02-13 17:32:07 +01:00
parent 25bb26bb4d
commit d9cb2179d5
1 changed files with 38 additions and 22 deletions

View File

@ -4,7 +4,7 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant.config_entries import ConfigFlow, OptionsFlow
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -30,12 +30,25 @@ class InvalidAuth(HomeAssistantError):
"""Invalid auth exception.""" """Invalid auth exception."""
class ConfigOptionsFlowHandler(config_entries.OptionsFlow): class ConfigOptionsFlowHandler(OptionsFlow):
"""Handle WeatherStation ConfigFlow.""" """Handle WeatherStation ConfigFlow."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None: def __init__(self) -> None:
"""Initialize flow.""" """Initialize flow."""
self.config_entry = config_entry super().__init__()
self.windy_data: dict[str, Any] = {}
self.windy_data_schema = {}
self.user_data: dict[str, str] = {}
self.user_data_schema = {}
self.sensors: dict[str, Any] = {}
@property
def config_entry(self):
return self.hass.config_entries.async_get_entry(self.handler)
def _get_entry_data(self):
"""Get entry data."""
self.user_data: dict[str, str] = { self.user_data: dict[str, str] = {
API_ID: self.config_entry.options.get(API_ID), API_ID: self.config_entry.options.get(API_ID),
@ -44,6 +57,19 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
DEV_DBG: self.config_entry.options.get(DEV_DBG), DEV_DBG: self.config_entry.options.get(DEV_DBG),
} }
self.user_data_schema = {
vol.Required(API_ID, default=self.user_data[API_ID] or ""): str,
vol.Required(API_KEY, default=self.user_data[API_KEY] or ""): str,
vol.Optional(WSLINK, default=self.user_data[WSLINK]): bool,
vol.Optional(DEV_DBG, default=self.user_data[DEV_DBG]): bool,
}
self.sensors: dict[str, Any] = {
SENSORS_TO_LOAD: self.config_entry.options.get(SENSORS_TO_LOAD)
if isinstance(self.config_entry.options.get(SENSORS_TO_LOAD), list)
else []
}
self.windy_data: dict[str, Any] = { self.windy_data: dict[str, Any] = {
WINDY_API_KEY: self.config_entry.options.get(WINDY_API_KEY), WINDY_API_KEY: self.config_entry.options.get(WINDY_API_KEY),
WINDY_ENABLED: self.config_entry.options.get(WINDY_ENABLED) WINDY_ENABLED: self.config_entry.options.get(WINDY_ENABLED)
@ -54,19 +80,6 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
else False, else False,
} }
self.sensors: dict[str, Any] = {
SENSORS_TO_LOAD: self.config_entry.options.get(SENSORS_TO_LOAD)
if isinstance(self.config_entry.options.get(SENSORS_TO_LOAD), list)
else []
}
self.user_data_schema = {
vol.Required(API_ID, default=self.user_data[API_ID] or ""): str,
vol.Required(API_KEY, default=self.user_data[API_KEY] or ""): str,
vol.Optional(WSLINK, default=self.user_data[WSLINK]): bool,
vol.Optional(DEV_DBG, default=self.user_data[DEV_DBG]): bool,
}
self.windy_data_schema = { self.windy_data_schema = {
vol.Optional( vol.Optional(
WINDY_API_KEY, default=self.windy_data[WINDY_API_KEY] or "" WINDY_API_KEY, default=self.windy_data[WINDY_API_KEY] or ""
@ -86,6 +99,8 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
"""Manage basic options - credentials.""" """Manage basic options - credentials."""
errors = {} errors = {}
self._get_entry_data()
if user_input is None: if user_input is None:
return self.async_show_form( return self.async_show_form(
step_id="basic", step_id="basic",
@ -121,6 +136,8 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
"""Manage windy options.""" """Manage windy options."""
errors = {} errors = {}
self._get_entry_data()
if user_input is None: if user_input is None:
return self.async_show_form( return self.async_show_form(
step_id="windy", step_id="windy",
@ -149,7 +166,7 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
return self.async_create_entry(title=DOMAIN, data=user_input) return self.async_create_entry(title=DOMAIN, data=user_input)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): class ConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Sencor SWS 12500 Weather Station.""" """Handle a config flow for Sencor SWS 12500 Weather Station."""
data_schema = { data_schema = {
@ -191,10 +208,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors=errors, errors=errors,
) )
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow( def async_get_options_flow(config_entry) -> ConfigOptionsFlowHandler:
config_entry: config_entries.ConfigEntry,
) -> ConfigOptionsFlowHandler:
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return ConfigOptionsFlowHandler(config_entry) return ConfigOptionsFlowHandler()