Skip to content

Commit f5613db

Browse files
committed
decode HPMA115xx ACK messages, #26
1 parent 1b2af2d commit f5613db

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/pms/sensors/honeywell/hpma115s0.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ def checksum(self) -> int:
4141
return self.message[-1]
4242

4343
@classmethod
44-
def _validate(cls, message: bytes, header: bytes, length: int) -> base.Message:
45-
# validate ACK message
46-
if header == b"\xA5\xA5" and length == 2:
47-
assert message.endswith(header)
48-
return cls(message)
44+
def decode(cls, message: bytes, command: base.Cmd) -> Tuple[float, ...]:
45+
# decode ACK message
46+
if command.answer_header == b"\xA5\xA5" and command.answer_length == 2:
47+
if not message.endswith(command.answer_header):
48+
raise WrongMessageFormat("message does not end in ACK")
49+
return tuple()
50+
return super().decode(message, command)
4951

52+
@classmethod
53+
def _validate(cls, message: bytes, header: bytes, length: int) -> base.Message:
5054
# consistency check: bug in message singnature
5155
assert len(header) == 3, f"wrong header length {len(header)}"
5256
assert header[:1] == b"\x40", f"wrong header start {header!r}"

tests/sensor/test_sensor.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from pms import SensorWarning, sensors
6+
from pms import SensorWarning, WrongMessageFormat
77
from pms.core import Sensor, Supported
88

99

@@ -33,9 +33,17 @@ def test_pre_heat(sensor):
3333

3434
@pytest.mark.parametrize("sensor", ["HPMA115S0", "HPMA115C0"])
3535
@pytest.mark.parametrize("command", ["passive_mode", "wake"])
36-
@pytest.mark.parametrize("buffer", [b"\xA5\xA5", b"\xA5\xA5\xA5\xA5"])
37-
def test_HPMA115xx_ACK_message(sensor, command, buffer):
38-
assert Sensor[sensor].check(buffer, command)
36+
@pytest.mark.parametrize(
37+
"buffer,check",
38+
[
39+
pytest.param(b"\xA5\xA5", True, id="ACK"),
40+
pytest.param(b"\xA5\xA5\xA5\xA5", True, id="ACK ACK"),
41+
pytest.param(b"\x40\x0d\x04", False, id="no ACK"),
42+
pytest.param(b"\xA5\xA5\x40\x0d\x04", False, id="no ACK at end"),
43+
],
44+
)
45+
def test_HPMA115xx_ACK_message(sensor, command, buffer, check):
46+
assert Sensor[sensor].check(buffer, command) == check
3947

4048

4149
class RawData(NamedTuple):

0 commit comments

Comments
 (0)