Client¶
LD2410 is the asyncio serial client from aio_ld2410 to handle LD2410
devices.
Create and Connect¶
- class aio_ld2410.LD2410[source]
- __init__(device: str, *, baud_rate: int = 256000, command_timeout: float | None = 2.0, read_bufsize: int | None = None) None[source]¶
Create a new async client for the provided LD2410 device.
Important
The command timeout affects all command requests when enabled. If the device doesn’t reply within this period, an
asyncio.TimoutErroris raised (which is equivalent to aTimeoutError) on Python 3.10+.- Parameters:
device (
str) – path to the device to use.baud_rate (
int, default:256000) – serial baud rate to use.command_timeout (
float|None, default:2.0) – how long to wait for a command reply (in seconds),Noneto disable.read_bufsize (
int|None, default:None) – max buffer size used by the underlyingasyncio.StreamReader.
- async __aenter__() Self[source]¶
Enter the device’s context, open the device.
This initializes the serial link and creates the reader
asyncio.Task.This method, along with
__aexit__()are called with the following syntax:async with LD2410('/dev/ttyUSB0'): pass
- Raises:
RuntimeError – when the device is already in use (
entered).- Returns:
- async __aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) bool[source]¶
Exit the device’s context and close the serial link.
The reader task is also canceled.
Properties¶
Configuration mode¶
- class aio_ld2410.LD2410[source]
- configure() AsyncIterator[ConfigModeStatus]¶
Enter the configuration mode.
As stated on the LD2410 documentation, no reports are generated when the configuration mode is active but the device now accepts configuration commands.
Notes
This context is protected with an
asyncio.Lock.This context absorbs
ModuleRestartedError(seerestart_module())
USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as dev: async with dev.configure(): # Some configuration commands
- Yields:
Device’s protocol information.
This is the standard reply for command
CONFIG_ENABLE.You most likely don’t need this returned value.
- Returns:
AsyncIterator[ConfigModeStatus] – An asynchronous iterator (seel YIELDS).
Getters¶
- class aio_ld2410.LD2410[source]
- async get_bluetooth_address() bytes[source]¶
Get the device’s bluetooth mac address.
USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as dev: async with dev.configure(): addr = await dev.get_bluetooth_address() print('MAC address:', addr.hex(':'))
- async get_distance_resolution() int[source]¶
Get the gate distance resolution (in centimeter).
Caution
This command may not be available on your variant or with your firmware.
See also
The internal
ResolutionIndexfor a list of available resolutions.- Raises:
CommandContextError – when called outside of the configuration context.
CommandReplyError – when the device returns an unknown resolution index.
CommandStatusError – when the device replies with a failed status.
- Returns:
int– The distance resolution in centimeters.
- async get_firmware_version() FirmwareVersion[source]¶
Get the device’s firmware version.
USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as device: async with device.configure(): ver = await device.get_firmware_version() print(f'[+] Running with firmware v{ver}')
- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- Returns:
FirmwareVersion– The firmware version structure.
- async get_light_control() LightControlStatus[source]¶
Get the light controls parameters for
OUTpin.This gets the specific configuration used to control the
OUTpin status with the integrated photo sensor.Caution
This command may not be available on your variant or with your firmware.
- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- Returns:
LightControlStatus– The status of the light configuration.
- async get_parameters() ParametersStatus[source]¶
Get the standard configuration parameters.
- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- Returns:
ParametersStatus– The currently applied standard parameters.
Setters¶
- class aio_ld2410.LD2410[source]
- async reset_to_factory() None[source]¶
Reset the device to its factory settings.
Tell the device to reset all its parameters to factory settings.
- Return type:
Important
This command requires a module restart to be effective!
Hint
In factory settings, you get the following parameters:
Max moving distance gate
8
Max static distance gate
8
No-one duration
5 sec
Serial port baud rate
256000 Hz
Bluetooth mode
enabled
Bluetooth password
HiLink
Distance resolution
75 cm
Light control
disabled
Light threshold
128
Light default
LOW
Gate
Moving threshold
Static threshold
0
50%
N/A
1
50%
N/A
2
40%
40%
3
30%
40%
4
20%
30%
5
15%
30%
6
15%
20%
7
15%
20%
8
15%
20%
- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- async restart_module(*, close_config_context: bool = False) None[source]¶
Restart the module immediately.
On my tests it takes at least 1100ms for the module to be responsive again.
Important
If you chose close the current configuration context using
close_config_contextyou will have to exit the context by yourself before you can issue new commands.- Keyword Arguments:
close_config_context – close the surrounding configuration context by raising a
ModuleRestartedError(seeconfigure()).- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
ModuleRestartedError – when
close_config_contextis True (do not catch it).
- Return type:
- async set_baud_rate(baud_rate: int) None[source]¶
Set the serial port baud rate.
Important
This command requires a module restart to be effective!
- Parameters:
baud_rate (
int) – the baud rate you want to apply (seeBaudRateIndex).- Return type:
See also
The internal
BaudRateIndexfor a list of available baud rates.- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when the provided baud rate is not suitable.
CommandStatusError – when the device replies with a failed status.
- async set_bluetooth_mode(enabled: bool) None[source]¶
Enable of disable bluetooth mode.
Important
This command requires a module restart to be effective!
- Parameters:
enabled (
bool) – whether bluetooth should be enabled on the device.- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- Return type:
- async set_bluetooth_password(password: str) None[source]¶
Set the device’s bluetooth password.
Caution
This command may not be available on your variant or with your firmware.
- Parameters:
password (
str) – must have less than 7 ASCII characters.- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when the password is not ASCII or has more than 6 chars.
CommandStatusError – when the device replies with a failed status.
- Return type:
- async set_distance_resolution(resolution: int) None[source]¶
Set the gate distance resolution (in centimeter).
Important
This command requires a module restart to be effective!
Caution
This command seems to be available for a few devices / firmwares.
- Parameters:
resolution (
int) – per-gate distance (the only valid values are 20 and 75 cm).- Return type:
See also
The internal
ResolutionIndexfor a list of available resolutions.- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when the provided resolution is not suitable.
CommandStatusError – when the device replies with a failed status.
- async set_engineering_mode(enabled: bool) None[source]¶
Enable or disable engineering reports.
Caution
The engineering mode is lost after a device restart.
- Parameters:
enabled (
bool) – whetherReportStatusshould contain an advanced report.- Return type:
See also
ReportStatusfor the complete description of reports.ReportEngineeringStatusfor the engineering part.
- Raises:
CommandContextError – when called outside of the configuration context.
CommandStatusError – when the device replies with a failed status.
- async set_gate_sensitivity(**kwargs: Unpack[GateSensitivityConfig]) None[source]¶
Set gate sensitivities.
This command is used to configure gate sensitivity to one or to all gate.
- Return type:
Hint
See
GateSensitivityConfigfor keyword arguments.Tip
These parameters apply immediately and are persistent across module restart.
distance_gatekeyword argument can be set to0xFFFFto broadcast to all gates.
USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as dev: async with dev.configure(): # Set sensitivities for gate 4. await dev.set_gate_sensitivity( distance_gate=4, moving_threshold=25, static_threshold=20, )
- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when a mandatory keyword argument is missing.
CommandStatusError – when the device replies with a failed status.
- async set_light_control(**kwargs: Unpack[LightControlConfig]) None[source]¶
Set the light controls parameters for
OUTpin.This sets the specific configuration used to control the
OUTpin status with the integrated photo sensor.- Return type:
Caution
This command may not be available on your variant or with your firmware.
Hint
See
LightControlConfigfor keyword arguments.USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as dev: async with dev.configure(): await dev.set_light_control( control=LightControl.BELOW, default=OutPinLevel.LOW, threshold=120, )
- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when a mandatory keyword argument is missing.
CommandStatusError – when the device replies with a failed status.
- async set_parameters(**kwargs: Unpack[ParametersConfig]) None[source]¶
Set the standard configuration parameters.
This method only sets basic configuration parameters.
- Return type:
Hint
See
ParametersConfigfor keyword arguments.Tip
These parameters apply immediately and are persistent across module restart.
USE EXAMPLE:
async with LD2410('/dev/ttyUSB0') as dev: async with dev.configure(): await dev.set_parameters( moving_max_distance_gate=7, static_max_distance_gate=7, presence_timeout=5, )
See also
set_gate_sensitivity()to set additional parameters.- Raises:
CommandContextError – when called outside of the configuration context.
CommandParamError – when a mandatory keyword argument is missing.
CommandStatusError – when the device replies with a failed status.
Reports¶
- class aio_ld2410.LD2410[source]
- get_last_report() ReportStatus | None[source]¶
Get the latest report received from the device, if any.
Note
This report can be very outdated if you spent too much time in configuration mode.
Tip
This method does not way for anything and it not asynchronous.
- Returns:
ReportStatus|None– The last report we received from the device.
- async get_next_report() ReportStatus[source]¶
Wait and get the next available report.
Caution
Must be called outside of the configuration context as no report is being generated in configuration mode.
- Returns:
ReportStatus– The next report we just received from the device (if any).
- async get_reports() AsyncIterator[ReportStatus][source]¶
Get reports as they arrive with an asynchronous iterator.
This is just a simple loop around
get_next_report().Caution
Must be called outside of the configuration context as no report is being generated in configuration mode.
- Yields:
An asynchronous iterator for
ReportStatus.- Returns:
AsyncIterator[ReportStatus] – An asynchronous iterator (seel YIELDS).