From 01ef44fd681b4c2bd37dd51bc794289f3a0a4dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiit=20R=C3=A4tsep?= Date: Wed, 11 Dec 2019 14:27:28 +0200 Subject: [PATCH] Fix Soma integration connection issue (#27692) * Added a check for Connect actually returning something before telling the user the setup succeeded * Added handling for KeyError in case API returns empty response, formatting * Trying to please the linter --- .../components/soma/.translations/en.json | 4 +++- homeassistant/components/soma/config_flow.py | 18 ++++++++++---- homeassistant/components/soma/strings.json | 4 +++- tests/components/soma/test_config_flow.py | 24 +++++++++++++++++-- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/soma/.translations/en.json b/homeassistant/components/soma/.translations/en.json index 42e09a8762c..ae2f9900162 100644 --- a/homeassistant/components/soma/.translations/en.json +++ b/homeassistant/components/soma/.translations/en.json @@ -3,7 +3,9 @@ "abort": { "already_setup": "You can only configure one Soma account.", "authorize_url_timeout": "Timeout generating authorize url.", - "missing_configuration": "The Soma component is not configured. Please follow the documentation." + "missing_configuration": "The Soma component is not configured. Please follow the documentation.", + "result_error": "SOMA Connect responded with error status.", + "connection_error": "Failed to connect to SOMA Connect." }, "create_entry": { "default": "Successfully authenticated with Soma." diff --git a/homeassistant/components/soma/config_flow.py b/homeassistant/components/soma/config_flow.py index f46066e23ca..afb5d05b77e 100644 --- a/homeassistant/components/soma/config_flow.py +++ b/homeassistant/components/soma/config_flow.py @@ -40,14 +40,22 @@ class SomaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Finish config flow.""" api = SomaApi(user_input["host"], user_input["port"]) try: - await self.hass.async_add_executor_job(api.list_devices) + result = await self.hass.async_add_executor_job(api.list_devices) _LOGGER.info("Successfully set up Soma Connect") - return self.async_create_entry( - title="Soma Connect", - data={"host": user_input["host"], "port": user_input["port"]}, + if result["result"] == "success": + return self.async_create_entry( + title="Soma Connect", + data={"host": user_input["host"], "port": user_input["port"]}, + ) + _LOGGER.error( + "Connection to SOMA Connect failed (result:%s)", result["result"] ) + return self.async_abort(reason="result_error") except RequestException: - _LOGGER.error("Connection to SOMA Connect failed") + _LOGGER.error("Connection to SOMA Connect failed with RequestException") + return self.async_abort(reason="connection_error") + except KeyError: + _LOGGER.error("Connection to SOMA Connect failed with KeyError") return self.async_abort(reason="connection_error") async def async_step_import(self, user_input=None): diff --git a/homeassistant/components/soma/strings.json b/homeassistant/components/soma/strings.json index aa2f92f0be6..67f1f6b7d46 100644 --- a/homeassistant/components/soma/strings.json +++ b/homeassistant/components/soma/strings.json @@ -3,7 +3,9 @@ "abort": { "already_setup": "You can only configure one Soma account.", "authorize_url_timeout": "Timeout generating authorize url.", - "missing_configuration": "The Soma component is not configured. Please follow the documentation." + "missing_configuration": "The Soma component is not configured. Please follow the documentation.", + "result_error": "SOMA Connect responded with error status.", + "connection_error": "Failed to connect to SOMA Connect." }, "create_entry": { "default": "Successfully authenticated with Soma." diff --git a/tests/components/soma/test_config_flow.py b/tests/components/soma/test_config_flow.py index 15f90516c17..1d00f83a608 100644 --- a/tests/components/soma/test_config_flow.py +++ b/tests/components/soma/test_config_flow.py @@ -35,11 +35,31 @@ async def test_import_create(hass): """Test configuration from YAML.""" flow = config_flow.SomaFlowHandler() flow.hass = hass - with patch.object(SomaApi, "list_devices", return_value={}): + with patch.object(SomaApi, "list_devices", return_value={"result": "success"}): result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY +async def test_error_status(hass): + """Test Connect successfully returning error status.""" + flow = config_flow.SomaFlowHandler() + flow.hass = hass + with patch.object(SomaApi, "list_devices", return_value={"result": "error"}): + result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "result_error" + + +async def test_key_error(hass): + """Test Connect returning empty string.""" + flow = config_flow.SomaFlowHandler() + flow.hass = hass + with patch.object(SomaApi, "list_devices", return_value={}): + result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "connection_error" + + async def test_exception(hass): """Test if RequestException fires when no connection can be made.""" flow = config_flow.SomaFlowHandler() @@ -55,6 +75,6 @@ async def test_full_flow(hass): hass.data[DOMAIN] = {} flow = config_flow.SomaFlowHandler() flow.hass = hass - with patch.object(SomaApi, "list_devices", return_value={}): + with patch.object(SomaApi, "list_devices", return_value={"result": "success"}): result = await flow.async_step_user({"host": MOCK_HOST, "port": MOCK_PORT}) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY