Do not log error messages when discovering Broadlink devices (#49394)

This commit is contained in:
Felipe Martins Diel 2021-04-19 01:12:27 -03:00 committed by GitHub
parent a67a45624d
commit 6a3832484c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 24 deletions

View File

@ -62,19 +62,23 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
unique_id = discovery_info[MAC_ADDRESS].lower().replace(":", "")
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
try:
hello = partial(blk.discover, discover_ip_address=host)
device = (await self.hass.async_add_executor_job(hello))[0]
except IndexError:
return self.async_abort(reason="cannot_connect")
except OSError as err:
if err.errno == errno.ENETUNREACH:
return self.async_abort(reason="cannot_connect")
return self.async_abort(reason="invalid_host")
except Exception as ex: # pylint: disable=broad-except
_LOGGER.error("Failed to connect to the device at %s", host, exc_info=ex)
return self.async_abort(reason="unknown")
supported_types = set.union(*DOMAINS_AND_TYPES.values())
if device.type not in supported_types:
return self.async_abort(reason="not_supported")
await self.async_set_device(device)
return await self.async_step_auth()

View File

@ -900,29 +900,10 @@ async def test_dhcp_unreachable(hass):
assert result["reason"] == "cannot_connect"
async def test_dhcp_connect_einval(hass):
"""Test DHCP discovery flow that fails to connect with EINVAL."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.EINVAL, None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
)
await hass.async_block_till_done()
assert result["type"] == "abort"
assert result["reason"] == "invalid_host"
async def test_dhcp_connect_unknown_error(hass):
"""Test DHCP discovery flow that fails to connect with an unknown error."""
"""Test DHCP discovery flow that fails to connect with an OSError."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(DEVICE_DISCOVERY, side_effect=ValueError("Unknown failure")):
with patch(DEVICE_DISCOVERY, side_effect=OSError()):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -938,6 +919,27 @@ async def test_dhcp_connect_unknown_error(hass):
assert result["reason"] == "unknown"
async def test_dhcp_device_not_supported(hass):
"""Test DHCP discovery flow that fails because the device is not supported."""
await setup.async_setup_component(hass, "persistent_notification", {})
device = get_device("Kitchen")
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: device.host,
MAC_ADDRESS: device_registry.format_mac(device.mac),
},
)
assert result["type"] == "abort"
assert result["reason"] == "not_supported"
async def test_dhcp_already_exists(hass):
"""Test DHCP discovery flow that fails to connect."""
await setup.async_setup_component(hass, "persistent_notification", {})