Skip to content

Commit

Permalink
For vizio integration, set unique ID early to prevent multiple zeroco…
Browse files Browse the repository at this point in the history
…nf discovery items for the same device to appear (home-assistant#31686)

* set unique ID early to prevent multiple zeroconf discovery items for the same device to appear

* add test
  • Loading branch information
raman325 authored Feb 10, 2020
1 parent 454e63b commit 284fd46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/vizio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ async def async_step_zeroconf(
) -> Dict[str, Any]:
"""Handle zeroconf discovery."""

# Set unique ID early to prevent device from getting rediscovered multiple times
await self.async_set_unique_id(
unique_id=discovery_info[CONF_HOST].split(":")[0], raise_on_progress=True
)

discovery_info[
CONF_HOST
] = f"{discovery_info[CONF_HOST]}:{discovery_info[CONF_PORT]}"
Expand Down
26 changes: 26 additions & 0 deletions tests/components/vizio/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,29 @@ async def test_zeroconf_flow_already_configured(
# Flow should abort because device is already setup
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_setup"


async def test_zeroconf_dupe_fail(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_bypass_setup: pytest.fixture,
vizio_guess_device_type: pytest.fixture,
) -> None:
"""Test zeroconf config flow when device gets discovered multiple times."""
discovery_info = MOCK_ZEROCONF_SERVICE_INFO.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info
)

# Form should always show even if all required properties are discovered
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"

discovery_info = MOCK_ZEROCONF_SERVICE_INFO.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info
)

# Flow should abort because device is already setup
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_in_progress"

0 comments on commit 284fd46

Please sign in to comment.