diff --git a/homeassistant/components/otbr/__init__.py b/homeassistant/components/otbr/__init__.py index 8f8810b5f33..8685282acec 100644 --- a/homeassistant/components/otbr/__init__.py +++ b/homeassistant/components/otbr/__init__.py @@ -24,6 +24,9 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Open Thread Border Router component.""" websocket_api.async_setup(hass) + if len(config_entries := hass.config_entries.async_entries(DOMAIN)): + for config_entry in config_entries[1:]: + await hass.config_entries.async_remove(config_entry.entry_id) return True diff --git a/tests/components/otbr/__init__.py b/tests/components/otbr/__init__.py index 1f103884db2..e641f67dfaf 100644 --- a/tests/components/otbr/__init__.py +++ b/tests/components/otbr/__init__.py @@ -1,6 +1,7 @@ """Tests for the Open Thread Border Router integration.""" BASE_URL = "http://core-silabs-multiprotocol:8081" CONFIG_ENTRY_DATA = {"url": "http://core-silabs-multiprotocol:8081"} +CONFIG_ENTRY_DATA_2 = {"url": "http://core-silabs-multiprotocol_2:8081"} DATASET_CH15 = bytes.fromhex( "0E080000000000010000000300000F35060004001FFFE00208F642646DA209B1D00708FDF57B5A" diff --git a/tests/components/otbr/test_init.py b/tests/components/otbr/test_init.py index 990c015244f..4ec99818b28 100644 --- a/tests/components/otbr/test_init.py +++ b/tests/components/otbr/test_init.py @@ -11,10 +11,12 @@ from homeassistant.components import otbr from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import issue_registry as ir +from homeassistant.setup import async_setup_component from . import ( BASE_URL, CONFIG_ENTRY_DATA, + CONFIG_ENTRY_DATA_2, DATASET_CH15, DATASET_CH16, DATASET_INSECURE_NW_KEY, @@ -280,3 +282,32 @@ async def test_get_active_dataset_tlvs_invalid( aioclient_mock.get(f"{BASE_URL}/node/dataset/active", text="unexpected") with pytest.raises(HomeAssistantError): assert await otbr.async_get_active_dataset_tlvs(hass) + + +async def test_remove_extra_entries( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +) -> None: + """Test we remove additional config entries.""" + + config_entry1 = MockConfigEntry( + data=CONFIG_ENTRY_DATA, + domain=otbr.DOMAIN, + options={}, + title="Open Thread Border Router", + ) + config_entry2 = MockConfigEntry( + data=CONFIG_ENTRY_DATA_2, + domain=otbr.DOMAIN, + options={}, + title="Open Thread Border Router", + ) + config_entry1.add_to_hass(hass) + config_entry2.add_to_hass(hass) + assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 2 + with patch( + "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16 + ), patch( + "homeassistant.components.otbr.util.compute_pskc" + ): # Patch to speed up tests + assert await async_setup_component(hass, otbr.DOMAIN, {}) + assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 1