Fix switch race conditions and dropped API commands in yardian (#176767)

This commit is contained in:
Yardian Support
2026-08-24 22:40:48 +02:00
committed by GitHub
parent 5c727b3128
commit 247fa20afa
4 changed files with 28 additions and 15 deletions
+2 -1
View File
@@ -5,5 +5,6 @@ MANUFACTURER = "Aeon Matrix"
PRODUCT_NAME = "Yardian Smart Sprinkler Controller"
DEFAULT_WATERING_DURATION = 6
SWITCH_REFRESH_DELAY = 2
SWITCH_ON_REFRESH_DELAY = 2
SWITCH_OFF_REFRESH_DELAY = 4
BUTTON_REFRESH_DELAY = 3
@@ -1,6 +1,5 @@
"""Update coordinators for Yardian."""
import asyncio
from dataclasses import dataclass
import datetime
import logging
@@ -89,11 +88,8 @@ class YardianUpdateCoordinator(DataUpdateCoordinator[YardianCoordinatorData]):
type(self.controller).__name__,
)
try:
async with asyncio.timeout(10):
# Fetch device state and operation info; specific exceptions are
# handled by the outer block to avoid double-logging.
dev_state = await self.controller.fetch_device_state()
oper_info = await self.controller.fetch_oper_info()
dev_state = await self.controller.fetch_device_state()
oper_info = await self.controller.fetch_oper_info()
except TimeoutError as e:
raise UpdateFailed("Timeout communicating with device") from e
+20 -7
View File
@@ -11,7 +11,11 @@ from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import VolDictType
from .const import DEFAULT_WATERING_DURATION, SWITCH_REFRESH_DELAY
from .const import (
DEFAULT_WATERING_DURATION,
SWITCH_OFF_REFRESH_DELAY,
SWITCH_ON_REFRESH_DELAY,
)
from .coordinator import YardianConfigEntry, YardianUpdateCoordinator
from .entity import YardianZoneEntity
@@ -69,16 +73,25 @@ class YardianSwitch(YardianZoneEntity, SwitchEntity):
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self.coordinator.controller.start_irrigation(
self._zone_id,
kwargs.get("duration", DEFAULT_WATERING_DURATION),
)
await asyncio.sleep(SWITCH_REFRESH_DELAY)
# Optimistic UI update
self.coordinator.data.active_zones.add(self._zone_id)
self.async_write_ha_state()
duration = kwargs.get("duration", DEFAULT_WATERING_DURATION)
await self.coordinator.controller.start_irrigation(self._zone_id, duration)
await asyncio.sleep(SWITCH_ON_REFRESH_DELAY)
await self.coordinator.async_request_refresh()
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
# Optimistic UI update
self.coordinator.data.active_zones.discard(self._zone_id)
self.async_write_ha_state()
await self.coordinator.controller.stop_zone(self._zone_id)
await asyncio.sleep(SWITCH_REFRESH_DELAY)
await asyncio.sleep(SWITCH_OFF_REFRESH_DELAY)
await self.coordinator.async_request_refresh()
+4 -1
View File
@@ -91,5 +91,8 @@ def switch_platform_only() -> Generator[None]:
@pytest.fixture(autouse=True)
def mock_ui_refresh_delay() -> Generator[None]:
"""Patch the Switch refresh delay to 0 seconds to speed up tests."""
with patch("homeassistant.components.yardian.switch.SWITCH_REFRESH_DELAY", 0):
with (
patch("homeassistant.components.yardian.switch.SWITCH_ON_REFRESH_DELAY", 0),
patch("homeassistant.components.yardian.switch.SWITCH_OFF_REFRESH_DELAY", 0),
):
yield