From 63605ea216b5a986dcdd31e88599c39c683afbf8 Mon Sep 17 00:00:00 2001 From: Jeroen ter Heerdt Date: Fri, 12 Apr 2024 20:49:34 +0000 Subject: [PATCH] fixing bug with config flow when OWM checkbox is unchecked --- .../smart_irrigation/__init__.py | 18 ++++++++----- .../smart_irrigation/config_flow.py | 27 +++++++------------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/custom_components/smart_irrigation/__init__.py b/custom_components/smart_irrigation/__init__.py index 04692135..55ff845c 100644 --- a/custom_components/smart_irrigation/__init__.py +++ b/custom_components/smart_irrigation/__init__.py @@ -147,13 +147,17 @@ async def options_update_listener(hass, config_entry): hass.data[const.DOMAIN][const.CONF_USE_OWM] = config_entry.options.get( const.CONF_USE_OWM ) - if const.CONF_OWM_API_KEY in config_entry.options: - hass.data[const.DOMAIN][const.CONF_OWM_API_KEY] = config_entry.options.get( - const.CONF_OWM_API_KEY - ).strip() - hass.data[const.DOMAIN][const.CONF_OWM_API_VERSION] = config_entry.options.get( - const.CONF_OWM_API_VERSION - ) + if hass.data[const.DOMAIN][const.CONF_USE_OWM]: + if const.CONF_OWM_API_KEY in config_entry.options: + hass.data[const.DOMAIN][const.CONF_OWM_API_KEY] = config_entry.options.get( + const.CONF_OWM_API_KEY + ).strip() + hass.data[const.DOMAIN][const.CONF_OWM_API_VERSION] = config_entry.options.get( + const.CONF_OWM_API_VERSION + ) + else: + hass.data[const.DOMAIN][const.CONF_OWM_API_KEY] = None + hass.data[const.DOMAIN][const.CONF_OWM_API_VERSION] = None await hass.config_entries.async_reload(config_entry.entry_id) diff --git a/custom_components/smart_irrigation/config_flow.py b/custom_components/smart_irrigation/config_flow.py index 41c3ef5b..5fbc1032 100644 --- a/custom_components/smart_irrigation/config_flow.py +++ b/custom_components/smart_irrigation/config_flow.py @@ -8,6 +8,7 @@ from .helpers import test_api_key, InvalidAuth, CannotConnect from .options_flow import SmartIrrigationOptionsFlowHandler + class SmartIrrigationConfigFlow(config_entries.ConfigFlow, domain=const.DOMAIN): """Config flow for SmartIrrigation.""" @@ -33,23 +34,23 @@ async def async_step_user(self, user_input=None): self._name = user_input[const.CONF_INSTANCE_NAME] self._use_owm = user_input[const.CONF_USE_OWM] if not self._use_owm: - #else create the entry right away + # else create the entry right away return self.async_create_entry(title=const.NAME, data=user_input) return await self._show_step_1(user_input) except NotUnique: self._errors["base"] = "name" return await self._show_step_user(user_input) - async def _show_step_user(self,user_input): + async def _show_step_user(self, user_input): return self.async_show_form( - step_id = "user", + step_id="user", data_schema=vol.Schema( { vol.Required(const.CONF_INSTANCE_NAME, default=const.NAME): str, vol.Required(const.CONF_USE_OWM, default=True): bool, } ), - errors = self._errors, + errors=self._errors, ) async def async_step_step1(self, user_input=None): @@ -63,9 +64,7 @@ async def async_step_step1(self, user_input=None): self._owm_api_version = user_input[const.CONF_OWM_API_VERSION] user_input[const.CONF_USE_OWM] = self._use_owm user_input[const.CONF_INSTANCE_NAME] = self._name - await test_api_key( - self.hass,self._owm_api_key,self._owm_api_version - ) + await test_api_key(self.hass, self._owm_api_key, self._owm_api_version) return self.async_create_entry(title=const.NAME, data=user_input) except InvalidAuth: @@ -73,40 +72,34 @@ async def async_step_step1(self, user_input=None): except CannotConnect: self._errors["base"] = "auth" - return await self._show_step_1(user_input) return await self._show_step_1(user_input) - async def _show_step_1(self,user_input): + async def _show_step_1(self, user_input): return self.async_show_form( - step_id = "step1", + step_id="step1", data_schema=vol.Schema( { - vol.Required(const.CONF_OWM_API_KEY): str, vol.Required(const.CONF_OWM_API_VERSION, default="3.0"): selector( {"select": {"options": ["2.5", "3.0"]}} ), } ), - errors = self._errors, + errors=self._errors, ) - @staticmethod @callback def async_get_options_flow(config_entry): """Get options flow.""" return SmartIrrigationOptionsFlowHandler(config_entry) - - async def _check_unique(self, thename): """Test if the specified name is not already claimed.""" await self.async_set_unique_id(thename) self._abort_if_unique_id_configured() - class NotUnique(exceptions.HomeAssistantError): - """Error to indicate there is invalid auth.""" \ No newline at end of file + """Error to indicate there is invalid auth."""