Skip to content

Commit 9f595a9

Browse files
authored
Check if the Brother printer serial number matches (#155842)
1 parent 5dc215a commit 9f595a9

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

homeassistant/components/brother/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from homeassistant.components.snmp import async_get_snmp_engine
1010
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE, Platform
1111
from homeassistant.core import HomeAssistant
12-
from homeassistant.exceptions import ConfigEntryNotReady
12+
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
1313

1414
from .const import (
1515
CONF_COMMUNITY,
@@ -50,6 +50,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b
5050
coordinator = BrotherDataUpdateCoordinator(hass, entry, brother)
5151
await coordinator.async_config_entry_first_refresh()
5252

53+
if brother.serial.lower() != entry.unique_id:
54+
raise ConfigEntryError(
55+
translation_domain=DOMAIN,
56+
translation_key="serial_mismatch",
57+
translation_placeholders={
58+
"device": entry.title,
59+
},
60+
)
61+
5362
entry.runtime_data = coordinator
5463

5564
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

homeassistant/components/brother/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@
207207
"cannot_connect": {
208208
"message": "An error occurred while connecting to the {device} printer: {error}"
209209
},
210+
"serial_mismatch": {
211+
"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."
212+
},
210213
"update_error": {
211214
"message": "An error occurred while retrieving data from the {device} printer: {error}"
212215
}

tests/components/brother/test_init.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ async def test_migrate_entry(
9898
assert config_entry.minor_version == 2
9999
assert config_entry.data[SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161
100100
assert config_entry.data[SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public"
101+
102+
103+
async def test_serial_mismatch(
104+
hass: HomeAssistant,
105+
mock_config_entry: MockConfigEntry,
106+
mock_brother: AsyncMock,
107+
mock_brother_client: AsyncMock,
108+
) -> None:
109+
"""Test if the serial number matches on init."""
110+
mock_brother_client.serial = "DIFFERENT_SERIAL"
111+
112+
await init_integration(hass, mock_config_entry)
113+
114+
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR

0 commit comments

Comments
 (0)