From 74e476a070bb84f12df2c7f25c921cb601b34157 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Sat, 29 Oct 2022 12:45:00 +0100 Subject: [PATCH] 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_user --- .../components/ovo_energy/__init__.py | 10 +++++++--- .../components/ovo_energy/config_flow.py | 20 +++++++++++++++---- homeassistant/components/ovo_energy/const.py | 1 + .../components/ovo_energy/strings.json | 3 ++- .../ovo_energy/translations/en.json | 3 ++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/ovo_energy/__init__.py b/homeassistant/components/ovo_energy/__init__.py index cb2ded6fce..cc19b0454b 100644 --- a/homeassistant/components/ovo_energy/__init__.py +++ b/homeassistant/components/ovo_energy/__init__.py @@ -21,7 +21,7 @@ from homeassistant.helpers.update_coordinator import ( UpdateFailed, ) -from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN +from .const import CONF_ACCOUNT, DATA_CLIENT, DATA_COORDINATOR, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -35,7 +35,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: try: 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: _LOGGER.warning(exception) @@ -49,7 +51,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async with async_timeout.timeout(10): try: 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: raise UpdateFailed(exception) from exception diff --git a/homeassistant/components/ovo_energy/config_flow.py b/homeassistant/components/ovo_energy/config_flow.py index f4dcc5301d..9e406ce6b9 100644 --- a/homeassistant/components/ovo_energy/config_flow.py +++ b/homeassistant/components/ovo_energy/config_flow.py @@ -6,11 +6,15 @@ import voluptuous as vol from homeassistant.config_entries import ConfigFlow 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}) 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): """Initialize the flow.""" self.username = None + self.account = None async def async_step_user(self, user_input=None): """Handle a flow initiated by the user.""" @@ -30,7 +35,9 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): client = OVOEnergy() try: 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: errors["base"] = "cannot_connect" @@ -44,6 +51,7 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): data={ CONF_USERNAME: user_input[CONF_USERNAME], 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): 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} if user_input is not None and user_input.get(CONF_PASSWORD) is not None: client = OVOEnergy() try: authenticated = await client.authenticate( - self.username, user_input[CONF_PASSWORD] + self.username, user_input[CONF_PASSWORD], self.account ) except aiohttp.ClientError: errors["base"] = "connection_error" @@ -78,6 +89,7 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): data={ CONF_USERNAME: self.username, CONF_PASSWORD: user_input[CONF_PASSWORD], + CONF_ACCOUNT: self.account, }, ) return self.async_abort(reason="reauth_successful") diff --git a/homeassistant/components/ovo_energy/const.py b/homeassistant/components/ovo_energy/const.py index f691eb9bc4..1068c5443f 100644 --- a/homeassistant/components/ovo_energy/const.py +++ b/homeassistant/components/ovo_energy/const.py @@ -3,3 +3,4 @@ DOMAIN = "ovo_energy" DATA_CLIENT = "ovo_client" DATA_COORDINATOR = "coordinator" +CONF_ACCOUNT = "account" diff --git a/homeassistant/components/ovo_energy/strings.json b/homeassistant/components/ovo_energy/strings.json index 6939229589..810602b141 100644 --- a/homeassistant/components/ovo_energy/strings.json +++ b/homeassistant/components/ovo_energy/strings.json @@ -10,7 +10,8 @@ "user": { "data": { "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.", "title": "Add OVO Energy Account" diff --git a/homeassistant/components/ovo_energy/translations/en.json b/homeassistant/components/ovo_energy/translations/en.json index 3539d91220..666c6fe677 100644 --- a/homeassistant/components/ovo_energy/translations/en.json +++ b/homeassistant/components/ovo_energy/translations/en.json @@ -17,7 +17,8 @@ "user": { "data": { "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.", "title": "Add OVO Energy Account"