A Python library for signals, Home Assistant entities and commands over serial or TCP, using the blaeck protocol. Works with hosts such as Loggbok.
With blaeckpy, a script can:
- Log signals: values such as a temperature or a counter.
- Show up in Home Assistant: with controls, sensors and events.
- Take commands: text such as
<SwitchLED,1>.
blaeckpy replaces blaecktcpy, which stays on PyPI for protocol v1.
Installation
pip install blaeckpy
Python 3.12 or later. blaeckpy itself needs nothing else; a serial port needs pyserial.
A first script
Two simulated readings, logged at the interval the host chooses.
TCP
import random
import socket
from blaeckpy import Blaeck
device = Blaeck("Weather Station")
temperature = device.add_signal("Temperature", float, unit="°C")
pressure = device.add_signal("Pressure", int, unit="hPa")
server = socket.create_server(("", 2323))
device.begin(server)
while True:
temperature.value = 20.0 + random.randint(0, 100) / 10
pressure.value = 1000 + random.randint(0, 30)
device.tick(timeout=0.1)
Connect the host to this computer on TCP port 2323.
Serial
import serial # pip install pyserial
port = serial.Serial("/dev/ttyUSB0", 115200) # COM3 on Windows
device.begin(port)
Everything else stays the same. Connect the host to the other end of the serial link at 115200 baud.
If the port goes away, a USB cable pulled say, tick() raises the port's error. A device begins
once, so to go on, start the script again.
Three calls do the work:
add_signal(...)declares a value to log. Set its.value; blaeckpy reads it whenever it sends data. The type comes from a type (float) or from a starting value (21.5), and.valuerefuses what the type can't hold.begin(...)hands blaeckpy the serial port or listening socket you opened. It stays yours to close.tick(timeout=0.1)answers commands and sends what is due, then waits up to 0.1 s for the next command or the next write that falls due, so the loop doesn't use a whole CPU core. Call it in every pass of your loop.
The host decides how often data is sent: it sends <BLAECK.DATA_START> and
<BLAECK.INTERVAL_START,1000> for one frame a second.
What a script declares
Signals, inputs, sensors, events, buttons and commands can also be declared on a sub-device.
| blaeckpy | What it is |
|---|---|
add_signal(name, float) |
A value to log; .value |
write_at_interval=ALWAYS / OFF / IfChanged(0.5), write_on_change=0.5, min_interval_ms= |
When a signal is sent |
add_number_input(name, 20.0, min=, max=, step=), add_text_input, add_switch, add_select(name, 0, options=[...]) |
Controls Home Assistant lets the user operate; .value, @input.on_change |
add_sensor(name, signal), add_sensor(name, 21.5), add_sensor(name, float, get=fn), add_sensor(name, 0, options=[...]) |
Values Home Assistant shows but the user can't set |
add_event(name, [types]), event.write(type) |
Occurrences, such as a doorbell |
add_button(name, fn), @device.add_button(name) |
A press with no value |
@device.on_command(name), @device.on_any_command |
Commands no host lists; parameters arrive as text |
add_device(name), mark_missing(), mark_present(), write_restarted() |
A second board shown as its own device |
unit=, icon=, device_class=, category=Category.CONFIG, ... |
How a host shows it |
sensor.icon = ..., input.set_range(0, 20), signal.write_on_change = 0.5, ... |
The same, changed while running. An entity's settings go out in a new entity list. A signal's reporting settings stay on the device. A signal's unit changes the schema hash, so a logging host stops until it reads the device list again |
@device.on_data_start, @device.on_interval_start, ... |
Following what the host switches on and off; on_interval_start gets the interval in ms, the others get nothing |
@device.on_connect, @device.on_disconnect |
TCP connections, with (client_no, address) |
device.terminal.print(...) |
Text for whoever looks into the running device |
set_timestamp_mode(TimestampMode.MICROS) |
Timestamps in data frames |
clear_all_signals(), clear_all_controls(), ... |
Declaring a new set while running |
A few things to know:
- Errors at registration raise. A bad name, a range with
minabovemaxor an option list with a blank entry raisesValueErrororTypeErrorwhere it was written, instead of being reported on a debug stream. A button or command left without a function raises atbegin(): until then it could still be a decorator. - A callback that raises doesn't stop the device. The exception is logged with its traceback
on the
blaeckpylogger,tick()goes on, and the host still gets its answer. While aclock=fails, no data goes out; a signal that changed goes out once it works again. - One thread. A device and what it made are used from the thread that calls
tick(). To feed values from another thread, hand them over, with aqueue.Queuesay, and set them in the loop. - Time is in seconds unless a name says otherwise:
tick(timeout=0.1), butmin_interval_ms=1000. Timestamps are the exception:tick(timestamp=),write_all(timestamp)andclock=are in microseconds, as they are on the wire. - The device list names blaeckpy and its own version, so Loggbok and error reports show which library a device runs.
- A command may be up to 1023 characters: room for a 255-byte text, the most Home Assistant holds, even with every byte percent-encoded. The device list tells a host the limit.
Status page and terminal output
begin() prints what the device is doing on the terminal, with colour where the terminal shows
it (NO_COLOR turns it off):
blaeckpy v1.0.0: 'Bench' listening on all addresses (0.0.0.0)
TCP 127.0.0.1:2323
TCP 192.168.1.20:2323
Status page:
http://127.0.0.1:8080
http://192.168.1.20:8080
Client 0 connected: 192.168.1.20:51234
These lines go through the blaeckpy logger. Once a script configures logging itself, with
logging.basicConfig() say, they go to its handlers instead. blaeckpy logs from level INFO
whatever basicConfig() was given, so the level is set on its logger:
logging.getLogger("blaeckpy").setLevel(logging.WARNING) # only warnings and errors
The status page shows the device in a browser: its connection, the clients connected, and each
device's signals and entities with their current values, refreshed every second. It needs no
internet. It runs on port 8080, or on a free port if 8080 is taken, and the terminal says
which. On TCP it listens on the same address as the device. On serial it stays on this computer,
unless status_host opens it to others, for a headless computer such as a Raspberry Pi. It has
no login, so anyone who can reach it can read it. The page only shows; it changes nothing on the
device. A sensor's get= is read for the page only while the page is open, and a password input
shows as ••••••••.
device.begin(server, status_port=8081) # another port
device.begin(server, status_port=None) # no status page
device.begin(port, status_host="0.0.0.0") # a serial device's page, open to other computers
Examples
The examples are in examples/. Each
one is a single file that listens on TCP port 2323; a comment shows the line to use for serial
instead. Run one and point Loggbok at it.
| Example | What it teaches |
|---|---|
| basic.py | The smallest script that logs two values |
| signals.py | Numeric, boolean and text signals, with units |
| commands.py | Plain commands, and a function that sees every command |
| controls.py | A number, text, switch, select and buttons |
| sensors.py | A number, bool, function, index and text sensor |
| events.py | Declaring and reporting occurrences |
| write_modes.py | Interval, on-change and explicit signal writes |
| sub_devices.py | A second device shown below the board, going missing and restarting |
| waveform_generator.py | A waveform driven from a Home Assistant dashboard |
Testing
Every frame is tested against recordings. blaeck records every frame it sends while playing a set
of scenarios (golden scenarios);
scripts/copy_golden.py copies them here, and blaeckpy must play each scenario through its public
API and send the same bytes. Real-socket tests cover TCP, and every example is run against a host.
pip install -e '.[test]'
pytest
Licence
MIT, see LICENSE. The status page uses Pico CSS, MIT, see pico-LICENSE.md.
Metadata
Release files for blaeckpy 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| blaeckpy-1.0.0.tar.gz | 117.5 kB | Without provenance | Details for blaeckpy-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| blaeckpy-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for blaeckpy-1.0.0-py3-none-any.whl |
Total release size: 190.5 kB
Release files / blaeckpy-1.0.0.tar.gz
| Download URL | blaeckpy-1.0.0.tar.gz |
|---|---|
| Size | 117.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ee57b90bcd650e24559b3281512f0e8560a59c5d24e30683f150a8aec88b26eb
|
|
BLAKE2b-256 checksum How to use checksums |
430a592c85cadd37f83057e6b3c3802d71aa1ab54bc91a8c0bed6b07c12749bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.0
|
Release files / blaeckpy-1.0.0-py3-none-any.whl
| Download URL | blaeckpy-1.0.0-py3-none-any.whl |
|---|---|
| Size | 73.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2251e9e53489b6e7d5bee5919f0cb2179ece98281c6a643ccb294532477930aa
|
|
BLAKE2b-256 checksum How to use checksums |
c8051da5b2d4753ac7bcedf8f873e5eb3c4045ee286bbfffc681502298344020
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.0
|
Security for blaeckpy 1.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.