Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 17, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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._failures < self._allowed_failures:
_LOGGER.info(
"Got exception while fetching the state: %s, failure: %d",
ex,
self._failure,
)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
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