Casting the measured values to `int` loses a lot of precision. Sending
the raw values, however, results in uselessly over-precise values.
Thus, we need to compromise by truncating the values at the tenths
place.
When Home Assistant restarts, it puts the sensor in "Unavailable" state
until it receives an "online" message. Since the sensor has no idea
Home Assistant is waiting for such a message, it will never send one.
By setting the "retain" flag on availability and configuration messages,
the broker will automatically resend them to Home Assistant when it
subscribes to the topic again, which resolves this issue.
Home Assistant can mark MQTT-based sensors as "unavailable" when it
receives a special message (`offline`) on a designated topic. We send
an `online` message when the sensor process starts, and `offline` when
the process shuts down. Additionally, we set the last will and
testament message to `offline` as well, so that Home Assistant will
still get the notification, even if the sensor does not shut down
cleanly.
The `time.sleep` function cannot be stopped prematurely. This means
that when the program receives a signal indicating it should stop, it
will not be able to do so until the running `sleep` completes. This
means it could take up to 10 seconds for the process to stop.
A better way to handle the shutdown signal is to use a pipe. When the
signal is received, the write end of the pipe is closed. The event loop
will detect this immediately, as `select.select` returns as soon as any
event occurs on any of the polled file descriptors, or the timeout
expires.
The backlight on the CM3-PANEL cannot be controled with normal DPMS. Instead,
the brightness is set using PWM on GPIO pin 22. The
`thermostat.backlight` program runs as a background process to manage
the PWM duty cycle. It exposes a D-Bus service to allow other programs
to control the backlight. It also watches for *xscreensaver* events to
detect when the screen is blanked and disable the backlight.
https://www.acmesystems.it/CM3-PANEL-7-BASIC_backlight