mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Reduce the amount of data fetched in individual Hydrawise API calls (#120328)
This commit is contained in:
parent
46dcf1dc44
commit
3b79ab6e18
@ -37,8 +37,8 @@ class HydrawiseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
# Verify that the provided credentials work."""
|
||||
api = client.Hydrawise(auth.Auth(username, password))
|
||||
try:
|
||||
# Skip fetching zones to save on metered API calls.
|
||||
user = await api.get_user()
|
||||
# Don't fetch zones because we don't need them yet.
|
||||
user = await api.get_user(fetch_zones=False)
|
||||
except NotAuthorizedError:
|
||||
return on_failure("invalid_auth")
|
||||
except TimeoutError:
|
||||
|
@ -40,13 +40,17 @@ class HydrawiseDataUpdateCoordinator(DataUpdateCoordinator[HydrawiseData]):
|
||||
|
||||
async def _async_update_data(self) -> HydrawiseData:
|
||||
"""Fetch the latest data from Hydrawise."""
|
||||
user = await self.api.get_user()
|
||||
# Don't fetch zones. We'll fetch them for each controller later.
|
||||
# This is to prevent 502 errors in some cases.
|
||||
# See: https://github.com/home-assistant/core/issues/120128
|
||||
user = await self.api.get_user(fetch_zones=False)
|
||||
controllers = {}
|
||||
zones = {}
|
||||
sensors = {}
|
||||
daily_water_use: dict[int, ControllerWaterUseSummary] = {}
|
||||
for controller in user.controllers:
|
||||
controllers[controller.id] = controller
|
||||
controller.zones = await self.api.get_zones(controller)
|
||||
for zone in controller.zones:
|
||||
zones[zone.id] = zone
|
||||
for sensor in controller.sensors:
|
||||
|
@ -1,6 +1,6 @@
|
||||
"""Common fixtures for the Hydrawise tests."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from collections.abc import Awaitable, Callable, Generator
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
@ -20,7 +20,6 @@ from pydrawise.schema import (
|
||||
Zone,
|
||||
)
|
||||
import pytest
|
||||
from typing_extensions import Generator
|
||||
|
||||
from homeassistant.components.hydrawise.const import DOMAIN
|
||||
from homeassistant.const import CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME
|
||||
@ -67,9 +66,9 @@ def mock_pydrawise(
|
||||
"""Mock Hydrawise."""
|
||||
with patch("pydrawise.client.Hydrawise", autospec=True) as mock_pydrawise:
|
||||
user.controllers = [controller]
|
||||
controller.zones = zones
|
||||
controller.sensors = sensors
|
||||
mock_pydrawise.return_value.get_user.return_value = user
|
||||
mock_pydrawise.return_value.get_zones.return_value = zones
|
||||
mock_pydrawise.return_value.get_water_use_summary.return_value = (
|
||||
controller_water_use_summary
|
||||
)
|
||||
@ -142,7 +141,7 @@ def sensors() -> list[Sensor]:
|
||||
),
|
||||
status=SensorStatus(
|
||||
water_flow=LocalizedValueType(value=577.0044752010709, unit="gal"),
|
||||
active=None,
|
||||
active=False,
|
||||
),
|
||||
),
|
||||
]
|
||||
@ -154,7 +153,6 @@ def zones() -> list[Zone]:
|
||||
return [
|
||||
Zone(
|
||||
name="Zone One",
|
||||
number=1,
|
||||
id=5965394,
|
||||
scheduled_runs=ScheduledZoneRuns(
|
||||
summary="",
|
||||
@ -171,7 +169,6 @@ def zones() -> list[Zone]:
|
||||
),
|
||||
Zone(
|
||||
name="Zone Two",
|
||||
number=2,
|
||||
id=5965395,
|
||||
scheduled_runs=ScheduledZoneRuns(
|
||||
current_run=ScheduledZoneRun(
|
||||
|
@ -46,7 +46,7 @@ async def test_form(
|
||||
CONF_PASSWORD: "__password__",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
mock_pydrawise.get_user.assert_called_once_with()
|
||||
mock_pydrawise.get_user.assert_called_once_with(fetch_zones=False)
|
||||
|
||||
|
||||
async def test_form_api_error(
|
||||
|
Loading…
x
Reference in New Issue
Block a user