diff --git a/homeassistant/components/ovo_energy/config_flow.py b/homeassistant/components/ovo_energy/config_flow.py index e4d33865f57..ac3e8371123 100644 --- a/homeassistant/components/ovo_energy/config_flow.py +++ b/homeassistant/components/ovo_energy/config_flow.py @@ -9,58 +9,48 @@ from homeassistant import config_entries from homeassistant.config_entries import ConfigFlow from homeassistant.const import CONF_PASSWORD, CONF_USERNAME -from .const import CONF_ACCOUNT_ID, DOMAIN +from .const import CONF_ACCOUNT_ID, DOMAIN # pylint: disable=unused-import _LOGGER = logging.getLogger(__name__) +USER_SCHEMA = vol.Schema( + {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} +) -@config_entries.HANDLERS.register(DOMAIN) -class OVOEnergyFlowHandler(ConfigFlow): + +class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a OVO Energy config flow.""" VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL - def __init__(self): - """Initialize OVO Energy flow.""" - - async def _show_setup_form(self, errors=None): - """Show the setup form to the user.""" - return self.async_show_form( - step_id="user", - data_schema=vol.Schema( - {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} - ), - errors=errors or {}, - ) - async def async_step_user(self, user_input=None): """Handle a flow initiated by the user.""" - if user_input is None: - return await self._show_setup_form() - errors = {} - - client = OVOEnergy() - - try: - if ( - await client.authenticate( - user_input.get(CONF_USERNAME), user_input.get(CONF_PASSWORD) + if user_input is not None: + client = OVOEnergy() + try: + authenticated = await client.authenticate( + user_input[CONF_USERNAME], user_input[CONF_PASSWORD] ) - is not True - ): - errors["base"] = "authorization_error" - return await self._show_setup_form(errors) - except aiohttp.ClientError: - errors["base"] = "connection_error" - return await self._show_setup_form(errors) + except aiohttp.ClientError: + errors["base"] = "connection_error" + else: + if authenticated: + await self.async_set_unique_id(user_input[CONF_USERNAME]) + self._abort_if_unique_id_configured() - return self.async_create_entry( - title=client.account_id, - data={ - CONF_USERNAME: user_input.get(CONF_USERNAME), - CONF_PASSWORD: user_input.get(CONF_PASSWORD), - CONF_ACCOUNT_ID: client.account_id, - }, + return self.async_create_entry( + title=client.account_id, + data={ + CONF_USERNAME: user_input[CONF_USERNAME], + CONF_PASSWORD: user_input[CONF_PASSWORD], + CONF_ACCOUNT_ID: client.account_id, + }, + ) + + errors["base"] = "authorization_error" + + return self.async_show_form( + step_id="user", data_schema=USER_SCHEMA, errors=errors ) diff --git a/homeassistant/components/ovo_energy/manifest.json b/homeassistant/components/ovo_energy/manifest.json index 27a28863405..2da08d3339b 100644 --- a/homeassistant/components/ovo_energy/manifest.json +++ b/homeassistant/components/ovo_energy/manifest.json @@ -4,6 +4,5 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/ovo_energy", "requirements": ["ovoenergy==1.1.6"], - "dependencies": [], "codeowners": ["@timmo001"] } diff --git a/homeassistant/components/ovo_energy/strings.json b/homeassistant/components/ovo_energy/strings.json index a98b0223644..0132f3582b6 100644 --- a/homeassistant/components/ovo_energy/strings.json +++ b/homeassistant/components/ovo_energy/strings.json @@ -1,18 +1,19 @@ { - "config": { - "error": { - "authorization_error": "Authorization error. Check your credentials.", - "connection_error": "Could not connect to OVO Energy." - }, - "step": { - "user": { - "data": { - "username": "[%key:common::config_flow::data::username%]", - "password": "[%key:common::config_flow::data::password%]" + "config": { + "error": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", + "authorization_error": "Authorization error. Check your credentials.", + "connection_error": "[%key:common::config_flow::error::cannot_connect%]" }, - "description": "Set up an OVO Energy instance to access your energy usage.", - "title": "Add OVO Energy" - } + "step": { + "user": { + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + }, + "description": "Set up an OVO Energy instance to access your energy usage.", + "title": "Add OVO Energy Account" + } + } } - } } diff --git a/homeassistant/components/ovo_energy/translations/en.json b/homeassistant/components/ovo_energy/translations/en.json index 64a3bfe80c3..0132f3582b6 100644 --- a/homeassistant/components/ovo_energy/translations/en.json +++ b/homeassistant/components/ovo_energy/translations/en.json @@ -1,18 +1,19 @@ { "config": { "error": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", "authorization_error": "Authorization error. Check your credentials.", - "connection_error": "Could not connect to OVO Energy." + "connection_error": "[%key:common::config_flow::error::cannot_connect%]" }, "step": { "user": { "data": { - "password": "Password", - "username": "Username" + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" }, "description": "Set up an OVO Energy instance to access your energy usage.", - "title": "Add OVO Energy" + "title": "Add OVO Energy Account" } } } -} \ No newline at end of file +}