Initial config and ConfigFlow update.

Minor changes to configuration flow.
pull/1/head
schizza 2024-03-09 15:19:21 +01:00
parent b0c7c72645
commit 19f7baf7d7
6 changed files with 183 additions and 145 deletions

View File

@ -1,4 +1,6 @@
"""Config flow for Sencor SWS 12500 Weather Station integration.""" """Config flow for Sencor SWS 12500 Weather Station integration."""
from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
@ -8,6 +10,7 @@ from homeassistant.exceptions import HomeAssistantError
from .const import ( from .const import (
API_ID, API_ID,
API_KEY, API_KEY,
DEV_DBG,
DOMAIN, DOMAIN,
INVALID_CREDENTIALS, INVALID_CREDENTIALS,
WINDY_API_KEY, WINDY_API_KEY,
@ -15,13 +18,6 @@ from .const import (
WINDY_LOGGER_ENABLED, WINDY_LOGGER_ENABLED,
) )
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(API_ID, default="API ID"): str,
vol.Required(API_KEY, default="API KEY"): str,
}
)
class CannotConnect(HomeAssistantError): class CannotConnect(HomeAssistantError):
"""We can not connect. - not used in push mechanism.""" """We can not connect. - not used in push mechanism."""
@ -32,12 +28,41 @@ class InvalidAuth(HomeAssistantError):
class ConfigOptionsFlowHandler(config_entries.OptionsFlow): class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle WeatherStation options.""" """Handle WeatherStation ConfigFlow."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None: def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow.""" """Initialize flow."""
self.config_entry = config_entry self.config_entry = config_entry
self.user_data: dict[str, str] = {
API_ID: self.config_entry.options.get(API_ID),
API_KEY: self.config_entry.options.get(API_KEY),
DEV_DBG: self.config_entry.options.get(DEV_DBG),
}
self.windy_data: dict[str, Any] = {
WINDY_API_KEY: self.config_entry.options.get(WINDY_API_KEY),
WINDY_ENABLED: self.config_entry.options.get(WINDY_ENABLED) if isinstance(self.config_entry.options.get(WINDY_ENABLED), bool) else False,
WINDY_LOGGER_ENABLED: self.config_entry.options.get(WINDY_LOGGER_ENABLED) if isinstance(self.config_entry.options.get(WINDY_LOGGER_ENABLED), bool) else False,
}
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(DEV_DBG, default=self.user_data[DEV_DBG]): bool,
}
self.windy_data_schema = {
vol.Optional(
WINDY_API_KEY, default=self.windy_data[WINDY_API_KEY] or ""
): str,
vol.Optional(WINDY_ENABLED, default=self.windy_data[WINDY_ENABLED]): bool,
vol.Optional(
WINDY_LOGGER_ENABLED,
default=self.windy_data[WINDY_LOGGER_ENABLED],
): bool,
}
async def async_step_init(self, user_input=None): async def async_step_init(self, user_input=None):
"""Manage the options - show menu first.""" """Manage the options - show menu first."""
return self.async_show_menu(step_id="init", menu_options=["basic", "windy"]) return self.async_show_menu(step_id="init", menu_options=["basic", "windy"])
@ -46,25 +71,17 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
"""Manage basic options - credentials.""" """Manage basic options - credentials."""
errors = {} errors = {}
api_id = self.config_entry.options.get(API_ID)
api_key = self.config_entry.options.get(API_KEY)
OPTIONAL_USER_DATA_SCHEMA = vol.Schema( # pylint: disable=invalid-name
{
vol.Required(API_ID, default=api_id): str,
vol.Required(API_KEY, default=api_key): str,
}
)
if user_input is None: if user_input is None:
return self.async_show_form( return self.async_show_form(
step_id="basic", data_schema=OPTIONAL_USER_DATA_SCHEMA, errors=errors step_id="basic",
data_schema=vol.Schema(self.user_data_schema),
errors=errors,
) )
if user_input[API_ID] in INVALID_CREDENTIALS: if user_input[API_ID] in INVALID_CREDENTIALS:
errors["base"] = "valid_credentials_api" errors[API_ID] = "valid_credentials_api"
elif user_input[API_KEY] in INVALID_CREDENTIALS: elif user_input[API_KEY] in INVALID_CREDENTIALS:
errors["base"] = "valid_credentials_key" errors[API_KEY] = "valid_credentials_key"
elif user_input[API_KEY] == user_input[API_ID]: elif user_input[API_KEY] == user_input[API_ID]:
errors["base"] = "valid_credentials_match" errors["base"] = "valid_credentials_match"
else: else:
@ -76,55 +93,58 @@ class ConfigOptionsFlowHandler(config_entries.OptionsFlow):
WINDY_LOGGER_ENABLED WINDY_LOGGER_ENABLED
) )
data.update(user_input) # retain windy data
user_input.update(self.windy_data)
return self.async_create_entry(title=DOMAIN, data=data) return self.async_create_entry(title=DOMAIN, data=user_input)
self.user_data = user_input
# we are ending with error msg, reshow form # we are ending with error msg, reshow form
return self.async_show_form( return self.async_show_form(
step_id="basic", data_schema=OPTIONAL_USER_DATA_SCHEMA, errors=errors step_id="basic",
data_schema=vol.Schema(self.user_data_schema),
errors=errors,
) )
async def async_step_windy(self, user_input=None): async def async_step_windy(self, user_input=None):
"""Manage windy options.""" """Manage windy options."""
errors = {} errors = {}
windy_key = self.config_entry.options.get(WINDY_API_KEY)
windy_enabled = self.config_entry.options.get(WINDY_ENABLED)
windy_logger_enabled = self.config_entry.options.get(WINDY_LOGGER_ENABLED)
OPTIONAL_USER_DATA_SCHEMA = vol.Schema( # pylint: disable=invalid-name
{
vol.Optional(WINDY_API_KEY, default=windy_key): str,
vol.Optional(WINDY_ENABLED, default=windy_enabled): bool,
vol.Optional(WINDY_LOGGER_ENABLED, default=windy_logger_enabled): bool,
}
)
if user_input is None: if user_input is None:
return self.async_show_form( return self.async_show_form(
step_id="windy", data_schema=OPTIONAL_USER_DATA_SCHEMA, errors=errors step_id="windy",
data_schema=vol.Schema(self.windy_data_schema),
errors=errors,
) )
if (user_input[WINDY_ENABLED] is True) and (user_input[WINDY_API_KEY] == ""): if (user_input[WINDY_ENABLED] is True) and (user_input[WINDY_API_KEY] == ""):
errors["base"] = "windy_key_required" errors[WINDY_API_KEY] = "windy_key_required"
return self.async_show_form( return self.async_show_form(
step_id="windy", data_schema=OPTIONAL_USER_DATA_SCHEMA, errors=errors step_id="windy",
data_schema=self.windy_data_schema,
description_placeholders={
WINDY_ENABLED: True,
WINDY_LOGGER_ENABLED: user_input[WINDY_LOGGER_ENABLED],
},
errors=errors,
) )
# retain API_ID and API_KEY from config # retain user_data
data: dict = {} user_input.update(self.user_data)
data[API_ID] = self.config_entry.options.get(API_ID)
data[API_KEY] = self.config_entry.options.get(API_KEY)
data.update(user_input) return self.async_create_entry(title=DOMAIN, data=user_input)
return self.async_create_entry(title=DOMAIN, data=data)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): class ConfigFlow(config_entries.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 = {
vol.Required(API_ID): str,
vol.Required(API_KEY): str,
vol.Optional(DEV_DBG): bool,
}
VERSION = 1 VERSION = 1
async def async_step_user(self, user_input=None): async def async_step_user(self, user_input=None):
@ -134,24 +154,25 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
return self.async_show_form( return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA step_id="user",
data_schema=vol.Schema(self.data_schema),
) )
errors = {} errors = {}
if user_input[API_ID] in INVALID_CREDENTIALS: if user_input[API_ID] in INVALID_CREDENTIALS:
errors["base"] = "valid_credentials_api" errors[API_ID] = "valid_credentials_api"
elif user_input[API_KEY] in INVALID_CREDENTIALS: elif user_input[API_KEY] in INVALID_CREDENTIALS:
errors["base"] = "valid_credentials_key" errors[API_KEY] = "valid_credentials_key"
elif user_input[API_KEY] == user_input[API_ID]: elif user_input[API_KEY] == user_input[API_ID]:
errors["base"] = "valid_credentials_match" errors["base"] = "valid_credentials_match"
else: else:
return self.async_create_entry( return self.async_create_entry(title=DOMAIN, data=user_input, options=user_input)
title=DOMAIN, data=user_input, options=user_input
)
return self.async_show_form( return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors step_id="user",
data_schema=vol.Schema(self.data_schema),
errors=errors,
) )
@staticmethod @staticmethod

View File

@ -10,10 +10,12 @@ ICON = "mdi:weather"
API_KEY = "API_KEY" API_KEY = "API_KEY"
API_ID = "API_ID" API_ID = "API_ID"
DEV_DBG: Final = "dev_debug_checkbox"
WINDY_API_KEY = "WINDY_API_KEY" WINDY_API_KEY = "WINDY_API_KEY"
WINDY_ENABLED: Final = "windy_enabled_checkbox" WINDY_ENABLED: Final = "windy_enabled_checkbox"
WINDY_LOGGER_ENABLED: Final = "windy_logger_checkbox" WINDY_LOGGER_ENABLED: Final = "windy_logger_checkbox"
WINDY_NOT_INSERTED: Final = "Data was succefuly sent to Windy, but not inserted by Windy API. Does anyone else sent data to Windy?" WINDY_NOT_INSERTED: Final = "Data was succefuly sent to Windy, but not inserted by Windy API. Does anyone else sent data to Windy?"
WINDY_INVALID_KEY: Final = "Windy API KEY is invalid. Send data to Windy is now disabled. Check your API KEY and try again." WINDY_INVALID_KEY: Final = "Windy API KEY is invalid. Send data to Windy is now disabled. Check your API KEY and try again."
WINDY_SUCCESS: Final = ( WINDY_SUCCESS: Final = (

View File

@ -9,6 +9,6 @@
"iot_class": "local_push", "iot_class": "local_push",
"requirements": [], "requirements": [],
"ssdp": [], "ssdp": [],
"version": "0.0.2", "version": "0.1.1",
"zeroconf": [] "zeroconf": []
} }

View File

@ -4,11 +4,10 @@ from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntityDescription,
SensorEntity, SensorEntity,
SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
UnitOfIrradiance, UnitOfIrradiance,
@ -17,7 +16,6 @@ from homeassistant.const import (
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo

View File

@ -1,54 +1,62 @@
{ {
"config": { "config": {
"step": {
"user": {
"title": "Configure access for Weather Station",
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"data": {
"api_id": "API ID / Station ID",
"api_key": "API KEY / Password"
}
}
},
"error": { "error": {
"valid_credentials_api": "Provide valid API ID.", "valid_credentials_api": "Provide valid API ID.",
"valid_credentials_key": "Provide valid API KEY.", "valid_credentials_key": "Provide valid API KEY.",
"valid_credentials_match": "API ID and API KEY should not be the same." "valid_credentials_match": "API ID and API KEY should not be the same."
},
"step": {
"user": {
"data": {
"API_ID": "API ID / Station ID",
"API_KEY": "API KEY / Password",
"dev_debug_checkbox": "Developer log"
},
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"title": "Configure access for Weather Station",
"data_description": {
"dev_debug_checkbox": " Enable only if you want to send debuging data to the developer."
}
}
} }
}, },
"options": { "options": {
"step": {
"init": {
"title": "Configure SWS12500 Integration",
"description": "Choose what do you want to configure. If basic access or resending data for Windy site",
"menu_options": {
"basic": "Basic - configure credentials for Weather Station",
"windy": "Windy configuration"
}
},
"basic": {
"title": "Configure credentials",
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"data": {
"API_ID": "API ID / Station ID",
"API_KEY": "API KEY / Password"
}
},
"windy": {
"title": "Configure Windy",
"description": "Resend weather data to your Windy stations.",
"data": {
"WINDY_API_KEY": "API KEY provided by Windy",
"windy_enabled_checkbox": "Enable resending data to Windy",
"windy_logger_checkbox": "Log Windy data and responses"
}
}
},
"error": { "error": {
"valid_credentials_api": "Provide valid API ID.", "valid_credentials_api": "Provide valid API ID.",
"valid_credentials_key": "Provide valid API KEY.", "valid_credentials_key": "Provide valid API KEY.",
"valid_credentials_match": "API ID and API KEY should not be the same.", "valid_credentials_match": "API ID and API KEY should not be the same.",
"windy_key_required": "Windy API key is required if you want to enable this function." "windy_key_required": "Windy API key is required if you want to enable this function."
},
"step": {
"basic": {
"data": {
"API_ID": "API ID / Station ID",
"API_KEY": "API KEY / Password",
"dev_debug_checkbox": "Developer log"
},
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"title": "Configure credentials",
"data_description": {
"dev_debug_checkbox": " Enable only if you want to send debuging data to the developer."
}
},
"init": {
"description": "Choose what do you want to configure. If basic access or resending data for Windy site",
"menu_options": {
"basic": "Basic - configure credentials for Weather Station",
"windy": "Windy configuration"
},
"title": "Configure SWS12500 Integration"
},
"windy": {
"data": {
"WINDY_API_KEY": "API KEY provided by Windy",
"windy_enabled_checkbox": "Enable resending data to Windy",
"windy_logger_checkbox": "Log Windy data and responses"
},
"description": "Resend weather data to your Windy stations.",
"title": "Configure Windy"
}
} }
} }
} }

View File

@ -1,54 +1,63 @@
{ {
"config": { "config": {
"error": { "error": {
"valid_credentials_api": "Provide valid API ID.", "valid_credentials_api": "Provide valid API ID.",
"valid_credentials_key": "Provide valid API KEY.", "valid_credentials_key": "Provide valid API KEY.",
"valid_credentials_match": "API ID and API KEY should not be the same." "valid_credentials_match": "API ID and API KEY should not be the same."
},
"step": {
"user": {
"data": {
"api_id": "API ID / Station ID",
"api_key": "API KEY / Password"
},
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"title": "Configure access for Weather Station"
}
}
}, },
"options": { "step": {
"error": { "user": {
"valid_credentials_api": "Provide valid API ID.", "data": {
"valid_credentials_key": "Provide valid API KEY.", "API_ID": "API ID / Station ID",
"valid_credentials_match": "API ID and API KEY should not be the same.", "API_KEY": "API KEY / Password",
"windy_key_required": "Windy API key is required if you want to enable this function." "dev_debug_checkbox": "Developer"
}, },
"step": { "description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"basic": { "title": "Configure access for Weather Station",
"data": { "data_description": {
"API_ID": "API ID / Station ID", "dev_debug_checkbox": " Enable only if you want to send debuging data to the developer."
"API_KEY": "API KEY / Password"
},
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"title": "Configure credentials"
},
"init": {
"description": "Choose what do you want to configure. If basic access or resending data for Windy site",
"menu_options": {
"basic": "Basic - configure credentials for Weather Station",
"windy": "Windy configuration"
},
"title": "Configure SWS12500 Integration"
},
"windy": {
"data": {
"WINDY_API_KEY": "API KEY provided by Windy",
"windy_enabled_checkbox": "Enable resending data to Windy",
"windy_logger_checkbox": "Log Windy data and responses"
},
"description": "Resend weather data to your Windy stations.",
"title": "Configure Windy"
}
} }
}
} }
} },
"options": {
"error": {
"valid_credentials_api": "Provide valid API ID.",
"valid_credentials_key": "Provide valid API KEY.",
"valid_credentials_match": "API ID and API KEY should not be the same.",
"windy_key_required": "Windy API key is required if you want to enable this function."
},
"step": {
"basic": {
"data": {
"API_ID": "API ID / Station ID",
"API_KEY": "API KEY / Password",
"dev_debug_checkbox": "Developer"
},
"description": "Provide API ID and API KEY so the Weather Station can access HomeAssistant",
"title": "Configure credentials",
"data_description": {
"api_id": "data desc.",
"dev_debug_checkbox": " Enable only if you want to send debuging data to the developer."
}
},
"init": {
"description": "Choose what do you want to configure. If basic access or resending data for Windy site",
"menu_options": {
"basic": "Basic - configure credentials for Weather Station",
"windy": "Windy configuration"
},
"title": "Configure SWS12500 Integration"
},
"windy": {
"data": {
"WINDY_API_KEY": "API KEY provided by Windy",
"windy_enabled_checkbox": "Enable resending data to Windy",
"windy_logger_checkbox": "Log Windy data and responses"
},
"description": "Resend weather data to your Windy stations.",
"title": "Configure Windy"
}
}
}
}