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.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
)

View file

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

View file

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

View file

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