1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00

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
This commit is contained in:
Tiit Rätsep 2019-12-11 14:27:28 +02:00 committed by Martin Hjelmare
parent 004af97699
commit 01ef44fd68
4 changed files with 41 additions and 9 deletions

View File

@ -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."

View File

@ -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):

View File

@ -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."

View File

@ -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