From 466d41f1bb9c527662623198fe62008c4ce342f9 Mon Sep 17 00:00:00 2001 From: SchiZzA Date: Tue, 9 Dec 2025 12:01:32 +0100 Subject: [PATCH] Config_flow for Ecowitt --- custom_components/sws12500/config_flow.py | 54 +++++++++++++++++-- custom_components/sws12500/const.py | 1 + .../sws12500/translations/cs.json | 13 +++++ .../sws12500/translations/en.json | 12 +++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/custom_components/sws12500/config_flow.py b/custom_components/sws12500/config_flow.py index 6be91c9..c72f02a 100644 --- a/custom_components/sws12500/config_flow.py +++ b/custom_components/sws12500/config_flow.py @@ -1,19 +1,22 @@ """Config flow for Sencor SWS 12500 Weather Station integration.""" +import secrets from typing import Any import voluptuous as vol +from yarl import URL from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow from homeassistant.core import callback from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.network import get_url from .const import ( API_ID, API_KEY, DEV_DBG, DOMAIN, - ECOWITT, + ECOWITT_ENABLED, ECOWITT_WEBHOOK_ID, INVALID_CREDENTIALS, POCASI_CZ_API_ID, @@ -138,10 +141,11 @@ class ConfigOptionsFlowHandler(OptionsFlow): } self.ecowitt = { - ECOWITT_WEBHOOK_ID: self.config_entry.options.get(ECOWITT_WEBHOOK_ID, "") + ECOWITT_WEBHOOK_ID: self.config_entry.options.get(ECOWITT_WEBHOOK_ID, ""), + ECOWITT_ENABLED: self.config_entry.options.get(ECOWITT_ENABLED, False), } - async def async_step_init(self, user_input=None): + async def async_step_init(self, user_input: dict[str, Any] = {}): """Manage the options - show menu first.""" return self.async_show_menu( step_id="init", menu_options=["basic", "ecowitt", "windy", "pocasi"] @@ -167,7 +171,7 @@ class ConfigOptionsFlowHandler(OptionsFlow): elif user_input[API_KEY] == user_input[API_ID]: errors["base"] = "valid_credentials_match" else: - self.retain_data(user_input) + user_input = self.retain_data(user_input) return self.async_create_entry(title=DOMAIN, data=user_input) @@ -201,7 +205,7 @@ class ConfigOptionsFlowHandler(OptionsFlow): errors=errors, ) - self.retain_data(user_input) + user_input = self.retain_data(user_input) return self.async_create_entry(title=DOMAIN, data=user_input) @@ -239,6 +243,46 @@ class ConfigOptionsFlowHandler(OptionsFlow): return self.async_create_entry(title=DOMAIN, data=user_input) + async def async_step_ecowitt(self, user_input: Any = None) -> ConfigFlowResult: + """Ecowitt stations setup.""" + + errors = {} + await self._get_entry_data() + + if not (webhook := self.ecowitt.get(ECOWITT_WEBHOOK_ID)): + webhook = secrets.token_hex(8) + + if user_input is None: + url: URL = URL(get_url(self.hass)) + + if not url.host: + url.host = "UNKNOWN" + + ecowitt_schema = { + vol.Required( + ECOWITT_WEBHOOK_ID, + default=webhook, + ): str, + vol.Optional( + ECOWITT_ENABLED, + default=self.ecowitt.get(ECOWITT_ENABLED, False), + ): bool, + } + + return self.async_show_form( + step_id="ecowitt", + data_schema=vol.Schema(ecowitt_schema), + description_placeholders={ + "url": url.host, + "port": str(url.port), + "webhook_id": webhook, + }, + errors=errors, + ) + + user_input = self.retain_data(user_input) + return self.async_create_entry(title=DOMAIN, data=user_input) + def retain_data(self, data: dict[str, Any]) -> dict[str, Any]: """Retain user_data.""" diff --git a/custom_components/sws12500/const.py b/custom_components/sws12500/const.py index 16caa6c..fd4f163 100644 --- a/custom_components/sws12500/const.py +++ b/custom_components/sws12500/const.py @@ -26,6 +26,7 @@ WSLINK: Final = "wslink" ECOWITT: Final = "ecowitt" ECOWITT_WEBHOOK_ID: Final = "ecowitt_webhook_id" +ECOWITT_ENABLED: Final = "ecowitt_enabled" POCASI_CZ_API_KEY = "POCASI_CZ_API_KEY" POCASI_CZ_API_ID = "POCASI_CZ_API_ID" diff --git a/custom_components/sws12500/translations/cs.json b/custom_components/sws12500/translations/cs.json index 3b7c9dc..f4a67fb 100644 --- a/custom_components/sws12500/translations/cs.json +++ b/custom_components/sws12500/translations/cs.json @@ -42,6 +42,7 @@ "basic": "Základní - přístupové údaje (přihlášení)", "windy": "Nastavení pro přeposílání dat na Windy", "pocasi": "Nastavení pro přeposlání dat na Počasí Meteo CZ", + "ecowitt": "Nastavení pro stanice Ecowitt", "migration": "Migrace statistiky senzoru" } }, @@ -92,6 +93,18 @@ "pocasi_logger_checkbox": "Zapnout pouze v případě, že chcete zaslat ladící informace vývojáři." } }, + "ecowitt": { + "description": "Nastavení pro Ecowitt", + "title": "Konfigurace pro stanice Ecowitt", + "data": { + "ecowitt_webhook_id": "Unikátní webhook ID", + "ecowitt_enabled": "Povolit data ze stanice Ecowitt" + }, + "data_description": { + "ecowitt_webhook_id": "Nastavení pro stanici: {url}:{port}/weatherhub/{webhook_id}", + "ecowitt_enabled": "Povolit přijímání dat ze stanic Ecowitt" + } + }, "migration": { "title": "Migrace statistiky senzoru.", "description": "Pro správnou funkci dlouhodobé statistiky je nutné provést migraci jednotky senzoru v dlouhodobé statistice. Původní jednotka dlouhodobé statistiky pro denní úhrn srážek byla v mm/d, nicméně stanice zasílá pouze data v mm bez časového rozlišení.\n\n Senzor, který má být migrován je pro denní úhrn srážek. Pokud je v seznamu již správná hodnota u senzoru pro denní úhrn (mm), pak je již migrace hotová.\n\n Výsledek migrace pro senzor: {migration_status}, přepvedeno celkem {migration_count} řádků.", diff --git a/custom_components/sws12500/translations/en.json b/custom_components/sws12500/translations/en.json index 885634a..f7381de 100644 --- a/custom_components/sws12500/translations/en.json +++ b/custom_components/sws12500/translations/en.json @@ -87,6 +87,18 @@ "pocasi_logger_checkbox": "Enable only if you want to send debbug data to the developer" } }, + "ecowitt": { + "description": "Nastavení pro Ecowitt", + "title": "Konfigurace pro stanice Ecowitt", + "data": { + "ecowitt_webhook_id": "Unikátní webhook ID", + "ecowitt_enabled": "Povolit data ze stanice Ecowitt" + }, + "data_description": { + "ecowitt_webhook_id": "Nastavení pro stanici: {url}:{port}/weatherhub/{webhook_id}", + "ecowitt_enabled": "Povolit přijímání dat ze stanic Ecowitt" + } + }, "migration": { "title": "Statistic migration.", "description": "For the correct functioning of long-term statistics, it is necessary to migrate the sensor unit in the long-term statistics. The original unit of long-term statistics for daily precipitation was in mm/d, however, the station only sends data in mm without time differentiation.\n\n The sensor to be migrated is for daily precipitation. If the correct value is already in the list for the daily precipitation sensor (mm), then the migration is already complete.\n\n Migration result for the sensor: {migration_status}, a total of {migration_count} rows converted.",