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 support for airqualitymonitor running firmware v4+ #1510

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ def status(self) -> AirQualityMonitorStatus:
self.model, AVAILABLE_PROPERTIES[MODEL_AIRQUALITYMONITOR_V1]
)

is_s1_firmware_version_4 = (
self.model == MODEL_AIRQUALITYMONITOR_S1
and self.info().firmware_version.startswith("4")
)
if is_s1_firmware_version_4 and "battery" in properties:
properties.remove("battery")

if self.model == MODEL_AIRQUALITYMONITOR_B1:
values = self.send("get_air_data")
else:
Expand Down
70 changes: 60 additions & 10 deletions miio/tests/test_airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,57 @@ def test_status(self):
)


class DummyDeviceInfo:
def __init__(self, version) -> None:
self.firmware_version = version


class DummyAirQualityMonitorS1(DummyDevice, AirQualityMonitor):
def __init__(self, *args, **kwargs):
def __init__(self, version, state, *args, **kwargs):
self._model = MODEL_AIRQUALITYMONITOR_S1
self.state = {
"battery": 100,
"co2": 695,
"humidity": 62.1,
"pm25": 19.4,
"temperature": 27.4,
"tvoc": 254,
}
self.version = version
self.state = state
self.return_values = {"get_prop": self._get_state}
super().__init__(args, kwargs)

def _get_state(self, props):
"""Return wanted properties."""
return self.state

def info(self):
return DummyDeviceInfo(version=self.version)


@pytest.fixture(scope="class")
def airqualitymonitors1(request):
request.cls.device = DummyAirQualityMonitorS1()
request.cls.device = DummyAirQualityMonitorS1(
version="3.1.8_9999",
state={
"battery": 100,
"co2": 695,
"humidity": 62.1,
"pm25": 19.4,
"temperature": 27.4,
"tvoc": 254,
},
)
# TODO add ability to test on a real device


@pytest.fixture(scope="class")
def airqualitymonitors1_v4(request):
request.cls.device = DummyAirQualityMonitorS1(
version="4.1.8_9999",
state={
"co2": 695,
"humidity": 62.1,
"pm25": 19.4,
"temperature": 27.4,
"tvoc": 254,
},
)


@pytest.mark.usefixtures("airqualitymonitors1")
class TestAirQualityMonitorS1(TestCase):
def state(self):
Expand All @@ -132,6 +158,30 @@ def test_status(self):
assert self.state().night_mode is None


@pytest.mark.usefixtures("airqualitymonitors1_v4")
class TestAirQualityMonitorS1_V4(TestCase):
def state(self):
return self.device.status()

def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(
AirQualityMonitorStatus(self.device.start_state)
)

assert self.state().co2 == self.device.start_state["co2"]
assert self.state().humidity == self.device.start_state["humidity"]
assert self.state().pm25 == self.device.start_state["pm25"]
assert self.state().temperature == self.device.start_state["temperature"]
assert self.state().tvoc == self.device.start_state["tvoc"]
assert self.state().aqi is None
assert self.state().battery is None
assert self.state().usb_power is None
assert self.state().display_clock is None
assert self.state().night_mode is None


class DummyAirQualityMonitorB1(DummyDevice, AirQualityMonitor):
def __init__(self, *args, **kwargs):
self._model = MODEL_AIRQUALITYMONITOR_B1
Expand Down