From 9d1d60ee9119a63cfd13baf110e84d4bdff0d210 Mon Sep 17 00:00:00 2001 From: abmantis Date: Sat, 7 Oct 2023 01:56:11 +0000 Subject: [PATCH] Improve Ikea Idasen config flow error messages --- .../components/idasen_desk/config_flow.py | 7 ++- .../components/idasen_desk/manifest.json | 2 +- .../components/idasen_desk/strings.json | 3 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../idasen_desk/test_config_flow.py | 55 ++++++++++++++++++- 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/idasen_desk/config_flow.py b/homeassistant/components/idasen_desk/config_flow.py index f56446396d2c5..92f5a8367519b 100644 --- a/homeassistant/components/idasen_desk/config_flow.py +++ b/homeassistant/components/idasen_desk/config_flow.py @@ -4,9 +4,9 @@ import logging from typing import Any -from bleak import BleakError +from bleak.exc import BleakError from bluetooth_data_tools import human_readable_name -from idasen_ha import Desk +from idasen_ha import AuthFailedError, Desk import voluptuous as vol from homeassistant import config_entries @@ -64,6 +64,9 @@ async def async_step_user( desk = Desk(None) try: await desk.connect(discovery_info.device, monitor_height=False) + except AuthFailedError as err: + _LOGGER.exception("AuthFailedError", exc_info=err) + errors["base"] = "auth_failed" except TimeoutError as err: _LOGGER.exception("TimeoutError", exc_info=err) errors["base"] = "cannot_connect" diff --git a/homeassistant/components/idasen_desk/manifest.json b/homeassistant/components/idasen_desk/manifest.json index f77e0c2237339..cdb06cf907d0d 100644 --- a/homeassistant/components/idasen_desk/manifest.json +++ b/homeassistant/components/idasen_desk/manifest.json @@ -11,5 +11,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/idasen_desk", "iot_class": "local_push", - "requirements": ["idasen-ha==1.4"] + "requirements": ["idasen-ha==1.4.1"] } diff --git a/homeassistant/components/idasen_desk/strings.json b/homeassistant/components/idasen_desk/strings.json index f7459906ac8da..6b9bf80edfc16 100644 --- a/homeassistant/components/idasen_desk/strings.json +++ b/homeassistant/components/idasen_desk/strings.json @@ -9,7 +9,8 @@ } }, "error": { - "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "auth_failed": "Unable to authenticate with the desk. This is usually solved by using an ESPHome Bluetooth Proxy. Please check the integration documentation for alternative workarounds.", + "cannot_connect": "Cannot connect. Make sure that the desk is in Bluetooth pairing mode. If not already, you can also use an ESPHome Bluetooth Proxy, as it provides a better connection.", "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { diff --git a/requirements_all.txt b/requirements_all.txt index ef817124ad837..2541b403469a1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1042,7 +1042,7 @@ ical==5.0.1 icmplib==3.0 # homeassistant.components.idasen_desk -idasen-ha==1.4 +idasen-ha==1.4.1 # homeassistant.components.network ifaddr==0.2.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5803986ff9d6b..88ecf51937147 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -822,7 +822,7 @@ ical==5.0.1 icmplib==3.0 # homeassistant.components.idasen_desk -idasen-ha==1.4 +idasen-ha==1.4.1 # homeassistant.components.network ifaddr==0.2.0 diff --git a/tests/components/idasen_desk/test_config_flow.py b/tests/components/idasen_desk/test_config_flow.py index 8635e5bfddcdb..223ecc55e28ad 100644 --- a/tests/components/idasen_desk/test_config_flow.py +++ b/tests/components/idasen_desk/test_config_flow.py @@ -2,6 +2,7 @@ from unittest.mock import patch from bleak import BleakError +from idasen_ha import AuthFailedError import pytest from homeassistant import config_entries @@ -89,7 +90,7 @@ async def test_user_step_no_new_devices_found(hass: HomeAssistant) -> None: async def test_user_step_cannot_connect( hass: HomeAssistant, exception: Exception ) -> None: - """Test user step and we cannot connect.""" + """Test user step with a cannot connect error.""" with patch( "homeassistant.components.idasen_desk.config_flow.async_discovered_service_info", return_value=[IDASEN_DISCOVERY_INFO], @@ -140,6 +141,58 @@ async def test_user_step_cannot_connect( assert len(mock_setup_entry.mock_calls) == 1 +async def test_user_step_auth_failed(hass: HomeAssistant) -> None: + """Test user step with an auth failed error.""" + with patch( + "homeassistant.components.idasen_desk.config_flow.async_discovered_service_info", + return_value=[IDASEN_DISCOVERY_INFO], + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + with patch( + "homeassistant.components.idasen_desk.config_flow.Desk.connect", + side_effect=AuthFailedError, + ), patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address, + }, + ) + await hass.async_block_till_done() + + assert result2["type"] == FlowResultType.FORM + assert result2["step_id"] == "user" + assert result2["errors"] == {"base": "auth_failed"} + + with patch("homeassistant.components.idasen_desk.config_flow.Desk.connect"), patch( + "homeassistant.components.idasen_desk.config_flow.Desk.disconnect" + ), patch( + "homeassistant.components.idasen_desk.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + result3 = await hass.config_entries.flow.async_configure( + result2["flow_id"], + { + CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address, + }, + ) + await hass.async_block_till_done() + + assert result3["type"] == FlowResultType.CREATE_ENTRY + assert result3["title"] == IDASEN_DISCOVERY_INFO.name + assert result3["data"] == { + CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address, + } + assert result3["result"].unique_id == IDASEN_DISCOVERY_INFO.address + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_user_step_unknown_exception(hass: HomeAssistant) -> None: """Test user step with an unknown exception.""" with patch(