Allow some failures before setting Xiaomi Miio MIOT air purifiers unavailable (#50755)

This commit is contained in:
Maciej Bieniek 2021-05-17 14:13:01 +02:00 committed by GitHub
parent 0fac3ccebc
commit 97559087b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 @@ class XiaomiGenericDevice(XiaomiMiioEntity, FanEntity):
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 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
}
)
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: