Fix Plugwise to not use invalid discovery data (#70366)

This commit is contained in:
Franck Nijhof 2022-04-21 15:06:22 +02:00 committed by GitHub
parent 220cb57add
commit 7003862bd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 4 deletions

View File

@ -92,8 +92,26 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN):
_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

View File

@ -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},