Skip to content

Commit

Permalink
Fix options not getting updated when integration failed to initialize (
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Oct 21, 2023
1 parent fb60e4f commit 77225b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
21 changes: 6 additions & 15 deletions custom_components/tplink_deco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ async def async_create_config_data(hass: HomeAssistant, config_entry: ConfigEntr

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Set up this integration using UI."""
_LOGGER.debug("async_setup_entry: Config entry %s", config_entry.entry_id)

if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})

Expand Down Expand Up @@ -194,11 +196,13 @@ async def async_reboot_deco(service: ServiceCall) -> None:
)

config_entry.async_on_unload(config_entry.add_update_listener(update_listener))

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
_LOGGER.debug("async_unload_entry: Config entry %s", config_entry.entry_id)
data = hass.data[DOMAIN][config_entry.entry_id]
deco_coordinator = data.get(COORDINATOR_DECOS_KEY)
clients_coordinator = data.get(COORDINATOR_CLIENTS_KEY)
Expand All @@ -223,27 +227,14 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->

async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Reload config entry."""
_LOGGER.debug("async_reload_entry: Config entry %s", config_entry)
await async_unload_entry(hass, config_entry)
await async_setup_entry(hass, config_entry)


async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Update options."""
if not config_entry.options or config_entry.data == config_entry.options:
_LOGGER.debug(
"update_listener: No changes in options for %s", config_entry.entry_id
)
return

_LOGGER.debug(
"update_listener: Updating options and reloading %s", config_entry.entry_id
)
hass.config_entries.async_update_entry(
entry=config_entry,
title=config_entry.options.get(CONF_HOST),
data=config_entry.options,
options={},
)
_LOGGER.debug("update_listener: Reloading %s", config_entry.entry_id)
await async_reload_entry(hass, config_entry)


Expand Down
22 changes: 19 additions & 3 deletions custom_components/tplink_deco/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adds config flow for TP-Link Deco."""
import asyncio
import logging
from typing import Any

Expand All @@ -24,6 +25,8 @@
from .const import CONF_TIMEOUT_ERROR_RETRIES
from .const import CONF_TIMEOUT_SECONDS
from .const import CONF_VERIFY_SSL
from .const import COORDINATOR_CLIENTS_KEY
from .const import COORDINATOR_DECOS_KEY
from .const import DEFAULT_CONSIDER_HOME
from .const import DEFAULT_DECO_POSTFIX
from .const import DEFAULT_SCAN_INTERVAL
Expand Down Expand Up @@ -51,7 +54,7 @@ def _get_schema(data: dict[str:Any]):
scan_interval = data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
schema.update(
{
vol.Required(CONF_HOST, default=data.get(CONF_HOST, "192.168.0.1")): str,
vol.Required(CONF_HOST, default=data.get(CONF_HOST, "10.0.0.1")): str,
vol.Required(
CONF_SCAN_INTERVAL,
default=scan_interval,
Expand Down Expand Up @@ -112,7 +115,13 @@ def _ensure_user_input_optionals(data: dict[str:Any]) -> None:
async def _async_test_credentials(hass: HomeAssistant, data: dict[str:Any]):
"""Return true if credentials is valid."""
try:
await async_create_and_refresh_coordinators(hass, data, consider_home_seconds=1)
coordinators = await async_create_and_refresh_coordinators(
hass, data, consider_home_seconds=1
)
await asyncio.gather(
coordinators[COORDINATOR_DECOS_KEY].async_shutdown(),
coordinators[COORDINATOR_CLIENTS_KEY].async_shutdown(),
)
return {}
except TimeoutException:
return {"base": "timeout_connect"}
Expand Down Expand Up @@ -194,6 +203,7 @@ class TplinkDecoOptionsFlowHandler(config_entries.OptionsFlow):

def __init__(self, config_entry: ConfigEntry):
"""Initialize HACS options flow."""
self.config_entry = config_entry
self.data = dict(config_entry.data)
self._errors = {}

Expand All @@ -207,7 +217,13 @@ async def async_step_init(self, user_input: dict[str:Any] = None):

self._errors = await _async_test_credentials(self.hass, self.data)
if len(self._errors) == 0:
return self.async_create_entry(data=self.data)
self.hass.config_entries.async_update_entry(
entry=self.config_entry,
title=self.data[CONF_HOST],
data=self.data,
options={},
)
return self.async_create_entry(data={})

return self.async_show_form(
step_id="init",
Expand Down

0 comments on commit 77225b2

Please sign in to comment.