From 7003862bd891e6bceafa416e4a1cefcee4dce1d1 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 21 Apr 2022 15:06:22 +0200 Subject: [PATCH] Fix Plugwise to not use invalid discovery data (#70366) --- .../components/plugwise/config_flow.py | 22 +++++++++++++++-- tests/components/plugwise/test_config_flow.py | 24 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/plugwise/config_flow.py b/homeassistant/components/plugwise/config_flow.py index 5b5c79ba2b8b6..fbfdca00b418a 100644 --- a/homeassistant/components/plugwise/config_flow.py +++ b/homeassistant/components/plugwise/config_flow.py @@ -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 diff --git a/tests/components/plugwise/test_config_flow.py b/tests/components/plugwise/test_config_flow.py index 7668d492e756b..f2a2ef3bc4372 100644 --- a/tests/components/plugwise/test_config_flow.py +++ b/tests/components/plugwise/test_config_flow.py @@ -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},