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

Fix water_level calculation for humidifiers #1140

Merged
merged 12 commits into from
Oct 3, 2021
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Home Assistant (custom)
Other related projects
----------------------

This is a list of other projects around the xiaomi ecosystem that you can find interesting.
This is a list of other projects around the Xiaomi ecosystem that you can find interesting.
Feel free to submit more related projects.

- `dustcloud <https://github.com/dgiese/dustcloud>`__ (reverse engineering and rooting xiaomi devices)
Expand Down
12 changes: 8 additions & 4 deletions miio/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ def depth(self) -> Optional[int]:
def water_level(self) -> Optional[int]:
"""Return current water level in percent.
If water tank is full, depth is 125.
If water tank is full, depth is 120. If water tank is overfilled, depth is 125.
"""
depth = self.data.get("depth")
if depth is not None and depth <= 125:
return int(depth / 1.25)
return None
if depth is None or depth > 125:
return None

if depth < 0:
return 0

return int(min(depth / 1.2, 100))

@property
def water_tank_detached(self) -> Optional[bool]:
Expand Down
14 changes: 10 additions & 4 deletions miio/airhumidifier_miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,17 @@ def target_humidity(self) -> int:
def water_level(self) -> Optional[int]:
"""Return current water level in percent.
If water tank is full, depth is 125.
If water tank is full, raw water_level value is 120. If water tank is
overfilled, raw water_level value is 125.
"""
if self.data["water_level"] <= 125:
return int(self.data["water_level"] / 1.25)
return None
water_level = self.data["water_level"]
if water_level > 125:
return None

if water_level < 0:
return 0

return int(min(water_level / 1.2, 100))

@property
def water_tank_detached(self) -> bool:
Expand Down
Loading