From ca8ca76fd4488aff651239ec8418502ac2c8e865 Mon Sep 17 00:00:00 2001 From: Chris Close Date: Sun, 31 May 2026 15:48:39 +1000 Subject: [PATCH] Fix unhandled CommandTimeoutError in async_update Wraps async_get_instant_metrics and async_get_daily_power_consumption calls in try/except CommandTimeoutError to prevent entity update failures. Fixes #597 --- custom_components/meross_cloud/sensor.py | 5 ++++- custom_components/meross_cloud/switch.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/custom_components/meross_cloud/sensor.py b/custom_components/meross_cloud/sensor.py index 6474a836d7a..c4678ebea30 100644 --- a/custom_components/meross_cloud/sensor.py +++ b/custom_components/meross_cloud/sensor.py @@ -274,7 +274,10 @@ async def async_update(self): await super().async_update() _LOGGER.debug(f"Refreshing instant metrics for device {self.name}") - self._daily_consumption = await self._device.async_get_daily_power_consumption(channel=self._channel_id) + try: + self._daily_consumption = await self._device.async_get_daily_power_consumption(channel=self._channel_id) + except CommandTimeoutError: + log_exception(logger=_LOGGER, device=self._device) @property def native_value(self) -> StateType: diff --git a/custom_components/meross_cloud/switch.py b/custom_components/meross_cloud/switch.py index 962a52f6a83..8f1f8e78c62 100644 --- a/custom_components/meross_cloud/switch.py +++ b/custom_components/meross_cloud/switch.py @@ -11,6 +11,7 @@ from meross_iot.controller.mixins.dnd import SystemDndMixin from meross_iot.controller.mixins.toggle import ToggleXMixin, ToggleMixin from meross_iot.manager import MerossManager +from meross_iot.model.exception import CommandTimeoutError from meross_iot.model.http.device import HttpDeviceInfo from meross_iot.model.enums import DNDMode @@ -18,7 +19,7 @@ from homeassistant.components.switch import SwitchEntity from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from . import MerossDevice -from .common import (DOMAIN, MANAGER, DEVICE_LIST_COORDINATOR, HA_SWITCH) +from .common import (DOMAIN, MANAGER, DEVICE_LIST_COORDINATOR, HA_SWITCH, log_exception) _LOGGER = logging.getLogger(__name__) @@ -61,10 +62,16 @@ async def async_update(self): # If the device supports power reading, update it if isinstance(self._device, ElectricityMixin): - self._last_power_sample = await self._device.async_get_instant_metrics(channel=self._channel_id) + try: + self._last_power_sample = await self._device.async_get_instant_metrics(channel=self._channel_id) + except CommandTimeoutError: + log_exception(logger=_LOGGER, device=self._device) if isinstance(self._device, ConsumptionXMixin): - self._daily_consumption = await self._device.async_get_daily_power_consumption(channel=self._channel_id) + try: + self._daily_consumption = await self._device.async_get_daily_power_consumption(channel=self._channel_id) + except CommandTimeoutError: + log_exception(logger=_LOGGER, device=self._device) @property def is_on(self) -> bool: