Skip to content

Commit

Permalink
Review comments fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
foxel committed Mar 11, 2020
1 parent d18a7e9 commit a400d56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
11 changes: 10 additions & 1 deletion miio/airfilter_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
import re
from typing import Optional


class FilterType(enum.Enum):
Expand All @@ -21,12 +22,20 @@ class FilterTypeUtil:

_filter_type_cache = {}

def determine_filter_type(self, product_id: str) -> FilterType:
def determine_filter_type(self, rfid_tag: Optional[str], product_id: Optional[str]) -> Optional[FilterType]:
"""
Determine Xiaomi air filter type based on its product ID.
:param rfid_tag: RFID tag value
:param product_id: Product ID such as "0:0:30:33"
"""
if rfid_tag is None:
return None
if rfid_tag == "0:0:0:0:0:0:0":
return FilterType.Unknown
if product_id is None:
return FilterType.Regular

ft = self._filter_type_cache.get(product_id, None)
if ft is None:
for filter_re, filter_type in FILTER_TYPE_RE:
Expand Down
8 changes: 1 addition & 7 deletions miio/airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,7 @@ def filter_rfid_tag(self) -> Optional[str]:
@property
def filter_type(self) -> Optional[FilterType]:
"""Type of installed filter."""
if self.filter_rfid_tag is None:
return None
if self.filter_rfid_tag == "0:0:0:0:0:0:0":
return FilterType.Unknown
if self.filter_rfid_product_id is None:
return FilterType.Regular
return self.filter_type_util.determine_filter_type(self.filter_rfid_product_id)
return self.filter_type_util.determine_filter_type(self.filter_rfid_tag, self.filter_rfid_product_id)

@property
def learn_mode(self) -> bool:
Expand Down
15 changes: 5 additions & 10 deletions miio/airpurifier_miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,7 @@ def filter_rfid_tag(self) -> Optional[str]:
@property
def filter_type(self) -> Optional[FilterType]:
"""Type of installed filter."""
if self.filter_rfid_tag is None:
return None
if self.filter_rfid_tag == "0:0:0:0:0:0:0":
return FilterType.Unknown
if self.filter_rfid_product_id is None:
return FilterType.Regular
return self.filter_type_util.determine_filter_type(self.filter_rfid_product_id)
return self.filter_type_util.determine_filter_type(self.filter_rfid_tag, self.filter_rfid_product_id)

def __repr__(self) -> str:
s = (
Expand Down Expand Up @@ -364,12 +358,13 @@ def set_mode(self, mode: OperationMode):
default_output=format_output("Setting favorite level to {level}"),
)
def set_favorite_level(self, level: int):
"""Set favorite level."""
"""Set favorite level.
Set the favorite level used when the mode is `favorite`,
should be between 0 and 14.
"""
if level < 0 or level > 14:
raise AirPurifierMiotException("Invalid favorite level: %s" % level)

# Set the favorite level used when the mode is `favorite`,
# should be between 0 and 14.
return self.set_property("favorite_level", level)

@command(
Expand Down
13 changes: 10 additions & 3 deletions miio/tests/test_airfilter_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ def airfilter_util(request):

@pytest.mark.usefixtures("airfilter_util")
class TestAirFilterUtil(TestCase):
def test_determine_filter_type__recognises_unknown_filter(self):
assert (
self.filter_type_util.determine_filter_type("0:0:0:0:0:0:0", None)
is FilterType.Unknown
)

def test_determine_filter_type__recognises_antibacterial_filter(self):
assert (
self.filter_type_util.determine_filter_type("12:34:41:30")
self.filter_type_util.determine_filter_type("80:64:d1:ba:4f:5f:4", "12:34:41:30")
is FilterType.AntiBacterial
)

def test_determine_filter_type__recognises_antiformaldehyde_filter(self):
assert (
self.filter_type_util.determine_filter_type("12:34:00:31")
self.filter_type_util.determine_filter_type("80:64:d1:ba:4f:5f:4", "12:34:00:31")
is FilterType.AntiFormaldehyde
)

Expand All @@ -30,9 +36,10 @@ def test_determine_filter_type__falls_back_to_regular_filter(self):
"12:34:56:31",
"12:34:56:31:11:11",
"CO:FF:FF:EE",
None,
]
for product_id in regular_filters:
assert (
self.filter_type_util.determine_filter_type(product_id)
self.filter_type_util.determine_filter_type("80:64:d1:ba:4f:5f:4", product_id)
is FilterType.Regular
)

0 comments on commit a400d56

Please sign in to comment.