Fix homekit_controller pairing retry when the first attempt is busy (#38605)

* Fix homekit_controller pairing retry

If the device was busy on the first pairing attempt, it
was not possible to retry.

* always restart pairing on recoverable execptions

* move code

* malformed pin is safe to restart

* make busy_error an abort

* switch max retries, simplify tests

* try pairing later

* try pairing later

* merge

* s/tlv_error/protocol_error/g

* Adjust wording
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-08-09 22:17:13 -05:00
1 parent 4e56339ba1
commit 9bb7b3b125
4 files changed
+137 -63

No files matched your search

@@ -14,8 +14,6 @@ from tests.async_mock import patch
from tests.common import MockConfigEntry
PAIRING_START_FORM_ERRORS = [
(aiohomekit.BusyError, "busy_error"),
(aiohomekit.MaxTriesError, "max_tries_error"),
(KeyError, "pairing_failed"),
]
@@ -24,6 +22,12 @@ PAIRING_START_ABORT_ERRORS = [
(aiohomekit.UnavailableError, "already_paired"),
]
PAIRING_TRY_LATER_ERRORS = [
(aiohomekit.BusyError, "busy_error"),
(aiohomekit.MaxTriesError, "max_tries_error"),
(IndexError, "protocol_error"),
]
PAIRING_FINISH_FORM_ERRORS = [
(aiohomekit.exceptions.MalformedPinError, "authentication_error"),
(aiohomekit.MaxPeersError, "max_peers_error"),
@@ -314,6 +318,39 @@ async def test_pair_abort_errors_on_start(hass, controller, exception, expected)
assert result["reason"] == expected
@pytest.mark.parametrize("exception,expected", PAIRING_TRY_LATER_ERRORS)
async def test_pair_try_later_errors_on_start(hass, controller, exception, expected):
"""Test various pairing errors."""
device = setup_mock_accessory(controller)
discovery_info = get_device_discovery_info(device)
# Device is discovered
result = await hass.config_entries.flow.async_init(
"homekit_controller", context={"source": "zeroconf"}, data=discovery_info
)
# User initiates pairing - device refuses to enter pairing mode but may be successful after entering pairing mode or rebooting
test_exc = exception("error")
with patch.object(device, "start_pairing", side_effect=test_exc):
result2 = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result2["step_id"] == "try_pair_later"
assert result2["type"] == "form"
assert result2["errors"]["base"] == expected
# Device is rebooted or placed into pairing mode as they have been instructed
# We start pairing again
result3 = await hass.config_entries.flow.async_configure(result2["flow_id"])
# .. and successfully complete pair
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], user_input={"pairing_code": "111-22-333"}
)
assert result4["type"] == "create_entry"
assert result4["title"] == "Koogeek-LS1-20833F"
@pytest.mark.parametrize("exception,expected", PAIRING_START_FORM_ERRORS)
async def test_pair_form_errors_on_start(hass, controller, exception, expected):
"""Test various pairing errors."""
@@ -347,6 +384,13 @@ async def test_pair_form_errors_on_start(hass, controller, exception, expected):
"source": "zeroconf",
}
# User re-tries entering pairing code
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"pairing_code": "111-22-333"}
)
assert result["type"] == "create_entry"
assert result["title"] == "Koogeek-LS1-20833F"
@pytest.mark.parametrize("exception,expected", PAIRING_FINISH_ABORT_ERRORS)
async def test_pair_abort_errors_on_finish(hass, controller, exception, expected):