Skip to content

Commit 63303bd

Browse files
authored
Allow port and SNMP community configuration for Brother printer (home-assistant#151506)
1 parent 59cd24f commit 63303bd

File tree

8 files changed

+238
-28
lines changed

8 files changed

+238
-28
lines changed

homeassistant/components/brother/__init__.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22

33
from __future__ import annotations
44

5+
import logging
6+
57
from brother import Brother, SnmpError
68

79
from homeassistant.components.snmp import async_get_snmp_engine
8-
from homeassistant.const import CONF_HOST, CONF_TYPE, Platform
10+
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE, Platform
911
from homeassistant.core import HomeAssistant
1012
from homeassistant.exceptions import ConfigEntryNotReady
1113

12-
from .const import DOMAIN
14+
from .const import (
15+
CONF_COMMUNITY,
16+
DEFAULT_COMMUNITY,
17+
DEFAULT_PORT,
18+
DOMAIN,
19+
SECTION_ADVANCED_SETTINGS,
20+
)
1321
from .coordinator import BrotherConfigEntry, BrotherDataUpdateCoordinator
1422

23+
_LOGGER = logging.getLogger(__name__)
24+
1525
PLATFORMS = [Platform.SENSOR]
1626

1727

1828
async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
1929
"""Set up Brother from a config entry."""
2030
host = entry.data[CONF_HOST]
31+
port = entry.data[SECTION_ADVANCED_SETTINGS][CONF_PORT]
32+
community = entry.data[SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY]
2133
printer_type = entry.data[CONF_TYPE]
2234

2335
snmp_engine = await async_get_snmp_engine(hass)
2436
try:
2537
brother = await Brother.create(
26-
host, printer_type=printer_type, snmp_engine=snmp_engine
38+
host, port, community, printer_type=printer_type, snmp_engine=snmp_engine
2739
)
2840
except (ConnectionError, SnmpError, TimeoutError) as error:
2941
raise ConfigEntryNotReady(
@@ -48,3 +60,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b
4860
async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
4961
"""Unload a config entry."""
5062
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
63+
64+
65+
async def async_migrate_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
66+
"""Migrate an old entry."""
67+
if entry.version == 1 and entry.minor_version < 2:
68+
new_data = entry.data.copy()
69+
new_data[SECTION_ADVANCED_SETTINGS] = {
70+
CONF_PORT: DEFAULT_PORT,
71+
CONF_COMMUNITY: DEFAULT_COMMUNITY,
72+
}
73+
hass.config_entries.async_update_entry(entry, data=new_data, minor_version=2)
74+
75+
_LOGGER.info(
76+
"Migration to configuration version %s.%s successful",
77+
entry.version,
78+
entry.minor_version,
79+
)
80+
81+
return True

homeassistant/components/brother/config_flow.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,65 @@
99

1010
from homeassistant.components.snmp import async_get_snmp_engine
1111
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12-
from homeassistant.const import CONF_HOST, CONF_TYPE
12+
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
1313
from homeassistant.core import HomeAssistant
14+
from homeassistant.data_entry_flow import section
1415
from homeassistant.exceptions import HomeAssistantError
1516
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
1617
from homeassistant.util.network import is_host_valid
1718

18-
from .const import DOMAIN, PRINTER_TYPES
19+
from .const import (
20+
CONF_COMMUNITY,
21+
DEFAULT_COMMUNITY,
22+
DEFAULT_PORT,
23+
DOMAIN,
24+
PRINTER_TYPES,
25+
SECTION_ADVANCED_SETTINGS,
26+
)
1927

2028
DATA_SCHEMA = vol.Schema(
2129
{
2230
vol.Required(CONF_HOST): str,
2331
vol.Optional(CONF_TYPE, default="laser"): vol.In(PRINTER_TYPES),
32+
vol.Required(SECTION_ADVANCED_SETTINGS): section(
33+
vol.Schema(
34+
{
35+
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
36+
vol.Required(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): str,
37+
},
38+
),
39+
{"collapsed": True},
40+
),
41+
}
42+
)
43+
ZEROCONF_SCHEMA = vol.Schema(
44+
{
45+
vol.Optional(CONF_TYPE, default="laser"): vol.In(PRINTER_TYPES),
46+
vol.Required(SECTION_ADVANCED_SETTINGS): section(
47+
vol.Schema(
48+
{
49+
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
50+
vol.Required(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): str,
51+
},
52+
),
53+
{"collapsed": True},
54+
),
55+
}
56+
)
57+
RECONFIGURE_SCHEMA = vol.Schema(
58+
{
59+
vol.Required(CONF_HOST): str,
60+
vol.Required(SECTION_ADVANCED_SETTINGS): section(
61+
vol.Schema(
62+
{
63+
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
64+
vol.Required(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): str,
65+
},
66+
),
67+
{"collapsed": True},
68+
),
2469
}
2570
)
26-
RECONFIGURE_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
2771

2872

2973
async def validate_input(
@@ -35,7 +79,12 @@ async def validate_input(
3579

3680
snmp_engine = await async_get_snmp_engine(hass)
3781

38-
brother = await Brother.create(user_input[CONF_HOST], snmp_engine=snmp_engine)
82+
brother = await Brother.create(
83+
user_input[CONF_HOST],
84+
user_input[SECTION_ADVANCED_SETTINGS][CONF_PORT],
85+
user_input[SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY],
86+
snmp_engine=snmp_engine,
87+
)
3988
await brother.async_update()
4089

4190
if expected_mac is not None and brother.serial.lower() != expected_mac:
@@ -48,6 +97,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN):
4897
"""Handle a config flow for Brother Printer."""
4998

5099
VERSION = 1
100+
MINOR_VERSION = 2
51101

52102
def __init__(self) -> None:
53103
"""Initialize."""
@@ -126,13 +176,11 @@ async def async_step_zeroconf_confirm(
126176
title = f"{self.brother.model} {self.brother.serial}"
127177
return self.async_create_entry(
128178
title=title,
129-
data={CONF_HOST: self.host, CONF_TYPE: user_input[CONF_TYPE]},
179+
data={CONF_HOST: self.host, **user_input},
130180
)
131181
return self.async_show_form(
132182
step_id="zeroconf_confirm",
133-
data_schema=vol.Schema(
134-
{vol.Optional(CONF_TYPE, default="laser"): vol.In(PRINTER_TYPES)}
135-
),
183+
data_schema=ZEROCONF_SCHEMA,
136184
description_placeholders={
137185
"serial_number": self.brother.serial,
138186
"model": self.brother.model,
@@ -160,7 +208,7 @@ async def async_step_reconfigure(
160208
else:
161209
return self.async_update_reload_and_abort(
162210
entry,
163-
data_updates={CONF_HOST: user_input[CONF_HOST]},
211+
data_updates=user_input,
164212
)
165213

166214
return self.async_show_form(

homeassistant/components/brother/const.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@
1010
PRINTER_TYPES: Final = ["laser", "ink"]
1111

1212
UPDATE_INTERVAL = timedelta(seconds=30)
13+
14+
SECTION_ADVANCED_SETTINGS = "advanced_settings"
15+
16+
CONF_COMMUNITY = "community"
17+
18+
DEFAULT_COMMUNITY = "public"
19+
DEFAULT_PORT = 161

homeassistant/components/brother/strings.json

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,44 @@
88
"type": "Type of the printer"
99
},
1010
"data_description": {
11-
"host": "The hostname or IP address of the Brother printer to control."
11+
"host": "The hostname or IP address of the Brother printer to control.",
12+
"type": "Brother printer type: ink or laser."
13+
},
14+
"sections": {
15+
"advanced_settings": {
16+
"name": "Advanced settings",
17+
"data": {
18+
"port": "[%key:common::config_flow::data::port%]",
19+
"community": "SNMP Community"
20+
},
21+
"data_description": {
22+
"port": "The SNMP port of the Brother printer.",
23+
"community": "A simple password for devices to communicate to each other."
24+
}
25+
}
1226
}
1327
},
1428
"zeroconf_confirm": {
1529
"description": "Do you want to add the printer {model} with serial number `{serial_number}` to Home Assistant?",
1630
"title": "Discovered Brother Printer",
1731
"data": {
1832
"type": "[%key:component::brother::config::step::user::data::type%]"
33+
},
34+
"data_description": {
35+
"type": "[%key:component::brother::config::step::user::data_description::type%]"
36+
},
37+
"sections": {
38+
"advanced_settings": {
39+
"name": "Advanced settings",
40+
"data": {
41+
"port": "[%key:common::config_flow::data::port%]",
42+
"community": "SNMP Community"
43+
},
44+
"data_description": {
45+
"port": "The SNMP port of the Brother printer.",
46+
"community": "A simple password for devices to communicate to each other."
47+
}
48+
}
1949
}
2050
},
2151
"reconfigure": {
@@ -25,6 +55,19 @@
2555
},
2656
"data_description": {
2757
"host": "[%key:component::brother::config::step::user::data_description::host%]"
58+
},
59+
"sections": {
60+
"advanced_settings": {
61+
"name": "Advanced settings",
62+
"data": {
63+
"port": "[%key:common::config_flow::data::port%]",
64+
"community": "SNMP Community"
65+
},
66+
"data_description": {
67+
"port": "The SNMP port of the Brother printer.",
68+
"community": "A simple password for devices to communicate to each other."
69+
}
70+
}
2871
}
2972
}
3073
},

tests/components/brother/conftest.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
from brother import BrotherSensors
88
import pytest
99

10-
from homeassistant.components.brother.const import DOMAIN
11-
from homeassistant.const import CONF_HOST, CONF_TYPE
10+
from homeassistant.components.brother.const import (
11+
CONF_COMMUNITY,
12+
DOMAIN,
13+
SECTION_ADVANCED_SETTINGS,
14+
)
15+
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
1216

1317
from tests.common import MockConfigEntry
1418

@@ -122,5 +126,10 @@ def mock_config_entry() -> MockConfigEntry:
122126
domain=DOMAIN,
123127
title="HL-L2340DW 0123456789",
124128
unique_id="0123456789",
125-
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
129+
data={
130+
CONF_HOST: "localhost",
131+
CONF_TYPE: "laser",
132+
SECTION_ADVANCED_SETTINGS: {CONF_PORT: 161, CONF_COMMUNITY: "public"},
133+
},
134+
minor_version=2,
126135
)

tests/components/brother/snapshots/test_diagnostics.ambr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
}),
6767
'firmware': '1.2.3',
6868
'info': dict({
69+
'advanced_settings': dict({
70+
'community': 'public',
71+
'port': 161,
72+
}),
6973
'host': 'localhost',
7074
'type': 'laser',
7175
}),

0 commit comments

Comments
 (0)