Skip to content

Commit

Permalink
Allow some failures before setting Xiaomi Miio MIOT air purifiers una…
Browse files Browse the repository at this point in the history
…vailable (home-assistant#50755)
  • Loading branch information
bieniu authored and tkdrob committed May 17, 2021
1 parent 661d6f1 commit 2f74b19
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions homeassistant/components/xiaomi_miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2f74b19

Please sign in to comment.