From 6985d36f18d5dd3a8bddac190a89f42ef1fb187f Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Mon, 22 Apr 2024 11:39:53 +0100 Subject: [PATCH] Update ovoenergy to 2.0.0 (#115921) Co-authored-by: J. Nick Koston --- .../components/ovo_energy/__init__.py | 25 ++++++++++++------- .../components/ovo_energy/config_flow.py | 24 ++++++++++++++---- .../components/ovo_energy/manifest.json | 2 +- homeassistant/components/ovo_energy/sensor.py | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../components/ovo_energy/test_config_flow.py | 22 ++++++++++++---- 7 files changed, 56 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/ovo_energy/__init__.py b/homeassistant/components/ovo_energy/__init__.py index e0c2b77664a..d207f3161f4 100644 --- a/homeassistant/components/ovo_energy/__init__.py +++ b/homeassistant/components/ovo_energy/__init__.py @@ -7,13 +7,14 @@ from datetime import timedelta import logging import aiohttp +from ovoenergy import OVOEnergy from ovoenergy.models import OVODailyUsage -from ovoenergy.ovoenergy import OVOEnergy from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, @@ -32,29 +33,35 @@ PLATFORMS = [Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up OVO Energy from a config entry.""" - client = OVOEnergy() + client = OVOEnergy( + client_session=async_get_clientsession(hass), + ) + + if custom_account := entry.data.get(CONF_ACCOUNT) is not None: + client.custom_account_id = custom_account try: - authenticated = await client.authenticate( + if not await client.authenticate( entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], - entry.data[CONF_ACCOUNT], - ) + ): + raise ConfigEntryAuthFailed + + await client.bootstrap_accounts() except aiohttp.ClientError as exception: _LOGGER.warning(exception) raise ConfigEntryNotReady from exception - if not authenticated: - raise ConfigEntryAuthFailed - async def async_update_data() -> OVODailyUsage: """Fetch data from OVO Energy.""" + if custom_account := entry.data.get(CONF_ACCOUNT) is not None: + client.custom_account_id = custom_account + async with asyncio.timeout(10): try: authenticated = await client.authenticate( 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 41c64913764..87d53e5fbf9 100644 --- a/homeassistant/components/ovo_energy/config_flow.py +++ b/homeassistant/components/ovo_energy/config_flow.py @@ -6,11 +6,12 @@ from collections.abc import Mapping from typing import Any import aiohttp -from ovoenergy.ovoenergy import OVOEnergy +from ovoenergy import OVOEnergy import voluptuous as vol from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import CONF_ACCOUNT, DOMAIN @@ -41,13 +42,19 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a flow initiated by the user.""" errors = {} if user_input is not None: - client = OVOEnergy() + client = OVOEnergy( + client_session=async_get_clientsession(self.hass), + ) + + if custom_account := user_input.get(CONF_ACCOUNT) is not None: + client.custom_account_id = custom_account + try: authenticated = await client.authenticate( user_input[CONF_USERNAME], user_input[CONF_PASSWORD], - user_input.get(CONF_ACCOUNT, None), ) + await client.bootstrap_accounts() except aiohttp.ClientError: errors["base"] = "cannot_connect" else: @@ -86,10 +93,17 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): 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() + client = OVOEnergy( + client_session=async_get_clientsession(self.hass), + ) + + if self.account is not None: + client.custom_account_id = self.account + try: authenticated = await client.authenticate( - self.username, user_input[CONF_PASSWORD], self.account + self.username, + user_input[CONF_PASSWORD], ) except aiohttp.ClientError: errors["base"] = "connection_error" diff --git a/homeassistant/components/ovo_energy/manifest.json b/homeassistant/components/ovo_energy/manifest.json index 9435958f1fe..af4a313206e 100644 --- a/homeassistant/components/ovo_energy/manifest.json +++ b/homeassistant/components/ovo_energy/manifest.json @@ -7,5 +7,5 @@ "integration_type": "service", "iot_class": "cloud_polling", "loggers": ["ovoenergy"], - "requirements": ["ovoenergy==1.3.1"] + "requirements": ["ovoenergy==2.0.0"] } diff --git a/homeassistant/components/ovo_energy/sensor.py b/homeassistant/components/ovo_energy/sensor.py index d5384837e9c..5b16e8cdef5 100644 --- a/homeassistant/components/ovo_energy/sensor.py +++ b/homeassistant/components/ovo_energy/sensor.py @@ -7,8 +7,8 @@ import dataclasses from datetime import datetime, timedelta from typing import Final +from ovoenergy import OVOEnergy from ovoenergy.models import OVODailyUsage -from ovoenergy.ovoenergy import OVOEnergy from homeassistant.components.sensor import ( SensorDeviceClass, diff --git a/requirements_all.txt b/requirements_all.txt index fa3e5893eef..573f2f4a8d3 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1495,7 +1495,7 @@ orvibo==1.1.2 ourgroceries==1.5.4 # homeassistant.components.ovo_energy -ovoenergy==1.3.1 +ovoenergy==2.0.0 # homeassistant.components.p1_monitor p1monitor==3.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8a10ee1c176..b3e0b9feaf6 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1189,7 +1189,7 @@ oralb-ble==0.17.6 ourgroceries==1.5.4 # homeassistant.components.ovo_energy -ovoenergy==1.3.1 +ovoenergy==2.0.0 # homeassistant.components.p1_monitor p1monitor==3.0.0 diff --git a/tests/components/ovo_energy/test_config_flow.py b/tests/components/ovo_energy/test_config_flow.py index 7575f1edb29..00899e745b9 100644 --- a/tests/components/ovo_energy/test_config_flow.py +++ b/tests/components/ovo_energy/test_config_flow.py @@ -5,7 +5,7 @@ from unittest.mock import patch import aiohttp from homeassistant import config_entries -from homeassistant.components.ovo_energy.const import DOMAIN +from homeassistant.components.ovo_energy.const import CONF_ACCOUNT, DOMAIN from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -13,7 +13,11 @@ from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry FIXTURE_REAUTH_INPUT = {CONF_PASSWORD: "something1"} -FIXTURE_USER_INPUT = {CONF_USERNAME: "example@example.com", CONF_PASSWORD: "something"} +FIXTURE_USER_INPUT = { + CONF_USERNAME: "example@example.com", + CONF_PASSWORD: "something", + CONF_ACCOUNT: "123456", +} UNIQUE_ID = "example@example.com" @@ -37,9 +41,14 @@ async def test_authorization_error(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" - with patch( - "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", - return_value=False, + with ( + patch( + "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", + return_value=False, + ), + patch( + "homeassistant.components.ovo_energy.config_flow.OVOEnergy.bootstrap_accounts", + ), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -88,6 +97,9 @@ async def test_full_flow_implementation(hass: HomeAssistant) -> None: "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", return_value=True, ), + patch( + "homeassistant.components.ovo_energy.config_flow.OVOEnergy.bootstrap_accounts", + ), patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.username", "some_name",