Use broadlink.hello() for direct discovery (#49405)

pull/49052/head
Felipe Martins Diel 2021-04-19 05:16:03 -03:00 committed by GitHub
parent 0f90678e0e
commit e98f27ead6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 56 deletions

View File

@ -64,10 +64,9 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
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]
device = await self.hass.async_add_executor_job(blk.hello, host)
except IndexError:
except NetworkTimeoutError:
return self.async_abort(reason="cannot_connect")
except OSError as err:
@ -91,10 +90,10 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
timeout = user_input.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)
try:
hello = partial(blk.discover, discover_ip_address=host, timeout=timeout)
device = (await self.hass.async_add_executor_job(hello))[0]
hello = partial(blk.hello, host, timeout=timeout)
device = await self.hass.async_add_executor_job(hello)
except IndexError:
except NetworkTimeoutError:
errors["base"] = "cannot_connect"
err_msg = "Device not found"

View File

@ -13,7 +13,7 @@ from homeassistant.helpers import device_registry
from . import get_device
DEVICE_DISCOVERY = "homeassistant.components.broadlink.config_flow.blk.discover"
DEVICE_HELLO = "homeassistant.components.broadlink.config_flow.blk.hello"
DEVICE_FACTORY = "homeassistant.components.broadlink.config_flow.blk.gendevice"
@ -42,7 +42,7 @@ async def test_flow_user_works(hass):
assert result["step_id"] == "user"
assert result["errors"] == {}
with patch(DEVICE_DISCOVERY, return_value=[mock_api]) as mock_discover:
with patch(DEVICE_HELLO, return_value=mock_api) as mock_hello:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -61,7 +61,7 @@ async def test_flow_user_works(hass):
assert result["title"] == device.name
assert result["data"] == device.get_entry_data()
assert mock_discover.call_count == 1
assert mock_hello.call_count == 1
assert mock_api.auth.call_count == 1
@ -73,7 +73,7 @@ async def test_flow_user_already_in_progress(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[device.get_mock_api()]):
with patch(DEVICE_HELLO, return_value=device.get_mock_api()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -83,7 +83,7 @@ async def test_flow_user_already_in_progress(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[device.get_mock_api()]):
with patch(DEVICE_HELLO, return_value=device.get_mock_api()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -110,7 +110,7 @@ async def test_flow_user_mac_already_configured(hass):
device.timeout = 20
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -129,7 +129,7 @@ async def test_flow_user_invalid_ip_address(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.EINVAL, None)):
with patch(DEVICE_HELLO, side_effect=OSError(errno.EINVAL, None)):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "0.0.0.1"},
@ -146,7 +146,7 @@ async def test_flow_user_invalid_hostname(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, side_effect=OSError(socket.EAI_NONAME, None)):
with patch(DEVICE_HELLO, side_effect=OSError(socket.EAI_NONAME, None)):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "pancakemaster.local"},
@ -165,7 +165,7 @@ async def test_flow_user_device_not_found(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[]):
with patch(DEVICE_HELLO, side_effect=blke.NetworkTimeoutError()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -185,7 +185,7 @@ async def test_flow_user_device_not_supported(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -201,7 +201,7 @@ async def test_flow_user_network_unreachable(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.ENETUNREACH, None)):
with patch(DEVICE_HELLO, side_effect=OSError(errno.ENETUNREACH, None)):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "192.168.1.32"},
@ -218,7 +218,7 @@ async def test_flow_user_os_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, side_effect=OSError()):
with patch(DEVICE_HELLO, side_effect=OSError()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "192.168.1.32"},
@ -239,7 +239,7 @@ async def test_flow_auth_authentication_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -260,7 +260,7 @@ async def test_flow_auth_network_timeout(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -281,7 +281,7 @@ async def test_flow_auth_firmware_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -302,7 +302,7 @@ async def test_flow_auth_network_unreachable(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -323,7 +323,7 @@ async def test_flow_auth_os_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host},
@ -344,13 +344,13 @@ async def test_flow_reset_works(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
)
with patch(DEVICE_DISCOVERY, return_value=[device.get_mock_api()]):
with patch(DEVICE_HELLO, return_value=device.get_mock_api()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -376,7 +376,7 @@ async def test_flow_unlock_works(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -415,7 +415,7 @@ async def test_flow_unlock_network_timeout(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -442,7 +442,7 @@ async def test_flow_unlock_firmware_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -469,7 +469,7 @@ async def test_flow_unlock_network_unreachable(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -496,7 +496,7 @@ async def test_flow_unlock_os_error(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -522,7 +522,7 @@ async def test_flow_do_not_unlock(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -550,7 +550,7 @@ async def test_flow_import_works(hass):
device = get_device("Living Room")
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]) as mock_discover:
with patch(DEVICE_HELLO, return_value=mock_api) as mock_hello:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -573,7 +573,7 @@ async def test_flow_import_works(hass):
assert result["data"]["type"] == device.devtype
assert mock_api.auth.call_count == 1
assert mock_discover.call_count == 1
assert mock_hello.call_count == 1
async def test_flow_import_already_in_progress(hass):
@ -581,12 +581,12 @@ async def test_flow_import_already_in_progress(hass):
device = get_device("Living Room")
data = {"host": device.host}
with patch(DEVICE_DISCOVERY, return_value=[device.get_mock_api()]):
with patch(DEVICE_HELLO, return_value=device.get_mock_api()):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=data
)
with patch(DEVICE_DISCOVERY, return_value=[device.get_mock_api()]):
with patch(DEVICE_HELLO, return_value=device.get_mock_api()):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=data
)
@ -602,7 +602,7 @@ async def test_flow_import_host_already_configured(hass):
mock_entry.add_to_hass(hass)
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -625,7 +625,7 @@ async def test_flow_import_mac_already_configured(hass):
device.host = "192.168.1.16"
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -643,7 +643,7 @@ async def test_flow_import_mac_already_configured(hass):
async def test_flow_import_device_not_found(hass):
"""Test we handle a device not found in the import step."""
with patch(DEVICE_DISCOVERY, return_value=[]):
with patch(DEVICE_HELLO, side_effect=blke.NetworkTimeoutError()):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -659,7 +659,7 @@ async def test_flow_import_device_not_supported(hass):
device = get_device("Kitchen")
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -672,7 +672,7 @@ async def test_flow_import_device_not_supported(hass):
async def test_flow_import_invalid_ip_address(hass):
"""Test we handle an invalid IP address in the import step."""
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.EINVAL, None)):
with patch(DEVICE_HELLO, side_effect=OSError(errno.EINVAL, None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -685,7 +685,7 @@ async def test_flow_import_invalid_ip_address(hass):
async def test_flow_import_invalid_hostname(hass):
"""Test we handle an invalid hostname in the import step."""
with patch(DEVICE_DISCOVERY, side_effect=OSError(socket.EAI_NONAME, None)):
with patch(DEVICE_HELLO, side_effect=OSError(socket.EAI_NONAME, None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -698,7 +698,7 @@ async def test_flow_import_invalid_hostname(hass):
async def test_flow_import_network_unreachable(hass):
"""Test we handle a network unreachable in the import step."""
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.ENETUNREACH, None)):
with patch(DEVICE_HELLO, side_effect=OSError(errno.ENETUNREACH, None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -711,7 +711,7 @@ async def test_flow_import_network_unreachable(hass):
async def test_flow_import_os_error(hass):
"""Test we handle an OS error in the import step."""
with patch(DEVICE_DISCOVERY, side_effect=OSError()):
with patch(DEVICE_HELLO, side_effect=OSError()):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@ -741,7 +741,7 @@ async def test_flow_reauth_works(hass):
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]) as mock_discover:
with patch(DEVICE_HELLO, return_value=mock_api) as mock_hello:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -752,7 +752,7 @@ async def test_flow_reauth_works(hass):
assert dict(mock_entry.data) == device.get_entry_data()
assert mock_api.auth.call_count == 1
assert mock_discover.call_count == 1
assert mock_hello.call_count == 1
async def test_flow_reauth_invalid_host(hass):
@ -775,7 +775,7 @@ async def test_flow_reauth_invalid_host(hass):
device.mac = get_device("Office").mac
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]) as mock_discover:
with patch(DEVICE_HELLO, return_value=mock_api) as mock_hello:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -785,7 +785,7 @@ async def test_flow_reauth_invalid_host(hass):
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_host"}
assert mock_discover.call_count == 1
assert mock_hello.call_count == 1
assert mock_api.auth.call_count == 0
@ -809,7 +809,7 @@ async def test_flow_reauth_valid_host(hass):
device.host = "192.168.1.128"
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]) as mock_discover:
with patch(DEVICE_HELLO, return_value=mock_api) as mock_hello:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": device.host, "timeout": device.timeout},
@ -819,7 +819,7 @@ async def test_flow_reauth_valid_host(hass):
assert result["reason"] == "already_configured"
assert mock_entry.data["host"] == device.host
assert mock_discover.call_count == 1
assert mock_hello.call_count == 1
assert mock_api.auth.call_count == 1
@ -831,7 +831,7 @@ async def test_dhcp_can_finish(hass):
device.host = "1.2.3.4"
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -865,7 +865,7 @@ async def test_dhcp_can_finish(hass):
async def test_dhcp_fails_to_connect(hass):
"""Test DHCP discovery flow that fails to connect."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(DEVICE_DISCOVERY, side_effect=IndexError()):
with patch(DEVICE_HELLO, side_effect=blke.NetworkTimeoutError()):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -884,7 +884,7 @@ async def test_dhcp_fails_to_connect(hass):
async def test_dhcp_unreachable(hass):
"""Test DHCP discovery flow that fails to connect."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.ENETUNREACH, None)):
with patch(DEVICE_HELLO, side_effect=OSError(errno.ENETUNREACH, None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -903,7 +903,7 @@ async def test_dhcp_unreachable(hass):
async def test_dhcp_connect_unknown_error(hass):
"""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=OSError()):
with patch(DEVICE_HELLO, side_effect=OSError()):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -925,7 +925,7 @@ async def test_dhcp_device_not_supported(hass):
device = get_device("Kitchen")
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -949,7 +949,7 @@ async def test_dhcp_already_exists(hass):
device.host = "1.2.3.4"
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},
@ -975,7 +975,7 @@ async def test_dhcp_updates_host(hass):
mock_entry.add_to_hass(hass)
mock_api = device.get_mock_api()
with patch(DEVICE_DISCOVERY, return_value=[mock_api]):
with patch(DEVICE_HELLO, return_value=mock_api):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": "dhcp"},