Add error message to options flow if connection fails for nut integration (#46972)

This commit is contained in:
Michael 2021-03-09 01:12:52 +01:00 committed by GitHub
parent 5d7b53603f
commit 8e58c3aa7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import voluptuous as vol
from homeassistant import config_entries, core, exceptions from homeassistant import config_entries, core, exceptions
from homeassistant.const import ( from homeassistant.const import (
CONF_ALIAS, CONF_ALIAS,
CONF_BASE,
CONF_HOST, CONF_HOST,
CONF_PASSWORD, CONF_PASSWORD,
CONF_PORT, CONF_PORT,
@ -211,10 +212,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try: try:
info = await validate_input(self.hass, config) info = await validate_input(self.hass, config)
except CannotConnect: except CannotConnect:
errors["base"] = "cannot_connect" errors[CONF_BASE] = "cannot_connect"
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception") _LOGGER.exception("Unexpected exception")
errors["base"] = "unknown" errors[CONF_BASE] = "unknown"
return info, errors return info, errors
@staticmethod @staticmethod
@ -241,7 +242,17 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
) )
info = await validate_input(self.hass, self.config_entry.data) errors = {}
try:
info = await validate_input(self.hass, self.config_entry.data)
except CannotConnect:
errors[CONF_BASE] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors[CONF_BASE] = "unknown"
if errors:
return self.async_show_form(step_id="abort", errors=errors)
base_schema = _resource_schema_base(info["available_resources"], resources) base_schema = _resource_schema_base(info["available_resources"], resources)
base_schema[ base_schema[
@ -249,10 +260,13 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
] = cv.positive_int ] = cv.positive_int
return self.async_show_form( return self.async_show_form(
step_id="init", step_id="init", data_schema=vol.Schema(base_schema), errors=errors
data_schema=vol.Schema(base_schema),
) )
async def async_step_abort(self, user_input=None):
"""Abort options flow."""
return self.async_create_entry(title="", data=self.config_entry.options)
class CannotConnect(exceptions.HomeAssistantError): class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect.""" """Error to indicate we cannot connect."""

View File

@ -41,6 +41,10 @@
"scan_interval": "Scan Interval (seconds)" "scan_interval": "Scan Interval (seconds)"
} }
} }
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
} }
} }
} }

View File

@ -33,6 +33,10 @@
} }
}, },
"options": { "options": {
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
},
"step": { "step": {
"init": { "init": {
"data": { "data": {