From 0dcb8ca270ace103af1c6289bb089da33f57c6ca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 16 Oct 2021 11:52:10 -1000 Subject: [PATCH] Avoid probing brother devices that have an existing config entry (#57829) --- homeassistant/components/brother/config_flow.py | 3 +++ tests/components/brother/test_config_flow.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/homeassistant/components/brother/config_flow.py b/homeassistant/components/brother/config_flow.py index 08daf0155a1..73196302207 100644 --- a/homeassistant/components/brother/config_flow.py +++ b/homeassistant/components/brother/config_flow.py @@ -86,6 +86,9 @@ class BrotherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # Hostname is format: brother.local. self.host = discovery_info["hostname"].rstrip(".") + # Do not probe the device if the host is already configured + self._async_abort_entries_match({CONF_HOST: self.host}) + snmp_engine = get_snmp_engine(self.hass) self.brother = Brother(self.host, snmp_engine=snmp_engine) diff --git a/tests/components/brother/test_config_flow.py b/tests/components/brother/test_config_flow.py index c518076615a..3a828c97488 100644 --- a/tests/components/brother/test_config_flow.py +++ b/tests/components/brother/test_config_flow.py @@ -170,6 +170,23 @@ async def test_zeroconf_device_exists_abort(hass): assert result["reason"] == "already_configured" +async def test_zeroconf_no_probe_existing_device(hass): + """Test we do not probe the device is the host is already configured.""" + entry = MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG) + entry.add_to_hass(hass) + with patch("brother.Brother._get_data") as mock_get_data: + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data={"hostname": "localhost", "name": "Brother Printer"}, + ) + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" + assert len(mock_get_data.mock_calls) == 0 + + async def test_zeroconf_confirm_create_entry(hass): """Test zeroconf confirmation and create config entry.""" with patch(