mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Set Vizio unique ID for discovery flow early and abort if configured to prevent duplicate discovery flows (#42194)
This commit is contained in:
parent
b102ad731f
commit
148a7ff50c
@ -206,29 +206,29 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: Dict[str, Any] = None
|
self, user_input: Dict[str, Any] = None
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
|
assert self.hass
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
# Store current values in case setup fails and user needs to edit
|
# Store current values in case setup fails and user needs to edit
|
||||||
self._user_schema = _get_config_schema(user_input)
|
self._user_schema = _get_config_schema(user_input)
|
||||||
unique_id = await VizioAsync.get_unique_id(
|
if self.unique_id is None:
|
||||||
user_input[CONF_HOST],
|
unique_id = await VizioAsync.get_unique_id(
|
||||||
user_input[CONF_DEVICE_CLASS],
|
user_input[CONF_HOST],
|
||||||
session=async_get_clientsession(self.hass, False),
|
user_input[CONF_DEVICE_CLASS],
|
||||||
)
|
session=async_get_clientsession(self.hass, False),
|
||||||
|
|
||||||
if not unique_id:
|
|
||||||
errors[CONF_HOST] = "cannot_connect"
|
|
||||||
else:
|
|
||||||
# Set unique ID and abort if a flow with the same unique ID is already in progress
|
|
||||||
existing_entry = await self.async_set_unique_id(
|
|
||||||
unique_id=unique_id, raise_on_progress=True
|
|
||||||
)
|
)
|
||||||
# If device was discovered, abort if existing entry found, otherwise display an error
|
|
||||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
# Check if unique ID was found, set unique ID, and abort if a flow with
|
||||||
if self.context["source"] == SOURCE_ZEROCONF:
|
# the same unique ID is already in progress
|
||||||
self._abort_if_unique_id_configured()
|
if not unique_id:
|
||||||
elif existing_entry:
|
errors[CONF_HOST] = "cannot_connect"
|
||||||
|
elif (
|
||||||
|
await self.async_set_unique_id(
|
||||||
|
unique_id=unique_id, raise_on_progress=True
|
||||||
|
)
|
||||||
|
is not None
|
||||||
|
):
|
||||||
errors[CONF_HOST] = "existing_config_entry_found"
|
errors[CONF_HOST] = "existing_config_entry_found"
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
@ -346,6 +346,7 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
self, discovery_info: Optional[DiscoveryInfoType] = None
|
self, discovery_info: Optional[DiscoveryInfoType] = None
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""Handle zeroconf discovery."""
|
"""Handle zeroconf discovery."""
|
||||||
|
assert self.hass
|
||||||
|
|
||||||
discovery_info[
|
discovery_info[
|
||||||
CONF_HOST
|
CONF_HOST
|
||||||
@ -360,6 +361,15 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
discovery_info[CONF_HOST]
|
discovery_info[CONF_HOST]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Set unique ID early for discovery flow so we can abort if needed
|
||||||
|
unique_id = await VizioAsync.get_unique_id(
|
||||||
|
discovery_info[CONF_HOST],
|
||||||
|
discovery_info[CONF_DEVICE_CLASS],
|
||||||
|
session=async_get_clientsession(self.hass, False),
|
||||||
|
)
|
||||||
|
await self.async_set_unique_id(unique_id=unique_id, raise_on_progress=True)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
# Form must be shown after discovery so user can confirm/update configuration
|
# Form must be shown after discovery so user can confirm/update configuration
|
||||||
# before ConfigEntry creation.
|
# before ConfigEntry creation.
|
||||||
self._must_show_form = True
|
self._must_show_form = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user