Update ovoenergy to 2.0.0 (#115921)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Aidan Timson 2024-04-22 11:39:53 +01:00 committed by GitHub
parent 354e8e92f3
commit 6985d36f18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 56 additions and 23 deletions

View File

@ -7,13 +7,14 @@ from datetime import timedelta
import logging import logging
import aiohttp import aiohttp
from ovoenergy import OVOEnergy
from ovoenergy.models import OVODailyUsage from ovoenergy.models import OVODailyUsage
from ovoenergy.ovoenergy import OVOEnergy
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady 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.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
@ -32,29 +33,35 @@ PLATFORMS = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up OVO Energy from a config entry.""" """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: try:
authenticated = await client.authenticate( if not await client.authenticate(
entry.data[CONF_USERNAME], entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD], entry.data[CONF_PASSWORD],
entry.data[CONF_ACCOUNT], ):
) raise ConfigEntryAuthFailed
await client.bootstrap_accounts()
except aiohttp.ClientError as exception: except aiohttp.ClientError as exception:
_LOGGER.warning(exception) _LOGGER.warning(exception)
raise ConfigEntryNotReady from exception raise ConfigEntryNotReady from exception
if not authenticated:
raise ConfigEntryAuthFailed
async def async_update_data() -> OVODailyUsage: async def async_update_data() -> OVODailyUsage:
"""Fetch data from OVO Energy.""" """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): async with asyncio.timeout(10):
try: try:
authenticated = await client.authenticate( authenticated = await client.authenticate(
entry.data[CONF_USERNAME], entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD], 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

View File

@ -6,11 +6,12 @@ from collections.abc import Mapping
from typing import Any from typing import Any
import aiohttp import aiohttp
from ovoenergy.ovoenergy import OVOEnergy from ovoenergy import OVOEnergy
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_ACCOUNT, DOMAIN from .const import CONF_ACCOUNT, DOMAIN
@ -41,13 +42,19 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a flow initiated by the user.""" """Handle a flow initiated by the user."""
errors = {} errors = {}
if user_input is not None: 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: try:
authenticated = await client.authenticate( authenticated = await client.authenticate(
user_input[CONF_USERNAME], user_input[CONF_USERNAME],
user_input[CONF_PASSWORD], user_input[CONF_PASSWORD],
user_input.get(CONF_ACCOUNT, None),
) )
await client.bootstrap_accounts()
except aiohttp.ClientError: except aiohttp.ClientError:
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
else: else:
@ -86,10 +93,17 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
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(
client_session=async_get_clientsession(self.hass),
)
if self.account is not None:
client.custom_account_id = self.account
try: try:
authenticated = await client.authenticate( authenticated = await client.authenticate(
self.username, user_input[CONF_PASSWORD], self.account self.username,
user_input[CONF_PASSWORD],
) )
except aiohttp.ClientError: except aiohttp.ClientError:
errors["base"] = "connection_error" errors["base"] = "connection_error"

View File

@ -7,5 +7,5 @@
"integration_type": "service", "integration_type": "service",
"iot_class": "cloud_polling", "iot_class": "cloud_polling",
"loggers": ["ovoenergy"], "loggers": ["ovoenergy"],
"requirements": ["ovoenergy==1.3.1"] "requirements": ["ovoenergy==2.0.0"]
} }

View File

@ -7,8 +7,8 @@ import dataclasses
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Final from typing import Final
from ovoenergy import OVOEnergy
from ovoenergy.models import OVODailyUsage from ovoenergy.models import OVODailyUsage
from ovoenergy.ovoenergy import OVOEnergy
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,

View File

@ -1495,7 +1495,7 @@ orvibo==1.1.2
ourgroceries==1.5.4 ourgroceries==1.5.4
# homeassistant.components.ovo_energy # homeassistant.components.ovo_energy
ovoenergy==1.3.1 ovoenergy==2.0.0
# homeassistant.components.p1_monitor # homeassistant.components.p1_monitor
p1monitor==3.0.0 p1monitor==3.0.0

View File

@ -1189,7 +1189,7 @@ oralb-ble==0.17.6
ourgroceries==1.5.4 ourgroceries==1.5.4
# homeassistant.components.ovo_energy # homeassistant.components.ovo_energy
ovoenergy==1.3.1 ovoenergy==2.0.0
# homeassistant.components.p1_monitor # homeassistant.components.p1_monitor
p1monitor==3.0.0 p1monitor==3.0.0

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
import aiohttp import aiohttp
from homeassistant import config_entries 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.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
@ -13,7 +13,11 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
FIXTURE_REAUTH_INPUT = {CONF_PASSWORD: "something1"} 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" 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["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
with patch( with (
patch(
"homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate",
return_value=False, return_value=False,
),
patch(
"homeassistant.components.ovo_energy.config_flow.OVOEnergy.bootstrap_accounts",
),
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -88,6 +97,9 @@ async def test_full_flow_implementation(hass: HomeAssistant) -> None:
"homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate",
return_value=True, return_value=True,
), ),
patch(
"homeassistant.components.ovo_energy.config_flow.OVOEnergy.bootstrap_accounts",
),
patch( patch(
"homeassistant.components.ovo_energy.config_flow.OVOEnergy.username", "homeassistant.components.ovo_energy.config_flow.OVOEnergy.username",
"some_name", "some_name",