Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize OptionsFlow (#2748) #2791

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions custom_components/alexa_media/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@
hide_email,
obfuscate,
)
from awesomeversion import AwesomeVersion
from homeassistant import config_entries
from homeassistant.components.http.view import HomeAssistantView
from homeassistant.components.persistent_notification import (
async_dismiss as async_dismiss_persistent_notification,
)
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_URL
from homeassistant.const import (
CONF_EMAIL,
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_URL,
__version__ as HAVERSION,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult, UnknownFlow
from homeassistant.exceptions import Unauthorized
Expand Down Expand Up @@ -871,7 +878,8 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config = OrderedDict()
self._config_entry = config_entry
if AwesomeVersion(HAVERSION) < "2024.12":
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
Expand All @@ -883,7 +891,7 @@ async def async_step_init(
(
vol.Optional(
CONF_PUBLIC_URL,
default=self._config_entry.data.get(
default=self.config_entry.data.get(
CONF_PUBLIC_URL, DEFAULT_PUBLIC_URL
),
),
Expand All @@ -892,28 +900,28 @@ async def async_step_init(
(
vol.Optional(
CONF_INCLUDE_DEVICES,
default=self._config_entry.data.get(CONF_INCLUDE_DEVICES, ""),
default=self.config_entry.data.get(CONF_INCLUDE_DEVICES, ""),
),
str,
),
(
vol.Optional(
CONF_EXCLUDE_DEVICES,
default=self._config_entry.data.get(CONF_EXCLUDE_DEVICES, ""),
default=self.config_entry.data.get(CONF_EXCLUDE_DEVICES, ""),
),
str,
),
(
vol.Optional(
CONF_SCAN_INTERVAL,
default=self._config_entry.data.get(CONF_SCAN_INTERVAL, 120),
default=self.config_entry.data.get(CONF_SCAN_INTERVAL, 120),
),
int,
),
(
vol.Optional(
CONF_QUEUE_DELAY,
default=self._config_entry.data.get(
default=self.config_entry.data.get(
CONF_QUEUE_DELAY, DEFAULT_QUEUE_DELAY
),
),
Expand All @@ -922,7 +930,7 @@ async def async_step_init(
(
vol.Optional(
CONF_EXTENDED_ENTITY_DISCOVERY,
default=self._config_entry.data.get(
default=self.config_entry.data.get(
CONF_EXTENDED_ENTITY_DISCOVERY,
DEFAULT_EXTENDED_ENTITY_DISCOVERY,
),
Expand All @@ -932,7 +940,7 @@ async def async_step_init(
(
vol.Optional(
CONF_DEBUG,
default=self._config_entry.data.get(CONF_DEBUG, DEFAULT_DEBUG),
default=self.config_entry.data.get(CONF_DEBUG, DEFAULT_DEBUG),
),
bool,
),
Expand All @@ -941,36 +949,36 @@ async def async_step_init(

if user_input is not None:
"""Preserve these parameters"""
if CONF_URL in self._config_entry.data:
user_input[CONF_URL] = self._config_entry.data[CONF_URL]
if CONF_EMAIL in self._config_entry.data:
user_input[CONF_EMAIL] = self._config_entry.data[CONF_EMAIL]
if CONF_PASSWORD in self._config_entry.data:
user_input[CONF_PASSWORD] = self._config_entry.data[CONF_PASSWORD]
if CONF_SECURITYCODE in self._config_entry.data:
user_input[CONF_SECURITYCODE] = self._config_entry.data[
if CONF_URL in self.config_entry.data:
user_input[CONF_URL] = self.config_entry.data[CONF_URL]
if CONF_EMAIL in self.config_entry.data:
user_input[CONF_EMAIL] = self.config_entry.data[CONF_EMAIL]
if CONF_PASSWORD in self.config_entry.data:
user_input[CONF_PASSWORD] = self.config_entry.data[CONF_PASSWORD]
if CONF_SECURITYCODE in self.config_entry.data:
user_input[CONF_SECURITYCODE] = self.config_entry.data[
CONF_SECURITYCODE
]
if CONF_OTPSECRET in self._config_entry.data:
user_input[CONF_OTPSECRET] = self._config_entry.data[CONF_OTPSECRET]
if CONF_OAUTH in self._config_entry.data:
user_input[CONF_OAUTH] = self._config_entry.data[CONF_OAUTH]
if CONF_OTPSECRET in self.config_entry.data:
user_input[CONF_OTPSECRET] = self.config_entry.data[CONF_OTPSECRET]
if CONF_OAUTH in self.config_entry.data:
user_input[CONF_OAUTH] = self.config_entry.data[CONF_OAUTH]
"""Ensure public_url ends with trailing slash"""
if CONF_PUBLIC_URL in self._config_entry.data:
if CONF_PUBLIC_URL in self.config_entry.data:
if not user_input[CONF_PUBLIC_URL].endswith("/"):
user_input[CONF_PUBLIC_URL] = user_input[CONF_PUBLIC_URL] + "/"
"""Remove leading/trailing spaces in device strings"""
if CONF_INCLUDE_DEVICES in self._config_entry.data:
if CONF_INCLUDE_DEVICES in self.config_entry.data:
user_input[CONF_INCLUDE_DEVICES] = user_input[
CONF_INCLUDE_DEVICES
].strip()
if CONF_EXCLUDE_DEVICES in self._config_entry.data:
if CONF_EXCLUDE_DEVICES in self.config_entry.data:
user_input[CONF_EXCLUDE_DEVICES] = user_input[
CONF_EXCLUDE_DEVICES
].strip()

self.hass.config_entries.async_update_entry(
self._config_entry, data=user_input, options=self._config_entry.options
self.config_entry, data=user_input, options=self.config_entry.options
)
return self.async_create_entry(title="", data={})

Expand Down
Loading