Devices¶
cosalette device handlers — the bridge between domain logic and the MQTT framework.
Gas Counter¶
gas2mqtt.devices.gas_counter
¶
Gas counter device — stateful trigger detection and counting.
Uses an @app.device handler with a manual polling loop: 1. Reads Bz from the magnetometer at poll_interval 2. Feeds Bz into a SchmittTrigger 3. On rising edge: increments counter, optionally tracks consumption 4. Publishes state on every trigger event (not every poll) 5. Accepts inbound commands to set consumption value
State persistence
When state_file is configured, counter and consumption values
are saved after every state-publishing event and on shutdown.
On startup, saved state is restored so values survive restarts.
The trigger state is transient and not persisted.
MQTT state payload
{"counter": 42, "trigger": "CLOSED"} or with consumption tracking:
MQTT command payload (on gas2mqtt/gas_counter/set):
COUNTER_MODULUS
module-attribute
¶
Modulus for the tick counter (wraps at 2^16).
gas_counter
async
¶
Gas counter device — polls magnetometer, detects ticks.
This is a long-running device coroutine intended for registration
with @app.device("gas_counter"). It owns its polling loop,
manages a SchmittTrigger for edge detection, and optionally tracks
cumulative gas consumption.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
DeviceContext
|
Per-device context injected by cosalette. Provides MQTT publishing, shutdown-aware sleep, adapter resolution, and settings access. |
required |
store
|
DeviceStore
|
Per-device persistent store injected by cosalette. Already loaded on entry; saved by framework on shutdown. |
required |
Source code in packages/src/gas2mqtt/devices/gas_counter.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
Temperature¶
gas2mqtt.devices.temperature
¶
Temperature telemetry — PT1-filtered, calibrated sensor readings.
Reads the raw temperature from the magnetometer's built-in sensor,
applies a linear calibration (temp_scale * raw + temp_offset),
then smooths via an exponentially-weighted PT1 (first-order lag) filter.
The OnChange publish strategy ensures readings are only published
when the filtered value shifts by more than 0.05 °C, avoiding MQTT
chatter for sensor noise.
MQTT state payload
{"temperature": 22.5}
make_pt1
¶
make_pt1(settings: Gas2MqttSettings) -> Pt1Filter
Create PT1 filter from settings for temperature smoothing.
This is the init= factory: cosalette calls it once before the
handler loop and injects the returned Pt1Filter by type.
Source code in packages/src/gas2mqtt/devices/temperature.py
temperature
async
¶
temperature(
magnetometer: MagnetometerPort,
settings: Gas2MqttSettings,
pt1: Pt1Filter,
) -> dict[str, object]
Read temperature, calibrate, filter, and return state dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
magnetometer
|
MagnetometerPort
|
Sensor adapter (injected by cosalette DI). |
required |
settings
|
Gas2MqttSettings
|
Application settings with calibration coefficients. |
required |
pt1
|
Pt1Filter
|
PT1 filter instance (created by :func: |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
|
dict[str, object]
|
the framework when the |
Source code in packages/src/gas2mqtt/devices/temperature.py
Magnetometer¶
gas2mqtt.devices.magnetometer
¶
Magnetometer debug telemetry — raw 3-axis magnetic field readings.
Publishes raw Bx, By, Bz values from the QMC5883L sensor at the gas
counter's poll interval. Disabled by default — enable via the
enable_debug_device setting for calibration and troubleshooting.
MQTT state payload
{"bx": -1234, "by": 567, "bz": -4567}
magnetometer
async
¶
magnetometer(
magnetometer: MagnetometerPort,
) -> dict[str, object]
Read and return raw magnetic field values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
magnetometer
|
MagnetometerPort
|
Sensor adapter (injected by cosalette DI). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
|
dict[str, object]
|
on every poll cycle. |