Skip to content

Commit

Permalink
Merge pull request #69 from dmamontov/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dmamontov authored May 17, 2022
2 parents 4af3279 + 128a2af commit 957cd78
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
48 changes: 38 additions & 10 deletions custom_components/miwifi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
DEFAULT_TIMEOUT,
DOMAIN,
OPTION_IS_FROM_FLOW,
UPDATER,
)
from .discovery import async_start_discovery
from .enum import EncryptionAlgorithm
from .helper import async_user_documentation_url, async_verify_access, get_config_value
from .updater import async_get_updater, LuciUpdater

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -259,6 +259,7 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
"""

errors: dict[str, str] = {}

if user_input is not None:
code: codes = await async_verify_access(
self.hass,
Expand All @@ -271,6 +272,8 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
_LOGGER.debug("Verify access code: %s", code)

if codes.is_success(code):
await self.async_update_unique_id(user_input[CONF_IP_ADDRESS])

return self.async_create_entry(
title=user_input[CONF_IP_ADDRESS], data=user_input
)
Expand All @@ -286,6 +289,26 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
step_id="init", data_schema=self._get_options_schema(), errors=errors
)

async def async_update_unique_id(self, unique_id: str) -> None: # pragma: no cover
"""Async update unique_id
:param unique_id:
"""

if self._config_entry.unique_id == unique_id:
return

for flow in self.hass.config_entries.flow.async_progress(True):
if (
flow["flow_id"] != self.flow_id
and flow["context"].get("unique_id") == unique_id
):
self.hass.config_entries.flow.async_abort(flow["flow_id"])

self.hass.config_entries.async_update_entry(
self._config_entry, unique_id=unique_id
)

def _get_options_schema(self) -> vol.Schema:
"""Options schema.
Expand Down Expand Up @@ -340,19 +363,24 @@ def _get_options_schema(self) -> vol.Schema:
): vol.All(vol.Coerce(int), vol.Range(min=10)),
}

if (
DOMAIN not in self.hass.data
or self._config_entry.entry_id not in self.hass.data[DOMAIN]
or UPDATER not in self.hass.data[DOMAIN][self._config_entry.entry_id]
or self.hass.data[DOMAIN][self._config_entry.entry_id][UPDATER].is_repeater
):
schema |= {
try:
updater: LuciUpdater = async_get_updater(
self.hass, self._config_entry.entry_id
)

if not updater.is_repeater: # pragma: no cover
return vol.Schema(schema)
except ValueError:
pass

return vol.Schema(
schema
| {
vol.Optional(
CONF_IS_FORCE_LOAD,
default=get_config_value(
self._config_entry, CONF_IS_FORCE_LOAD, False
),
): cv.boolean,
}

return vol.Schema(schema)
)
7 changes: 6 additions & 1 deletion custom_components/miwifi/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,11 @@ def async_get_updater(hass: HomeAssistant, identifier: str) -> LuciUpdater:
:return LuciUpdater
"""

_error: str = f"Integration with identifier: {identifier} not found."

if DOMAIN not in hass.data:
raise ValueError(_error)

if identifier in hass.data[DOMAIN] and UPDATER in hass.data[DOMAIN][identifier]:
return hass.data[DOMAIN][identifier][UPDATER]

Expand All @@ -1279,6 +1284,6 @@ def async_get_updater(hass: HomeAssistant, identifier: str) -> LuciUpdater:
]

if len(integrations) == 0:
raise ValueError(f"Integration with identifier: {identifier} not found.")
raise ValueError(_error)

return integrations[0]

0 comments on commit 957cd78

Please sign in to comment.