Every PLESTY device must inherit from BaseDeviceSyncModel and implement its mandatory methods. This ensures a consistent API across all devices.
Import
from plesty.lib.device import BaseDeviceSyncModel
Constructor
The BaseDeviceSyncModel constructor takes the following parameters:
def __init__(
self,
id: str,
op_schema=None,
param_schema=None,
op_solver=None,
cmd_solver=None,
**kwargs,
)
id— unique device identifier (combined with the host IP at runtime)op_schema— path to the operation schema JSON fileparam_schema— path to the parameter schema JSON fileop_solver— custom operation solver (defaults to the schema-driven solver)cmd_solver— custom command solver (defaults to the schema-driven solver)
Construction is side-effect-free — no hardware is touched until connect().
Mandatory methods
These methods must be implemented in every device class:
init(main=None)
Initialize the device hardware and internal state. Called after construction.
def init(self, main=None) -> None:
# Device-specific initialization
pass
connect() -> bool
Establish a connection to the hardware. Return True on success, False on failure.
def connect(self) -> bool:
# Open connection to hardware
self._connected = True
return True
disconnect()
Close the connection to the hardware and release resources.
def disconnect(self) -> None:
# Close connection to hardware
self._connected = False
_write_(key, value)
Write a value to a device parameter identified by key.
def _write_(self, key: str, value: object) -> None:
# Write value to hardware
...
_query_(key) -> object
Read a value from a device parameter identified by key. Return the value.
def _query_(self, key: str) -> object:
# Read value from hardware
return ...
identity() -> str
Return a string identifying the device (model, serial number, etc.).
def identity(self) -> str:
return "MyDevice v1.0 SN:12345"
check_errors()
Check the device for errors and raise if any are found.
def check_errors(self) -> None:
# Check hardware for errors
pass
check_operatability() -> bool
Return True if the device is ready to operate.
def check_operatability(self) -> bool:
return self._connected
Optional methods
These methods are not required but are commonly implemented:
query_range(key) -> list
Return the valid range of values for a parameter as [min, max].
def query_range(self, key: str) -> list:
return [0.0, 100.0] # min, max
reset()
Reset the device to its default state.
def reset(self) -> None:
self._write_("mode", "default")
clear()
Clear any buffered data or pending state on the device.
def clear(self) -> None:
...
Device lifecycle
__init__() → init() → connect() → [read/write operations] → disconnect()
- Construct — create the device instance (no hardware access)
- Initialize — set up internal state
- Connect — establish hardware connection
- Operate — read and write parameters via
_write_/_query_ - Disconnect — close the connection and release resources
Next steps
- Define your device schemas
- Write the full implementation