diff --git a/homeassistant/components/xiaomi_miio/fan.py b/homeassistant/components/xiaomi_miio/fan.py index 6d18131cdeb0be..a485654e63837b 100644 --- a/homeassistant/components/xiaomi_miio/fan.py +++ b/homeassistant/components/xiaomi_miio/fan.py @@ -551,7 +551,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if model in MODELS_PURIFIER_MIOT: air_purifier = AirPurifierMiot(host, token) - entity = XiaomiAirPurifierMiot(name, air_purifier, config_entry, unique_id) + entity = XiaomiAirPurifierMiot( + name, air_purifier, config_entry, unique_id, allowed_failures=2 + ) elif model.startswith("zhimi.airpurifier."): air_purifier = AirPurifier(host, token) entity = XiaomiAirPurifier(name, air_purifier, config_entry, unique_id) @@ -769,9 +771,11 @@ async def async_set_child_lock_off(self): class XiaomiAirPurifier(XiaomiGenericDevice): """Representation of a Xiaomi Air Purifier.""" - def __init__(self, name, device, entry, unique_id): + def __init__(self, name, device, entry, unique_id, allowed_failures=0): """Initialize the plug switch.""" super().__init__(name, device, entry, unique_id) + self._allowed_failures = allowed_failures + self._failure = 0 if self._model == MODEL_AIRPURIFIER_PRO: self._device_features = FEATURE_FLAGS_AIRPURIFIER_PRO @@ -822,10 +826,24 @@ async def async_update(self): } ) + self._failure = 0 + except DeviceException as ex: - if self._available: - self._available = False - _LOGGER.error("Got exception while fetching the state: %s", ex) + self._failure += 1 + if self._failure < self._allowed_failures: + _LOGGER.info( + "Got exception while fetching the state: %s, failure: %d", + ex, + self._failure, + ) + else: + if self._available: + self._available = False + _LOGGER.error( + "Got exception while fetching the state: %s, failure: %d", + ex, + self._failure, + ) @property def speed_list(self) -> list: