Skip to main content

pylint Coverage PyPI PyPI - Python Version PyPI - Status License: GPL v3 code style

TinkerforgeAsync

This is a reimplementation of the Tinkerforge Python bindings (original Python bindings) using Python 3 asyncio. The original bindings used threads to manage the blocking operations. A much cleaner implementation is possible using the await syntax from asyncio.

Note: This API implementation is not an official Tinkerforge implementation. I am in no way affiliated with the Tinkerforge GmbH. Use at your own risk. If you find any bugs, please report them.

The library is fully type-hinted.

Supported Bricks/Bricklets

Brick Supported Tested
DC (discontinued) ✔️
ESP32 ✔️
ESP32 Ethernet ✔️
HAT ✔️
HAT Zero ✔️
IMU (discontinued) ✔️
IMU 2.0 ✔️
Master ✔️ ✔️
RED ✔️
Servo (discontinued) ✔️
Silent Stepper ✔️
Stepper (discontinued) ✔️
Bricklet Supported Tested
Accelerometer (discontinued) ✔️
Accelerometer 2.0 ✔️
Air Quality ✔️
Ambient Light (discontinued) ✔️
Ambient Light 2.0 (discontinued) ✔️ ✔️
Ambient Light 3.0 ✔️ ✔️
Analog In (discontinued) ✔️ ✔️
Analog In 2.0 (discontinued) ✔️
Analog In 3.0 ✔️
Analog Out (discontinued) ✔️
Analog Out 2.0 (discontinued) ✔️
Analog Out 3.0 ✔️
Barometer ✔️ ✔️
Barometer 2.0 ✔️ ✔️
CAN ✔️
CAN 2.0 ✔️
CO2 (discontinued) ✔️
CO2 2.0 ✔️
Color ✔️
Color 2.0 ✔️
Compass ✔️
Current12 (discontinued) ✔️
Current25 (discontinued) ✔️
DC 2.0 ✔️
Distance IR ✔️
Distance IR 2.0 ✔️
Distance US (discontinued) ✔️
Distance US 2.0 ✔️
DMX ✔️
Dual Button (discontinued) ✔️
Dual Button 2.0 ✔️
Dual Relay (discontinued) ✔️
Dust Detector ✔️
E-Paper 296x128 ✔️
Energy Monitor ✔️
GPS (discontinued) ✔️
GPS 2.0 ✔️
GPS 3.0 ✔️
Hall Effect ✔️
Hall Effect 2.0 ✔️
Humidity (discontinued) ✔️ ✔️
Humidity 2.0 ✔️ ✔️
IMU 3.0 ✔️
Industrial Analog Out (discontinued) ✔️
Industrial Analog Out 2.0 ✔️
Industrial Counter ✔️
Industrial Digital In 4 (discontinued) ✔️
Industrial Digital In 4 2.0 ✔️
Industrial Digital Out 4 ✔️
Industrial Digital Out 4 2.0 ✔️
Industrial Dual 0-20mA ✔️
Industrial Dual 0-20mA 2.0 ✔️ ✔️
Industrial Dual AC In ✔️
Industrial Dual AC Relay ✔️
Industrial Dual Analog In (discontinued) ✔️
Industrial Dual Analog In 2.0 ✔️ ✔️
Industrial Dual Relay ✔️
Industrial PTC ✔️ ✔️
Industrial Quad Relay (discontinued) ✔️
Industrial Quad Relay 2.0 ✔️
IO-16 ✔️ ✔️
IO-16 2.0 ✔️
IO-4 (discontinued) ✔️
IO-4 2.0 ✔️ ✔️
Isolator ✔️ ✔️
Joystick ✔️
Joystick 2.0 ✔️
Laser Range Finder (discontinued) ✔️
Laser Range Finder 2.0 ✔️
LCD 128x64 ✔️
LCD 16x2 (discontinued) ✔️
LCD 20x4 ✔️
LED Strip (discontinued) ✔️
LED Strip 2.0 ✔️
Line ✔️
Linear Poti ✔️
Linear Poti 2.0 ✔️
Load Cell (discontinued) ✔️
Load Cell 2.0 ✔️
Moisture (discontinued) ✔️ ✔️
Motion Detector (discontinued) ✔️
Motion Detector 2.0 ✔️ ✔️
Motorized Linear Poti ✔️
Multi Touch ✔️
Multi Touch 2.0 ✔️
NFC ✔️
NFC/RFID (discontinued) ✔️
OLED 128x64 (discontinued) ✔️
OLED 128x64 2.0 ✔️
OLED 64x48 ✔️
One Wire ✔️
Outdoor Weather ✔️
Particulate Matter ✔️
Performance DC ✔️
Piezo Buzzer (discontinued) ✔️
Piezo Speaker ✔️
Piezo Speaker 2.0 ✔️
PTC (discontinued) ✔️ ✔️
PTC 2.0 (discontinued) ✔️ ✔️
Real-Time Clock ✔️
Real-Time Clock 2.0 ✔️
Remote Switch (discontinued) ✔️
Remote Switch 2.0 ✔️
RGB LED (discontinued) ✔️
RGB LED 2.0 ✔️
RGB LED Button ✔️
RGB LED Matrix (discontinued) ✔️
Rotary Encoder (discontinued) ✔️
Rotary Encoder 2.0 ✔️
Rotary Poti ✔️
Rotary Poti 2.0 ✔️
RS232 ✔️
RS232 2.0 ✔️ ✔️
RS485 ✔️
Segment Display 4x7 ✔️ ✔️
Segment Display 4x7 2.0 ✔️ ✔️
Servo 2.0 ✔️
Silent Stepper 2.0 ✔️
Solid State Relay (discontinued) ✔️
Solid State Relay 2.0 ✔️
Sound Intensity ✔️
Sound Pressure Level ✔️
Temperature ✔️ ✔️
Temperature 2.0 ✔️ ✔️
Temperature IR (discontinued) ✔️
Temperature IR 2.0 ✔️
Thermal Imaging ✔️
Thermocouple (discontinued) ✔️
Thermocouple 2.0 ✔️ ✔️
Tilt ✔️
UV Light (discontinued) ✔️
UV Light 2.0 ✔️
Voltage (discontinued) ✔️
Voltage/Current (discontinued) ✔️
Voltage/Current 2.0 ✔️
XMC1400 Breakout ✔️

Tested means tested with the hardware. The other devices are tested against the protocol only, see below. Devices marked (discontinued) are no longer sold by Tinkerforge.

Generated Bricks and Bricklets

The modules of the Bricks and Bricklets without a check mark in the Tested column were generated from the Tinkerforge API definitions (CC0 licensed) and follow the same conventions as the hand-written modules:

  • All values are converted to SI units and returned as Decimal, e.g. V, A, W, m, s, K, Pa, kg, T and m/s². Energy is returned in J, speeds in m/s and accelerations given in g are converted to m/s². Angles stay in degrees, percentages in percent and dimensionless values (ppm, dB, UV index) as they are. Counts (bytes, steps, baud) are returned as int.
  • Every function documents the units of its parameters and results in a Units line of its docstring. The rest of the docstrings is taken from the official documentation, so numbers in the descriptions can refer to raw values.
  • Constants are Enums, invalid arguments raise a ValueError before anything is sent.
  • Functions, that transfer more data than fits into a single packet (e.g. write_pixels() of the LCD 128x64 Bricklet or write() of the RS485 Bricklet), have a high-level version, that splits the data into chunks. The *_low_level() functions are available, too. Streamed callbacks are reassembled before they are yielded by read_events().
  • get_value(), set_callback_configuration() and get_callback_configuration() work with the secondary ids (sids) listed in the CHANNELS table of each device.

The function ids and payload formats are checked against the official Python bindings by the tests.

Documentation

The documentation is currently work in progress. The full documentation will be moved to https://patrickbaus.github.io/tinkerforge_async/. Below you can find the current state of the documentation. I use the Numpydoc style for documentation and Sphinx for compiling it.

Setup

To install the library in a virtual environment (always use venvs with every project):

python3 -m venv env  # virtual environment, optional
source env/bin/activate  # only if the virtual environment is used
pip install tinkerforge-async

Changes made to the API

Some design choices of the original Tinkerforge API are overly complex. I therefore replaced them with a simpler and more intuitive approach. A list of things that were changed can be found below:

Design Changes

  • Only Python 3 is supported (3.12+)

  • Replaced threads with an async event loop

  • Completely rewritten how responses from bricks/bricklets work. All setters now have a response_expected parameter, which is set to True by default. If there is an error when calling the function, it will then raise an exception - either an AttributeError if the function is unknown, or a ValueError if one or more parameters are invalid.

    Old style:

    bricklet = BrickletHumidity(UID, ipcon)
    bricklet.set_response_expected(
        BrickletHumidity.FUNCTION_SET_HUMIDITY_CALLBACK_PERIOD, True
    )
    bricklet.set_humidity_callback_period(1000)
    

    New style:

    bricklet = BrickletHumidity(UID, ipcon)
    await bricklet.set_humidity_callback_period(
        1, response_expected=True
    )  # Raises an exception if unsuccessful
    
  • Replaced all constants with Enums and enforced their use using assertions. This will allow beginners to spot their mistakes earlier and make the code more readable, including any debug output statements.

    Old style:

    class BrickletHumidity(Device):
        FUNCTION_GET_HUMIDITY = 1
        FUNCTION_GET_ANALOG_VALUE = 2
        FUNCTION_SET_HUMIDITY_CALLBACK_PERIOD = 3
        FUNCTION_GET_HUMIDITY_CALLBACK_PERIOD = 4
        FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD = 5
        FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD = 6
        FUNCTION_SET_HUMIDITY_CALLBACK_THRESHOLD = 7
        FUNCTION_GET_HUMIDITY_CALLBACK_THRESHOLD = 8
        FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD = 9
        FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD = 10
        FUNCTION_SET_DEBOUNCE_PERIOD = 11
        FUNCTION_GET_DEBOUNCE_PERIOD = 12
        FUNCTION_GET_IDENTITY = 255
    

    New style:

    class BrickletHumidity(Device):
        @unique
        class FunctionID(Enum):
            GET_HUMIDITY = 1
            GET_ANALOG_VALUE = 2
            SET_HUMIDITY_CALLBACK_PERIOD = 3
            GET_HUMIDITY_CALLBACK_PERIOD = 4
            SET_ANALOG_VALUE_CALLBACK_PERIOD = 5
            GET_ANALOG_VALUE_CALLBACK_PERIOD = 6
            SET_HUMIDITY_CALLBACK_THRESHOLD = 7
            GET_HUMIDITY_CALLBACK_THRESHOLD = 8
            SET_ANALOG_VALUE_CALLBACK_THRESHOLD = 9
            GET_ANALOG_VALUE_CALLBACK_THRESHOLD = 10
            SET_DEBOUNCE_PERIOD = 11
            GET_DEBOUNCE_PERIOD = 12
    
  • Moved from base58 encoded uids to integers.

  • Moved from callbacks to queues in order to keep users out of the callback hell. It makes the code style more readable when using the await syntax anyway.

  • Payloads will now be decoded by the Device object and no longer by the ip_connection. This makes the code a lot more readable. To do so, the payload and decoded header will be handed to the device. It will then decode it, if possible, and pass it on to the queue.

  • If physical quantities are measured we will now return standard SI units, not some unexpected stuff like centi °C (Temperature Bricklet). To preserve the precision the Decimal package is used. This includes temperatures, which are given in K, not °C. Durations (callback periods, debounce periods, monoflop times, ...) are given in s, not ms, like the delay of asyncio.sleep(). Durations that are passed to the library can be int, float or Decimal, durations returned by the library are Decimal.

  • All callbacks now contain a timestamp (Unix timestamp) and the device object.

    Example:

     Event(timestamp=1658756708.6839857, sender=Temperature Bricklet 2.0 with uid 161085 connected at IPConnectionAsync(192.168.1.164:4223), sid=0, function_id=CallbackID.TEMPERATURE, payload=305.46)
    
  • Added the concept of secondary ids (sid). By default, the secondary id is 0. If there is more than one sensor on the bricklet, they will have a sid value of 1,2, etc. This is especially useful for sensors like the Industrial Dual Analog In Bricklet 2.0, which returns its two channels via the same callback.

  • New functions:

    BrickMaster.set_wpa_enterprise_username(username): Set the WPA enterprise username without calling BrickMaster.set_wifi_certificate(). Takes a string instead of an array of int. BrickMaster.set_wpa_enterprise_password(password): Set the WPA enterprise password without calling BrickMaster.set_wifi_certificate(). Takes a string instead of an array of int. BrickMaster.get_wpa_enterprise_username(): Get the WPA enterprise password without calling BrickMaster.get_wifi_certificate(). Also returns a string instead of an array of int. BrickMaster.get_wpa_enterprise_password(): Get the WPA enterprise password without calling BrickMaster.get_wifi_certificate(). Also returns a string instead of an array of int.

  • IP Connection

    • IPConnection.authenticate(secret): removed. This can now be done through connect()
    • IPConnection.set_timeout/IPConnection.get_timeout: Replaced by a property
    • IPConnection.register_callback(callback_id, function): Replaced by register_event_queue()
    • IPConnection.connect(host, port=4223, authentication_secret=''): If authentication_secret is not empty, try to authenticate.
  • IO-4 Bricklet 2.0

    • BrickletIO4V2.set_pwm_configuration() will now take the frequency in units of Hz and the duty cycle is normalized to 1, so it will take a float from [0...1].
    • BrickletIO4V2.get_pwm_configuration() will return the frequency in units of Hz and the duty cycle is normalized to 1.
    • BrickletIO4V2.set_edge_count_callback_configuration() sets the callback configuration for the edge counter callback. Its secondary ids are in [5...8] for channels [0...3].
    • BrickletIO4V2.get_edge_count_callback_configuration() returns the callback configuration for the edge counter callback.
  • Master Brick

    • BrickMaster.set_wifi_configuration()/BrickMaster.get_wifi_configuration() will take/return all ips in natural order
    • BrickMaster.set_ethernet_configuration()/BrickMaster.get_ethernet_configuration() will take/return all ips in natural order
    • BrickMaster.write_wifi2_serial_port() will only accept a bytestring and no length argument. The length will be automatically determined from the string.
    • BrickMaster.set_wifi2_status_led(enabled) added. This allows setting the status led by value instead of calling enable_wifi2_status_led/disable_wifi2_status_led
  • PTC Bricklet

    • BrickletPtc() takes an additional parameter to define the type of sensor. The options are BrickletPtc.SensorType.PT_100 and BrickletPtc.SensorType.PT_1000. This only determines the resistance returned by the bricklet. The default is BrickletPtc.SensorType.PT_100.
    • BrickletPtc.sensor_type getter and setter to change the type of sensor used.
  • PTC Bricklet 2.0

    • BrickletPtcV2() takes an additional parameter to define the type of sensor. The options are BrickletPtc.SensorType.PT_100 and BrickletPtc.SensorType.PT_1000. This only determines the resistance returned by the bricklet. The default is BrickletPtc.SensorType.PT_100.
    • BrickletPtcV2.sensor_type getter and setter to change the type of sensor used.
  • Thermocouple Bricklet 2.0

    • BrickletThermocoupleV2() takes an additional parameter to define the type of sensor. The options are of type BrickletThermocoupleV2.SensorType. The default is BrickletPtc.SensorType.TYPE_K.
    • BrickletThermocoupleV2.sensor_type getter and setter to change the type of sensor used.
  • Segment Display 4x7 Bricklet 2.0

    • BrickletSegmentDisplay4x7V2.set_segments() takes a list/tuple of 4 int instead of digit0, digit1, digit2, digit3. This is the same API as the older Segment Display 4x7 Bricklet.

Setup

There are currently no packages available at the PyPi repository. To install the module, clone the repository and run:

python3 -m venv env  # virtual environment, optional
source env/bin/activate  # only if the virtual environment is used
python3 setup.py install

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

The version is not stored in the source code. It is taken from the git tag by setuptools-scm when the package is built. To release a new version, publish a GitHub release with a new tag, e.g. 1.9.0. This builds the package as version 1.9.0 and uploads it to PyPI. Commits after a tag get a development version like 1.9.1.dev3.

For development, install the package in editable mode, which also generates tinkerforge_async/_version.py:

pip install -e .[dev]

Authors

License

This project is licensed under the GPL v3 license - see the LICENSE file for details

Metadata

Release files for tinkerforge-async 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinkerforge-async 2.0.0
File Size Uploaded Provenance Actions
tinkerforge_async-2.0.0.tar.gz 622.5 kB Without provenance Details for tinkerforge_async-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tinkerforge-async 2.0.0
File Interpreter ABI Platform Actions
tinkerforge_async-2.0.0-py3-none-any.whl Python 3 none any Details for tinkerforge_async-2.0.0-py3-none-any.whl

Total release size: 1.3 MB

Release files / tinkerforge_async-2.0.0.tar.gz

Download URL tinkerforge_async-2.0.0.tar.gz
Size 622.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f25308233ca8803566c81f269cac8c3a53884fa956c5157bbd16218a60925342
BLAKE2b-256 checksum
How to use checksums
9edc9455ae7d0c6d3ebf7ce0e8aa2f793f1e2953be11cc62c6ed8b0616574a61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / tinkerforge_async-2.0.0-py3-none-any.whl

Download URL tinkerforge_async-2.0.0-py3-none-any.whl
Size 664.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8921e476fe6762d7dbf42256a0c2ff82a61217dc0ca6779e54d448360dcae140
BLAKE2b-256 checksum
How to use checksums
db9e36e00b824be162c466c69a5700e8965fe03bcf6d735e1b68baabbe8b582f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for tinkerforge-async 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page