Skip to content

Commit

Permalink
Airmonitor after upgrading to 4, does not transmit battery status
Browse files Browse the repository at this point in the history
fix #1489
  • Loading branch information
WeslyG committed Aug 19, 2022
1 parent 13d6f4a commit 5850e27
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
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

0 comments on commit 5850e27

Please sign in to comment.