Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak sensor value setter to validate dtype #98

Merged
merged 14 commits into from
Sep 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/aiokatcp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def __init__(
self.stype = sensor_type
type_info = core.get_type(sensor_type)
self.type_name = type_info.name
self._core_type = type_info.type_
self._classic_observers: Set[ClassicObserver[_T]] = set()
self._delta_observers: Set[DeltaObserver[_T]] = set()
self.name = name
Expand Down Expand Up @@ -202,6 +203,9 @@ def set_value(
) -> None:
"""Set the current value of the sensor.

Also check if the incoming value dtype is compatible with the core dtype
of this sensor.
bmerry marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
value
Expand All @@ -214,7 +218,18 @@ def set_value(
timestamp
The time at which the sensor value was determined (seconds).
If not given, it defaults to :func:`time.time`.

Raises
------
TypeError
If the incoming `value` type is not compatible with the sensor's
core type.
"""
if not issubclass(type(value), self._core_type):
ludwigschwardt marked this conversation as resolved.
Show resolved Hide resolved
bmerry marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError(
f"Value type {type(value)} is not compatible with Sensor type "
f"{self.stype} with core type {self._core_type}"
)
if timestamp is None:
timestamp = time.time()
if status is None:
Expand Down
Loading