Skip to content

Commit

Permalink
Fix Plugwise to not use invalid discovery data (home-assistant#70366)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 21, 2022
1 parent 220cb57 commit 7003862
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions homeassistant/components/plugwise/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,26 @@ async def async_step_zeroconf(
_properties = discovery_info.properties

unique_id = discovery_info.hostname.split(".")[0]
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured({CONF_HOST: discovery_info.host})
if config_entry := await self.async_set_unique_id(unique_id):
try:
await validate_gw_input(
self.hass,
{
CONF_HOST: discovery_info.host,
CONF_PORT: discovery_info.port,
CONF_USERNAME: config_entry.data[CONF_USERNAME],
CONF_PASSWORD: config_entry.data[CONF_PASSWORD],
},
)
except Exception: # pylint: disable=broad-except
self._abort_if_unique_id_configured()
else:
self._abort_if_unique_id_configured(
{
CONF_HOST: discovery_info.host,
CONF_PORT: discovery_info.port,
}
)

if DEFAULT_USERNAME not in unique_id:
self._username = STRETCH_USERNAME
Expand Down
24 changes: 22 additions & 2 deletions tests/components/plugwise/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,38 @@ async def test_zeroconf_flow_stretch(
assert len(mock_smile_config_flow.connect.mock_calls) == 1


async def test_zercoconf_discovery_update_configuration(hass: HomeAssistant) -> None:
async def test_zercoconf_discovery_update_configuration(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_smile_config_flow: MagicMock,
) -> None:
"""Test if a discovered device is configured and updated with new host."""
entry = MockConfigEntry(
domain=DOMAIN,
title=CONF_NAME,
data={CONF_HOST: "0.0.0.0", CONF_PASSWORD: TEST_PASSWORD},
data={
CONF_HOST: "0.0.0.0",
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
},
unique_id=TEST_HOSTNAME,
)
entry.add_to_hass(hass)

assert entry.data[CONF_HOST] == "0.0.0.0"

# Test that an invalid discovery doesn't update the entry
mock_smile_config_flow.connect.side_effect = ConnectionFailedError
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={CONF_SOURCE: SOURCE_ZEROCONF},
data=TEST_DISCOVERY,
)
assert result.get("type") == RESULT_TYPE_ABORT
assert result.get("reason") == "already_configured"
assert entry.data[CONF_HOST] == "0.0.0.0"

mock_smile_config_flow.connect.side_effect = None
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={CONF_SOURCE: SOURCE_ZEROCONF},
Expand Down

0 comments on commit 7003862

Please sign in to comment.