Skip to content

Commit

Permalink
SensorsBase: Remove unneeded start_polling argument
Browse files Browse the repository at this point in the history
All the callers of SensorsBase.__init__() now call it with start_polling=False, so we can just remove the option.
  • Loading branch information
alexdewar committed Dec 9, 2024
1 parent 3a2cc7b commit d7251d2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion finesse/hardware/plugins/sensors/decades.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self._params: list[DecadesParameter]
"""Parameters returned by the server."""

super().__init__(poll_interval, start_polling=False)
super().__init__(poll_interval)

# Obtain full parameter list in order to parse received data
self.obtain_parameter_list(
Expand Down
2 changes: 1 addition & 1 deletion finesse/hardware/plugins/sensors/em27_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, url: str, poll_interval: float = float("nan")) -> None:
self._requester = HTTPRequester()
self._connected = False

super().__init__(poll_interval, start_polling=False)
super().__init__(poll_interval)

self.request_readings()

Expand Down
8 changes: 1 addition & 7 deletions finesse/hardware/plugins/sensors/sensors_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,19 @@ class SensorsBase(
calling send_readings_message() with new sensor values (at some point).
"""

def __init__(
self, poll_interval: float = float("nan"), start_polling: bool = True
) -> None:
def __init__(self, poll_interval: float = float("nan")) -> None:
"""Create a new SensorsBase.
Args:
poll_interval: How often to poll the sensor device (seconds). If set to nan,
the device will only be polled once on device open
start_polling: Whether to start polling the device immediately
"""
super().__init__()

self._poll_timer = QTimer()
self._poll_timer.timeout.connect(self.request_readings)
self._poll_interval = poll_interval

if start_polling:
self.start_polling()

def start_polling(self) -> None:
"""Begin polling the device."""
if not isnan(self._poll_interval):
Expand Down
25 changes: 9 additions & 16 deletions tests/hardware/plugins/sensors/test_sensors_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,27 @@ def device(timer_mock: Mock) -> SensorsBase:


class _MockSensorsDevice(SensorsBase, description="Mock sensors device"):
def __init__(self, poll_interval: float = float("nan"), start_polling=True):
def __init__(self, poll_interval: float = float("nan")):
self.request_readings_mock = MagicMock()
super().__init__(poll_interval, start_polling)
super().__init__(poll_interval)

def request_readings(self) -> None:
self.request_readings_mock()


@pytest.mark.parametrize("start_polling", (False, True))
@patch("finesse.hardware.plugins.sensors.sensors_base.QTimer")
def test_init(timer_mock: Mock, start_polling: bool) -> None:
def test_init(timer_mock: Mock) -> None:
"""Test for the constructor."""
with patch.object(_MockSensorsDevice, "start_polling") as start_mock:
device = _MockSensorsDevice(1.0, start_polling)
assert device._poll_interval == 1.0
timer = cast(Mock, device._poll_timer)
timer.timeout.connect.assert_called_once_with(device.request_readings)

if start_polling:
start_mock.assert_called_once_with()
else:
start_mock.assert_not_called()
device = _MockSensorsDevice(1.0)
assert device._poll_interval == 1.0
timer = cast(Mock, device._poll_timer)
timer.timeout.connect.assert_called_once_with(device.request_readings)


@patch("finesse.hardware.plugins.sensors.sensors_base.QTimer")
def test_start_polling_oneshot(timer_mock: Mock) -> None:
"""Test the start_polling() method when polling is only done once."""
device = _MockSensorsDevice(start_polling=False)
device = _MockSensorsDevice()

device.start_polling()
timer = cast(Mock, device._poll_timer)
Expand All @@ -53,7 +46,7 @@ def test_start_polling_oneshot(timer_mock: Mock) -> None:
@patch("finesse.hardware.plugins.sensors.sensors_base.QTimer")
def test_start_polling_repeated(timer_mock: Mock) -> None:
"""Test the start_polling() method when polling is only done repeatedly."""
device = _MockSensorsDevice(1.0, start_polling=False)
device = _MockSensorsDevice(1.0)

device.start_polling()
timer = cast(Mock, device._poll_timer)
Expand Down

0 comments on commit d7251d2

Please sign in to comment.