Add optional support for users with multiple ovo accounts (#80901)
* Added optional support for users with multiple ovo accounts * Made changes from code review around naming * Renaming config variable * Removing account from async_step_reauth * Removing more account from async_step_reauth * Fixing code now I better understand and can test async_step_reauth * Storing account id returned by ovoenergy client in config to handle when user has single account but then another gets added * Putting account into async_step_userpull/81188/head
parent
591d13bc49
commit
74e476a070
|
@ -21,7 +21,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
UpdateFailed,
|
UpdateFailed,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
from .const import CONF_ACCOUNT, DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -35,7 +35,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
authenticated = await client.authenticate(
|
authenticated = await client.authenticate(
|
||||||
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
|
entry.data[CONF_USERNAME],
|
||||||
|
entry.data[CONF_PASSWORD],
|
||||||
|
entry.data[CONF_ACCOUNT],
|
||||||
)
|
)
|
||||||
except aiohttp.ClientError as exception:
|
except aiohttp.ClientError as exception:
|
||||||
_LOGGER.warning(exception)
|
_LOGGER.warning(exception)
|
||||||
|
@ -49,7 +51,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(10):
|
||||||
try:
|
try:
|
||||||
authenticated = await client.authenticate(
|
authenticated = await client.authenticate(
|
||||||
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
|
entry.data[CONF_USERNAME],
|
||||||
|
entry.data[CONF_PASSWORD],
|
||||||
|
entry.data[CONF_ACCOUNT],
|
||||||
)
|
)
|
||||||
except aiohttp.ClientError as exception:
|
except aiohttp.ClientError as exception:
|
||||||
raise UpdateFailed(exception) from exception
|
raise UpdateFailed(exception) from exception
|
||||||
|
|
|
@ -6,11 +6,15 @@ import voluptuous as vol
|
||||||
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 DOMAIN
|
from .const import CONF_ACCOUNT, DOMAIN
|
||||||
|
|
||||||
REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
|
REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
|
||||||
USER_SCHEMA = vol.Schema(
|
USER_SCHEMA = vol.Schema(
|
||||||
{vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
|
{
|
||||||
|
vol.Required(CONF_USERNAME): str,
|
||||||
|
vol.Required(CONF_PASSWORD): str,
|
||||||
|
vol.Optional(CONF_ACCOUNT): str,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,6 +26,7 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize the flow."""
|
"""Initialize the flow."""
|
||||||
self.username = None
|
self.username = None
|
||||||
|
self.account = None
|
||||||
|
|
||||||
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."""
|
||||||
|
@ -30,7 +35,9 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
client = OVOEnergy()
|
client = OVOEnergy()
|
||||||
try:
|
try:
|
||||||
authenticated = await client.authenticate(
|
authenticated = await client.authenticate(
|
||||||
user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
|
user_input[CONF_USERNAME],
|
||||||
|
user_input[CONF_PASSWORD],
|
||||||
|
user_input.get(CONF_ACCOUNT, None),
|
||||||
)
|
)
|
||||||
except aiohttp.ClientError:
|
except aiohttp.ClientError:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
@ -44,6 +51,7 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
data={
|
data={
|
||||||
CONF_USERNAME: user_input[CONF_USERNAME],
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
||||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||||
|
CONF_ACCOUNT: client.account_id,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,13 +68,16 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
if user_input and user_input.get(CONF_USERNAME):
|
if user_input and user_input.get(CONF_USERNAME):
|
||||||
self.username = user_input[CONF_USERNAME]
|
self.username = user_input[CONF_USERNAME]
|
||||||
|
|
||||||
|
if user_input and user_input.get(CONF_ACCOUNT):
|
||||||
|
self.account = user_input[CONF_ACCOUNT]
|
||||||
|
|
||||||
self.context["title_placeholders"] = {CONF_USERNAME: self.username}
|
self.context["title_placeholders"] = {CONF_USERNAME: self.username}
|
||||||
|
|
||||||
if user_input is not None and user_input.get(CONF_PASSWORD) is not None:
|
if user_input is not None and user_input.get(CONF_PASSWORD) is not None:
|
||||||
client = OVOEnergy()
|
client = OVOEnergy()
|
||||||
try:
|
try:
|
||||||
authenticated = await client.authenticate(
|
authenticated = await client.authenticate(
|
||||||
self.username, user_input[CONF_PASSWORD]
|
self.username, user_input[CONF_PASSWORD], self.account
|
||||||
)
|
)
|
||||||
except aiohttp.ClientError:
|
except aiohttp.ClientError:
|
||||||
errors["base"] = "connection_error"
|
errors["base"] = "connection_error"
|
||||||
|
@ -78,6 +89,7 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
data={
|
data={
|
||||||
CONF_USERNAME: self.username,
|
CONF_USERNAME: self.username,
|
||||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||||
|
CONF_ACCOUNT: self.account,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return self.async_abort(reason="reauth_successful")
|
return self.async_abort(reason="reauth_successful")
|
||||||
|
|
|
@ -3,3 +3,4 @@ DOMAIN = "ovo_energy"
|
||||||
|
|
||||||
DATA_CLIENT = "ovo_client"
|
DATA_CLIENT = "ovo_client"
|
||||||
DATA_COORDINATOR = "coordinator"
|
DATA_COORDINATOR = "coordinator"
|
||||||
|
CONF_ACCOUNT = "account"
|
||||||
|
|
|
@ -10,7 +10,8 @@
|
||||||
"user": {
|
"user": {
|
||||||
"data": {
|
"data": {
|
||||||
"username": "[%key:common::config_flow::data::username%]",
|
"username": "[%key:common::config_flow::data::username%]",
|
||||||
"password": "[%key:common::config_flow::data::password%]"
|
"password": "[%key:common::config_flow::data::password%]",
|
||||||
|
"account": "OVO account id (only add if you have multiple accounts)"
|
||||||
},
|
},
|
||||||
"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 Account"
|
"title": "Add OVO Energy Account"
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
"user": {
|
"user": {
|
||||||
"data": {
|
"data": {
|
||||||
"password": "Password",
|
"password": "Password",
|
||||||
"username": "Username"
|
"username": "Username",
|
||||||
|
"account": "OVO account id (only add if you have multiple accounts)"
|
||||||
},
|
},
|
||||||
"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 Account"
|
"title": "Add OVO Energy Account"
|
||||||
|
|
Loading…
Reference in New Issue