Skip to content

Commit

Permalink
Fixing the opening of the serial port so it respects the connect pa…
Browse files Browse the repository at this point in the history
…ram. Fixing tests for some coverage.
  • Loading branch information
wtgee committed Sep 3, 2023
1 parent 8409a73 commit e8a4952
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/aag/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ def __init__(self, connect: bool = True, **kwargs):
try:
self._sensor: serial.Serial = serial.serial_for_url(self.config.serial_port,
baudrate=9600,
timeout=1)
timeout=1,
do_not_open=True
)
if connect:
self._sensor.open()
self._sensor.reset_input_buffer()
self._sensor.reset_output_buffer()
except serial.serialutil.SerialException as e:
print(f'[red]Unable to connect to weather sensor. Check the port. {e}')
raise e

self._sensor.reset_input_buffer()
self._sensor.reset_output_buffer()

self.handshake_block = r'\x11\s{12}0'

# Set up a queue for readings
Expand Down Expand Up @@ -80,6 +83,8 @@ def connect(self, raise_exceptions: bool = True) -> bool:
True if connected, False otherwise.
"""
try:
self._sensor.is_open or self._sensor.open()

# Initialize and get static values.
self.name = self.query(WeatherCommand.GET_INTERNAL_NAME)
self.firmware = self.query(WeatherCommand.GET_FIRMWARE)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def test_bad_port():
with pytest.raises(Exception):
CloudSensor(connect=False)

# Should raise an exception
with pytest.raises(Exception):
CloudSensor(connect=True)

def test_connect_loop():
with pytest.raises(Exception):
Expand Down

0 comments on commit e8a4952

Please sign in to comment.