diff --git a/homeassistant/components/brother/__init__.py b/homeassistant/components/brother/__init__.py index e732438bc03..f969ee7b17a 100644 --- a/homeassistant/components/brother/__init__.py +++ b/homeassistant/components/brother/__init__.py @@ -9,7 +9,7 @@ from brother import Brother, SnmpError from homeassistant.components.snmp import async_get_snmp_engine from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady from .const import ( CONF_COMMUNITY, @@ -50,6 +50,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b coordinator = BrotherDataUpdateCoordinator(hass, entry, brother) await coordinator.async_config_entry_first_refresh() + if brother.serial.lower() != entry.unique_id: + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="serial_mismatch", + translation_placeholders={ + "device": entry.title, + }, + ) + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/brother/strings.json b/homeassistant/components/brother/strings.json index f10a6d8c9c9..f52875018c1 100644 --- a/homeassistant/components/brother/strings.json +++ b/homeassistant/components/brother/strings.json @@ -207,6 +207,9 @@ "cannot_connect": { "message": "An error occurred while connecting to the {device} printer: {error}" }, + "serial_mismatch": { + "message": "The serial number for {device} doesn't match the one in the configuration. It's possible that the two Brother printers have swapped IP addresses. Restore the previous IP address configuration or reconfigure the devices with Home Assistant." + }, "update_error": { "message": "An error occurred while retrieving data from the {device} printer: {error}" } diff --git a/tests/components/brother/test_init.py b/tests/components/brother/test_init.py index d76afa3e6f6..f3eead2f693 100644 --- a/tests/components/brother/test_init.py +++ b/tests/components/brother/test_init.py @@ -98,3 +98,17 @@ async def test_migrate_entry( assert config_entry.minor_version == 2 assert config_entry.data[SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161 assert config_entry.data[SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public" + + +async def test_serial_mismatch( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_brother: AsyncMock, + mock_brother_client: AsyncMock, +) -> None: + """Test if the serial number matches on init.""" + mock_brother_client.serial = "DIFFERENT_SERIAL" + + await init_integration(hass, mock_config_entry) + + assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR