Improve error reporting when switchbot auth fails (#85244)

* Improve error reporting when switchbot auth fails

related issue #85243

* bump

* coverage
pull/85277/head
J. Nick Koston 2023-01-05 10:29:13 -10:00 committed by Paulus Schoutsen
parent 59d6f827c3
commit fa4c250001
7 changed files with 13 additions and 19 deletions

View File

@ -166,6 +166,7 @@ class SwitchbotConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle the SwitchBot API auth step.""" """Handle the SwitchBot API auth step."""
errors = {} errors = {}
assert self._discovered_adv is not None assert self._discovered_adv is not None
description_placeholders = {}
if user_input is not None: if user_input is not None:
try: try:
key_details = await self.hass.async_add_executor_job( key_details = await self.hass.async_add_executor_job(
@ -176,8 +177,10 @@ class SwitchbotConfigFlow(ConfigFlow, domain=DOMAIN):
) )
except SwitchbotAccountConnectionError as ex: except SwitchbotAccountConnectionError as ex:
raise AbortFlow("cannot_connect") from ex raise AbortFlow("cannot_connect") from ex
except SwitchbotAuthenticationError: except SwitchbotAuthenticationError as ex:
_LOGGER.debug("Authentication failed: %s", ex, exc_info=True)
errors = {"base": "auth_failed"} errors = {"base": "auth_failed"}
description_placeholders = {"error_detail": str(ex)}
else: else:
return await self.async_step_lock_key(key_details) return await self.async_step_lock_key(key_details)
@ -195,6 +198,7 @@ class SwitchbotConfigFlow(ConfigFlow, domain=DOMAIN):
), ),
description_placeholders={ description_placeholders={
"name": name_from_discovery(self._discovered_adv), "name": name_from_discovery(self._discovered_adv),
**description_placeholders,
}, },
) )

View File

@ -2,7 +2,7 @@
"domain": "switchbot", "domain": "switchbot",
"name": "SwitchBot", "name": "SwitchBot",
"documentation": "https://www.home-assistant.io/integrations/switchbot", "documentation": "https://www.home-assistant.io/integrations/switchbot",
"requirements": ["PySwitchbot==0.36.1"], "requirements": ["PySwitchbot==0.36.2"],
"config_flow": true, "config_flow": true,
"dependencies": ["bluetooth"], "dependencies": ["bluetooth"],
"codeowners": [ "codeowners": [

View File

@ -40,7 +40,7 @@
}, },
"error": { "error": {
"encryption_key_invalid": "Key ID or Encryption key is invalid", "encryption_key_invalid": "Key ID or Encryption key is invalid",
"auth_failed": "Authentication failed" "auth_failed": "Authentication failed: {error_detail}"
}, },
"abort": { "abort": {
"already_configured_device": "[%key:common::config_flow::abort::already_configured_device%]", "already_configured_device": "[%key:common::config_flow::abort::already_configured_device%]",

View File

@ -8,7 +8,7 @@
"unknown": "Unexpected error" "unknown": "Unexpected error"
}, },
"error": { "error": {
"auth_failed": "Authentication failed", "auth_failed": "Authentication failed: {error_detail}",
"encryption_key_invalid": "Key ID or Encryption key is invalid" "encryption_key_invalid": "Key ID or Encryption key is invalid"
}, },
"flow_title": "{name} ({address})", "flow_title": "{name} ({address})",
@ -47,18 +47,7 @@
"data": { "data": {
"address": "Device address" "address": "Device address"
} }
},
"lock_key": {
"description": "The {name} device requires encryption key, details on how to obtain it can be found in the documentation.",
"data": {
"key_id": "Key ID",
"encryption_key": "Encryption key"
} }
}
},
"error": {
"key_id_invalid": "Key ID or Encryption key is invalid",
"encryption_key_invalid": "Key ID or Encryption key is invalid"
} }
}, },
"options": { "options": {
@ -70,4 +59,4 @@
} }
} }
} }
} }

View File

@ -40,7 +40,7 @@ PyRMVtransport==0.3.3
PySocks==1.7.1 PySocks==1.7.1
# homeassistant.components.switchbot # homeassistant.components.switchbot
PySwitchbot==0.36.1 PySwitchbot==0.36.2
# homeassistant.components.transport_nsw # homeassistant.components.transport_nsw
PyTransportNSW==0.1.1 PyTransportNSW==0.1.1

View File

@ -36,7 +36,7 @@ PyRMVtransport==0.3.3
PySocks==1.7.1 PySocks==1.7.1
# homeassistant.components.switchbot # homeassistant.components.switchbot
PySwitchbot==0.36.1 PySwitchbot==0.36.2
# homeassistant.components.transport_nsw # homeassistant.components.transport_nsw
PyTransportNSW==0.1.1 PyTransportNSW==0.1.1

View File

@ -481,7 +481,7 @@ async def test_user_setup_wolock_auth(hass):
with patch( with patch(
"homeassistant.components.switchbot.config_flow.SwitchbotLock.retrieve_encryption_key", "homeassistant.components.switchbot.config_flow.SwitchbotLock.retrieve_encryption_key",
side_effect=SwitchbotAuthenticationError, side_effect=SwitchbotAuthenticationError("error from api"),
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -494,6 +494,7 @@ async def test_user_setup_wolock_auth(hass):
assert result["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "lock_auth" assert result["step_id"] == "lock_auth"
assert result["errors"] == {"base": "auth_failed"} assert result["errors"] == {"base": "auth_failed"}
assert "error from api" in result["description_placeholders"]["error_detail"]
with patch_async_setup_entry() as mock_setup_entry, patch( with patch_async_setup_entry() as mock_setup_entry, patch(
"homeassistant.components.switchbot.config_flow.SwitchbotLock.verify_encryption_key", "homeassistant.components.switchbot.config_flow.SwitchbotLock.verify_encryption_key",