Use IP address instead of hostname in Brother integration (#73556)

This commit is contained in:
Maciej Bieniek 2022-06-16 11:43:36 +02:00 committed by GitHub
parent 7731cfd978
commit c2b484e38b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 19 deletions

View File

@ -83,8 +83,7 @@ class BrotherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult: ) -> FlowResult:
"""Handle zeroconf discovery.""" """Handle zeroconf discovery."""
# Hostname is format: brother.local. self.host = discovery_info.host
self.host = discovery_info.hostname.rstrip(".")
# Do not probe the device if the host is already configured # Do not probe the device if the host is already configured
self._async_abort_entries_match({CONF_HOST: self.host}) self._async_abort_entries_match({CONF_HOST: self.host})
@ -102,7 +101,7 @@ class BrotherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# Check if already configured # Check if already configured
await self.async_set_unique_id(self.brother.serial.lower()) await self.async_set_unique_id(self.brother.serial.lower())
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured({CONF_HOST: self.host})
self.context.update( self.context.update(
{ {

View File

@ -12,7 +12,7 @@ from homeassistant.const import CONF_HOST, CONF_TYPE
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
CONFIG = {CONF_HOST: "localhost", CONF_TYPE: "laser"} CONFIG = {CONF_HOST: "127.0.0.1", CONF_TYPE: "laser"}
async def test_show_form(hass): async def test_show_form(hass):
@ -32,13 +32,15 @@ async def test_create_entry_with_hostname(hass):
return_value=json.loads(load_fixture("printer_data.json", "brother")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG DOMAIN,
context={"source": SOURCE_USER},
data={CONF_HOST: "example.local", CONF_TYPE: "laser"},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "HL-L2340DW 0123456789" assert result["title"] == "HL-L2340DW 0123456789"
assert result["data"][CONF_HOST] == CONFIG[CONF_HOST] assert result["data"][CONF_HOST] == "example.local"
assert result["data"][CONF_TYPE] == CONFIG[CONF_TYPE] assert result["data"][CONF_TYPE] == "laser"
async def test_create_entry_with_ipv4_address(hass): async def test_create_entry_with_ipv4_address(hass):
@ -48,9 +50,7 @@ async def test_create_entry_with_ipv4_address(hass):
return_value=json.loads(load_fixture("printer_data.json", "brother")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
context={"source": SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_TYPE: "laser"},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
@ -145,7 +145,7 @@ async def test_zeroconf_snmp_error(hass):
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="mock_host", host="127.0.0.1",
addresses=["mock_host"], addresses=["mock_host"],
hostname="example.local.", hostname="example.local.",
name="Brother Printer", name="Brother Printer",
@ -166,7 +166,7 @@ async def test_zeroconf_unsupported_model(hass):
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="mock_host", host="127.0.0.1",
addresses=["mock_host"], addresses=["mock_host"],
hostname="example.local.", hostname="example.local.",
name="Brother Printer", name="Brother Printer",
@ -187,15 +187,18 @@ async def test_zeroconf_device_exists_abort(hass):
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("printer_data.json", "brother")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG).add_to_hass( entry = MockConfigEntry(
hass domain=DOMAIN,
unique_id="0123456789",
data={CONF_HOST: "example.local", CONF_TYPE: "laser"},
) )
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="mock_host", host="127.0.0.1",
addresses=["mock_host"], addresses=["mock_host"],
hostname="example.local.", hostname="example.local.",
name="Brother Printer", name="Brother Printer",
@ -208,6 +211,9 @@ async def test_zeroconf_device_exists_abort(hass):
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
# Test config entry got updated with latest IP
assert entry.data["host"] == "127.0.0.1"
async def test_zeroconf_no_probe_existing_device(hass): async def test_zeroconf_no_probe_existing_device(hass):
"""Test we do not probe the device is the host is already configured.""" """Test we do not probe the device is the host is already configured."""
@ -218,9 +224,9 @@ async def test_zeroconf_no_probe_existing_device(hass):
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="mock_host", host="127.0.0.1",
addresses=["mock_host"], addresses=["mock_host"],
hostname="localhost", hostname="example.local.",
name="Brother Printer", name="Brother Printer",
port=None, port=None,
properties={}, properties={},
@ -245,7 +251,7 @@ async def test_zeroconf_confirm_create_entry(hass):
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="mock_host", host="127.0.0.1",
addresses=["mock_host"], addresses=["mock_host"],
hostname="example.local.", hostname="example.local.",
name="Brother Printer", name="Brother Printer",
@ -266,5 +272,5 @@ async def test_zeroconf_confirm_create_entry(hass):
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "HL-L2340DW 0123456789" assert result["title"] == "HL-L2340DW 0123456789"
assert result["data"][CONF_HOST] == "example.local" assert result["data"][CONF_HOST] == "127.0.0.1"
assert result["data"][CONF_TYPE] == "laser" assert result["data"][CONF_TYPE] == "laser"