Skip to content

Commit

Permalink
fixing bug with config flow when OWM checkbox is unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenterheerdt committed Apr 12, 2024
1 parent 2f9bbb0 commit 63605ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
18 changes: 11 additions & 7 deletions custom_components/smart_irrigation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
27 changes: 10 additions & 17 deletions custom_components/smart_irrigation/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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):
Expand All @@ -63,50 +64,42 @@ 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:
self._errors["base"] = "auth"
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."""
"""Error to indicate there is invalid auth."""

0 comments on commit 63605ea

Please sign in to comment.