Set Vizio unique ID for discovery flow early and abort if configured to prevent duplicate discovery flows (#42194)

This commit is contained in:
Raman Gupta 2020-10-22 03:20:17 -04:00 committed by GitHub
parent b102ad731f
commit 148a7ff50c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 17 deletions

View File

@ -206,29 +206,29 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, user_input: Dict[str, Any] = None
) -> Dict[str, Any]:
"""Handle a flow initialized by the user."""
assert self.hass
errors = {}
if user_input is not None:
# Store current values in case setup fails and user needs to edit
self._user_schema = _get_config_schema(user_input)
unique_id = await VizioAsync.get_unique_id(
user_input[CONF_HOST],
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 self.unique_id is None:
unique_id = await VizioAsync.get_unique_id(
user_input[CONF_HOST],
user_input[CONF_DEVICE_CLASS],
session=async_get_clientsession(self.hass, False),
)
# If device was discovered, abort if existing entry found, otherwise display an error
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
if self.context["source"] == SOURCE_ZEROCONF:
self._abort_if_unique_id_configured()
elif existing_entry:
# Check if unique ID was found, set unique ID, and abort if a flow with
# the same unique ID is already in progress
if not unique_id:
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"
if not errors:
@ -346,6 +346,7 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, discovery_info: Optional[DiscoveryInfoType] = None
) -> Dict[str, Any]:
"""Handle zeroconf discovery."""
assert self.hass
discovery_info[
CONF_HOST
@ -360,6 +361,15 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
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
# before ConfigEntry creation.
self._must_show_form = True