Skip to content

Commit

Permalink
Fix water_level calculation for humidifiers (#1140)
Browse files Browse the repository at this point in the history
* Fix water level calculation

* Run docformatter

* Run docformatter once again

* Run pre-commit

* Add water level tests

* Black

* make linters happy

* Generalize airhumidifer tests, parametrize water_level tests

* Cleanup set_dry test

* Fix set_dry tests on v1

* Simplify water_level logic

* Convert test_airhumidifer_miot away from TestCase

Co-authored-by: Teemu Rytilahti <tpr@iki.fi>
  • Loading branch information
bieniu and rytilahti authored Oct 3, 2021
1 parent dea3e91 commit b633844
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 682 deletions.
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

0 comments on commit b633844

Please sign in to comment.