From 11caa2ec143906b25f2545dbe2dfa793fbbea3c5 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Thu, 3 Oct 2024 16:33:29 +0200 Subject: [PATCH] Add reconfigure flow --- custom_components/blitzortung/config_flow.py | 38 +++++++++++++++++++ custom_components/blitzortung/strings.json | 18 ++++++--- .../blitzortung/translations/en.json | 14 ++++++- .../blitzortung/translations/pl.json | 18 ++++++--- 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/custom_components/blitzortung/config_flow.py b/custom_components/blitzortung/config_flow.py index 7eb0103..6a4df7a 100644 --- a/custom_components/blitzortung/config_flow.py +++ b/custom_components/blitzortung/config_flow.py @@ -21,6 +21,13 @@ DOMAIN, ) +RECONFIGURE_SCHEMA = vol.Schema( + { + vol.Required(CONF_LATITUDE): cv.latitude, + vol.Required(CONF_LONGITUDE): cv.longitude, + } +) + class BlitortungConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for blitzortung.""" @@ -64,6 +71,37 @@ async def async_step_user( } ), ) + + async def async_step_reconfigure( + self, _: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a reconfiguration flow initialized by the user.""" + self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) + + return await self.async_step_reconfigure_confirm() + + async def async_step_reconfigure_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a reconfiguration flow initialized by the user.""" + errors = {} + + if user_input is not None: + self.hass.config_entries.async_update_entry( + self.entry, data=self.entry.data | user_input + ) + await self.hass.config_entries.async_reload(self.entry.entry_id) + return self.async_abort(reason="reconfigure_successful") + + return self.async_show_form( + step_id="reconfigure_confirm", + data_schema=self.add_suggested_values_to_schema( + data_schema=RECONFIGURE_SCHEMA, + suggested_values=self.entry.data | (user_input or {}), + ), + description_placeholders={"name": self.entry.title}, + errors=errors, + ) @staticmethod def async_get_options_flow(config_entry: ConfigEntry): diff --git a/custom_components/blitzortung/strings.json b/custom_components/blitzortung/strings.json index c912d17..a2de55f 100644 --- a/custom_components/blitzortung/strings.json +++ b/custom_components/blitzortung/strings.json @@ -4,13 +4,23 @@ "user": { "description": "Set up Blitzortung lightning detection", "data": { - "name": "Name of the integration instance" + "name": "Name of the integration instance", + "latitude": "Latitude", + "longitude": "Longitude" + } + }, + "reconfigure_confirm": { + "description": "Update configuration for {name}.", + "data": { + "latitude": "Latitude", + "longitude": "Longitude" } } }, "error": {}, "abort": { - "already_configured": "Service is already configured" + "already_configured": "Service is already configured", + "reconfigure_successful": "Re-configuration was successful" } }, "options": { @@ -21,9 +31,7 @@ "data": { "radius": "Lightning detection radius (km / mi)", "time_window": "Time window (minutes, 0 - disabled)", - "max_tracked_lightnings": "Max number of tracked lightnings", - "latitude": "Latitude", - "longitude": "Longitude" + "max_tracked_lightnings": "Max number of tracked lightnings" } } } diff --git a/custom_components/blitzortung/translations/en.json b/custom_components/blitzortung/translations/en.json index c912d17..0f1a198 100644 --- a/custom_components/blitzortung/translations/en.json +++ b/custom_components/blitzortung/translations/en.json @@ -4,13 +4,23 @@ "user": { "description": "Set up Blitzortung lightning detection", "data": { - "name": "Name of the integration instance" + "name": "Name of the integration instance", + "latitude": "Latitude", + "longitude": "Longitude" + } + }, + "reconfigure_confirm": { + "description": "Update configuration for {name}.", + "data": { + "latitude": "Latitude", + "longitude": "Longitude" } } }, "error": {}, "abort": { - "already_configured": "Service is already configured" + "already_configured": "Service is already configured", + "reconfigure_successful": "Re-configuration was successful" } }, "options": { diff --git a/custom_components/blitzortung/translations/pl.json b/custom_components/blitzortung/translations/pl.json index 4e7b2c7..7f39d94 100644 --- a/custom_components/blitzortung/translations/pl.json +++ b/custom_components/blitzortung/translations/pl.json @@ -4,13 +4,23 @@ "user": { "description": "Konfiguracja wykrywania błyskawic Blitzortung", "data": { - "name": "Nazwa instancji integracji" + "name": "Nazwa instancji integracji", + "latitude": "Szerokość geograficzna", + "longitude": "Długość geograficzna" + } + }, + "reconfigure_confirm": { + "description": "Zaktualizuj konfigurację dla {name}.", + "data": { + "latitude": "Szerokość geograficzna", + "longitude": "Długość geograficzna" } } }, "error": {}, "abort": { - "already_configured": "Usługa jest już skonfigurowana" + "already_configured": "Usługa jest już skonfigurowana", + "reconfigure_successful": "Ponowna konfiguracja powiodła się" } }, "options": { @@ -21,9 +31,7 @@ "data": { "radius": "Promień wykrywania błyskawic (km / mi)", "time_window": "Okno czasowe (minuty, 0 - wyłączony)", - "max_tracked_lightnings": "Maksymalna ilość śledzonych błyskawic", - "latitude": "Szerokość geograficzna", - "longitude": "Długość geograficzna" + "max_tracked_lightnings": "Maksymalna ilość śledzonych błyskawic" } } }