diff --git a/custom_components/sws12500/config_flow.py b/custom_components/sws12500/config_flow.py index 60c3b83..c72f02a 100644 --- a/custom_components/sws12500/config_flow.py +++ b/custom_components/sws12500/config_flow.py @@ -1,18 +1,23 @@ """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_ENABLED, + ECOWITT_WEBHOOK_ID, INVALID_CREDENTIALS, POCASI_CZ_API_ID, POCASI_CZ_API_KEY, @@ -51,6 +56,8 @@ class ConfigOptionsFlowHandler(OptionsFlow): self.migrate_schema = {} self.pocasi_cz: dict[str, Any] = {} self.pocasi_cz_schema = {} + self.ecowitt: dict[str, Any] = {} + self.ecowitt_schema = {} @property def config_entry(self): @@ -133,10 +140,15 @@ class ConfigOptionsFlowHandler(OptionsFlow): ): bool, } - async def async_step_init(self, user_input=None): + self.ecowitt = { + 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: dict[str, Any] = {}): """Manage the options - show menu first.""" return self.async_show_menu( - step_id="init", menu_options=["basic", "windy", "pocasi"] + step_id="init", menu_options=["basic", "ecowitt", "windy", "pocasi"] ) async def async_step_basic(self, user_input=None): @@ -159,14 +171,7 @@ class ConfigOptionsFlowHandler(OptionsFlow): elif user_input[API_KEY] == user_input[API_ID]: errors["base"] = "valid_credentials_match" else: - # retain windy data - user_input.update(self.windy_data) - - # retain sensors - user_input.update(self.sensors) - - # retain pocasi data - user_input.update(self.pocasi_cz) + user_input = self.retain_data(user_input) return self.async_create_entry(title=DOMAIN, data=user_input) @@ -200,15 +205,7 @@ class ConfigOptionsFlowHandler(OptionsFlow): errors=errors, ) - # retain user_data - user_input.update(self.user_data) - - # retain senors - user_input.update(self.sensors) - - # retain pocasi cz - - user_input.update(self.pocasi_cz) + user_input = self.retain_data(user_input) return self.async_create_entry(title=DOMAIN, data=user_input) @@ -241,17 +238,63 @@ class ConfigOptionsFlowHandler(OptionsFlow): data_schema=vol.Schema(self.pocasi_cz_schema), errors=errors, ) - # retain user data - user_input.update(self.user_data) - # retain senors - user_input.update(self.sensors) - - # retain windy - user_input.update(self.windy_data) + user_input = self.retain_data(user_input) 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.""" + + return { + **self.user_data, + **self.windy_data, + **self.pocasi_cz, + **self.sensors, + **self.ecowitt, + **dict(data), + } + class ConfigFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a config flow for Sencor SWS 12500 Weather Station.""" diff --git a/custom_components/sws12500/const.py b/custom_components/sws12500/const.py index 3fd2298..fd4f163 100644 --- a/custom_components/sws12500/const.py +++ b/custom_components/sws12500/const.py @@ -12,6 +12,7 @@ DATABASE_PATH = "/config/home-assistant_v2.db" POCASI_CZ_URL: Final = "http://ms.pocasimeteo.cz" POCASI_CZ_SEND_MINIMUM: Final = 12 # minimal time to resend data + ICON = "mdi:weather" API_KEY = "API_KEY" @@ -23,6 +24,10 @@ SENSOR_TO_MIGRATE: Final = "sensor_to_migrate" DEV_DBG: Final = "dev_debug_checkbox" 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" POCASI_CZ_SEND_INTERVAL = "POCASI_SEND_INTERVAL" 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.",