Improve the OVO Energy integration (#38598)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Aidan Timson 2020-08-06 19:43:42 +01:00 committed by GitHub
parent ae40f87a5c
commit 8fe11fec04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 60 deletions

View File

@ -9,58 +9,48 @@ from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlow from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME 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__) _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.""" """Handle a OVO Energy config flow."""
VERSION = 1 VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL 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): async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user.""" """Handle a flow initiated by the user."""
if user_input is None:
return await self._show_setup_form()
errors = {} errors = {}
if user_input is not None:
client = OVOEnergy() client = OVOEnergy()
try:
try: authenticated = await client.authenticate(
if ( user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
await client.authenticate(
user_input.get(CONF_USERNAME), user_input.get(CONF_PASSWORD)
) )
is not True except aiohttp.ClientError:
): errors["base"] = "connection_error"
errors["base"] = "authorization_error" else:
return await self._show_setup_form(errors) if authenticated:
except aiohttp.ClientError: await self.async_set_unique_id(user_input[CONF_USERNAME])
errors["base"] = "connection_error" self._abort_if_unique_id_configured()
return await self._show_setup_form(errors)
return self.async_create_entry( return self.async_create_entry(
title=client.account_id, title=client.account_id,
data={ data={
CONF_USERNAME: user_input.get(CONF_USERNAME), CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input.get(CONF_PASSWORD), CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_ACCOUNT_ID: client.account_id, CONF_ACCOUNT_ID: client.account_id,
}, },
)
errors["base"] = "authorization_error"
return self.async_show_form(
step_id="user", data_schema=USER_SCHEMA, errors=errors
) )

View File

@ -4,6 +4,5 @@
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/ovo_energy", "documentation": "https://www.home-assistant.io/integrations/ovo_energy",
"requirements": ["ovoenergy==1.1.6"], "requirements": ["ovoenergy==1.1.6"],
"dependencies": [],
"codeowners": ["@timmo001"] "codeowners": ["@timmo001"]
} }

View File

@ -1,18 +1,19 @@
{ {
"config": { "config": {
"error": { "error": {
"authorization_error": "Authorization error. Check your credentials.", "already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"connection_error": "Could not connect to OVO Energy." "authorization_error": "Authorization error. Check your credentials.",
}, "connection_error": "[%key:common::config_flow::error::cannot_connect%]"
"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.", "step": {
"title": "Add OVO Energy" "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"
}
}
} }
}
} }

View File

@ -1,17 +1,18 @@
{ {
"config": { "config": {
"error": { "error": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"authorization_error": "Authorization error. Check your credentials.", "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": { "step": {
"user": { "user": {
"data": { "data": {
"password": "Password", "username": "[%key:common::config_flow::data::username%]",
"username": "Username" "password": "[%key:common::config_flow::data::password%]"
}, },
"description": "Set up an OVO Energy instance to access your energy usage.", "description": "Set up an OVO Energy instance to access your energy usage.",
"title": "Add OVO Energy" "title": "Add OVO Energy Account"
} }
} }
} }