diff --git a/homeassistant/components/hydrawise/config_flow.py b/homeassistant/components/hydrawise/config_flow.py index 1c2c1c5cf29..ab9ebbb065d 100644 --- a/homeassistant/components/hydrawise/config_flow.py +++ b/homeassistant/components/hydrawise/config_flow.py @@ -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: diff --git a/homeassistant/components/hydrawise/coordinator.py b/homeassistant/components/hydrawise/coordinator.py index d046dfcc92a..50caaa0c0de 100644 --- a/homeassistant/components/hydrawise/coordinator.py +++ b/homeassistant/components/hydrawise/coordinator.py @@ -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: diff --git a/tests/components/hydrawise/conftest.py b/tests/components/hydrawise/conftest.py index eb1518eb7f2..0b5327cd7b2 100644 --- a/tests/components/hydrawise/conftest.py +++ b/tests/components/hydrawise/conftest.py @@ -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( diff --git a/tests/components/hydrawise/test_config_flow.py b/tests/components/hydrawise/test_config_flow.py index a7fbc008aab..e85b1b9b249 100644 --- a/tests/components/hydrawise/test_config_flow.py +++ b/tests/components/hydrawise/test_config_flow.py @@ -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(