Skip to content

Commit

Permalink
Switch to PyPMS internal pre-heat settings
Browse files Browse the repository at this point in the history
This is a viable replacement for PMSx003 sensors since [^1]. It
makes sense to embed warm up knowledge in the PyPMS repository,
rather than pushing this one bit of implementation to consumers.

[^1]: avaldebe/PyPMS#35
  • Loading branch information
benthorner committed Dec 9, 2022
1 parent 19a981b commit a0fcdcd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/snsary/contrib/pypms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(
*,
sensor_name,
port="/dev/ttyS0",
warm_up_seconds=10,
timeout=5,
):
self.warm_up_seconds = warm_up_seconds
Expand Down Expand Up @@ -71,7 +70,7 @@ def stop(self):
self.logger.exception(e)

def sample(self, timestamp, elapsed_seconds, **kwargs):
if elapsed_seconds < self.warm_up_seconds:
if elapsed_seconds < self.__reader.pre_heat:
self.logger.info("Still warming up, no data yet.")
return []

Expand Down
22 changes: 5 additions & 17 deletions tests/contrib/test_pypms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def sensor(mock_sensor):
return PyPMSSensor(
sensor_name="PMSx003",
port=mock_sensor.port,
warm_up_seconds=0,
# timeout should be low to avoid failure tests
# hanging, high to still allow PTY to work
timeout=0.01,
Expand Down Expand Up @@ -157,25 +156,14 @@ def test_sample(
started_sensor,
):
readings = started_sensor.sample(timestamp="now", elapsed_seconds=0)
assert len(readings) == 12

pm10_reading = next(r for r in readings if r.name == "pm10")
assert pm10_reading.value == 11822
assert pm10_reading.timestamp == "now"

assert len(readings) == 0 # PMSx003 has 10 second warm up

def test_sample_warm_up(
started_sensor,
):
started_sensor.warm_up_seconds = 5
readings = started_sensor.sample(timestamp="now", elapsed_seconds=0)
assert len(readings) == 0

readings = started_sensor.sample(timestamp="now", elapsed_seconds=5)
readings = started_sensor.sample(timestamp="now", elapsed_seconds=10)
assert len(readings) == 12

pm10_reading = next(r for r in readings if r.name == "pm10")
assert pm10_reading.value == 11822
assert pm10_reading.timestamp == "now"


def test_sample_bad_response(
Expand All @@ -189,12 +177,12 @@ def test_sample_bad_response(
)

with pytest.raises(pms.WrongMessageFormat):
started_sensor.sample(timestamp="now", elapsed_seconds=0)
started_sensor.sample(timestamp="now", elapsed_seconds=30)


def test_sample_already_closed(
sensor,
mock_stop,
):
with pytest.raises(StopIteration):
sensor.sample(timestamp="now", elapsed_seconds=0)
sensor.sample(timestamp="now", elapsed_seconds=30)

0 comments on commit a0fcdcd

Please sign in to comment.