Skip to content

Commit

Permalink
reauth entry implementation
Browse files Browse the repository at this point in the history
Based on this doc [1], when a token has expired, the Hilo integration
will now fall in "Reconfigure" mode.

User will be able to reconfigure only the username and password to fetch
a new token.

Fixes #195

[1] https://developers.home-assistant.io/docs/config_entries_config_flow_handler/#reauthentication
  • Loading branch information
valleedelisle committed Mar 6, 2023
1 parent f2cd96e commit e4c7afe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
3 changes: 1 addition & 2 deletions custom_components/hilo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async def async_setup_entry( # noqa: C901
log_traces=log_traces,
state_yaml=state_yaml,
)
except InvalidCredentialsError as err:
except (KeyError, InvalidCredentialsError) as err:
raise ConfigEntryAuthFailed from err
except HiloError as err:
LOG.error("Config entry failed: %s", err)
Expand Down Expand Up @@ -231,7 +231,6 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry, api: API) -> None:
self.generate_energy_meters = entry.options.get(
CONF_GENERATE_ENERGY_METERS, DEFAULT_GENERATE_ENERGY_METERS
)

# This will get filled in by async_init:
self.coordinator: DataUpdateCoordinator | None = None
self.unknown_tracker_device: HiloDevice | None = None
Expand Down
39 changes: 30 additions & 9 deletions custom_components/hilo/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class HiloFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a Hilo config flow."""

VERSION = 1
reauth_entry: ConfigEntry | None = None

def __init__(self) -> None:
"""Initialize the config flow."""
Expand All @@ -101,14 +102,37 @@ def async_get_options_flow(

async def async_step_reauth(self, config: ConfigType) -> FlowResult:
"""Handle configuration by re-auth."""
self._username = config.get(CONF_USERNAME)
self._reauth = True
return await self.async_step_user()
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm()
#self._username = config.get(CONF_USERNAME)
#self._reauth = True
#return await self.async_step_user()

async def async_step_reauth_confirm(self, user_input=None):
"""Dialog that informs the user that reauth is required."""
if user_input is None:
return self._async_show_form(
step_id="reauth_confirm",
)
return await self.async_step_user(user_input)

async def async_oauth_create_entry(self, data: dict) -> dict:
"""Create an oauth config entry or update existing entry for reauth."""
if self.reauth_entry:
self.hass.config_entries.async_update_entry(self.reauth_entry, data=data)
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")
await self.async_set_unique_id(data['username'])
self._abort_if_unique_id_configured()
LOG.debug(f"Creating entry: {data}")
return self.async_create_entry(title=data['username'], data=data)

def _async_show_form(self, *, errors: dict[str, Any] | None = None) -> FlowResult:
def _async_show_form(self, *, step_id: str = "user", errors: dict[str, Any] | None = None) -> FlowResult:
"""Show the form."""
return self.async_show_form(
step_id="user",
step_id=step_id,
data_schema=STEP_USER_SCHEMA,
errors=errors or {},
)
Expand Down Expand Up @@ -138,11 +162,8 @@ async def async_step_user(
return self._async_show_form(errors=errors)

data = {CONF_USERNAME: hilo._username, CONF_TOKEN: hilo._refresh_token}
return await self.async_oauth_create_entry(data)

await self.async_set_unique_id(hilo._username)
self._abort_if_unique_id_configured()
LOG.debug(f"Creating entry: {data}")
return self.async_create_entry(title=hilo._username, data=data)


class HiloOptionsFlowHandler(config_entries.OptionsFlow):
Expand Down

0 comments on commit e4c7afe

Please sign in to comment.