diff --git a/homeassistant/components/harmony/config_flow.py b/homeassistant/components/harmony/config_flow.py index 576451ef2d6..9142eed2dba 100644 --- a/homeassistant/components/harmony/config_flow.py +++ b/homeassistant/components/harmony/config_flow.py @@ -86,6 +86,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): parsed_url = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]) friendly_name = discovery_info[ssdp.ATTR_UPNP_FRIENDLY_NAME] + if self._host_already_configured(parsed_url.hostname): + return self.async_abort(reason="already_configured") + # pylint: disable=no-member self.context["title_placeholders"] = {"name": friendly_name} @@ -158,6 +161,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return self.async_create_entry(title=validated[CONF_NAME], data=data) + def _host_already_configured(self, host): + """See if we already have a harmony entry matching the host.""" + for entry in self._async_current_entries(): + if entry.data[CONF_HOST] == host: + return True + return False + def _options_from_user_input(user_input): options = {} diff --git a/tests/components/harmony/test_config_flow.py b/tests/components/harmony/test_config_flow.py index 0228983ef9d..d159a0f025f 100644 --- a/tests/components/harmony/test_config_flow.py +++ b/tests/components/harmony/test_config_flow.py @@ -145,6 +145,30 @@ async def test_form_ssdp(hass): assert len(mock_setup_entry.mock_calls) == 1 +async def test_form_ssdp_aborts_before_checking_remoteid_if_host_known(hass): + """Test we abort without connecting if the host is already known.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + config_entry = MockConfigEntry( + domain=DOMAIN, data={"host": "2.2.2.2", "name": "any"}, + ) + config_entry.add_to_hass(hass) + + harmonyapi = _get_mock_harmonyapi(connect=True) + + with patch( + "homeassistant.components.harmony.util.HarmonyAPI", return_value=harmonyapi, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_SSDP}, + data={ + "friendlyName": "Harmony Hub", + "ssdp_location": "http://2.2.2.2:8088/description", + }, + ) + assert result["type"] == "abort" + + async def test_form_cannot_connect(hass): """Test we handle cannot connect error.""" result = await hass.config_entries.flow.async_init(