Sem descrição

dependabot[bot] 47af8071a8 build(deps): bump docker/setup-buildx-action from 3.7.1 to 3.8.0 (#160) há 2 meses atrás
.github 47af8071a8 build(deps): bump docker/setup-buildx-action from 3.7.1 to 3.8.0 (#160) há 2 meses atrás
docs b66f0cd3ef home assistant auto discovery: add config for button scheduling poweroff & button locking all sessions há 2 meses atrás
systemctl_mqtt b66f0cd3ef home assistant auto discovery: add config for button scheduling poweroff & button locking all sessions há 2 meses atrás
tests b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
.dockerignore 74669520c1 added dockerfile & docker-compose.yml há 4 anos atrás
.gitattributes b66f0cd3ef home assistant auto discovery: add config for button scheduling poweroff & button locking all sessions há 2 meses atrás
.gitignore c790a93981 instruct sessions to lock on systemctl/hostname/lock-all-sessions há 4 anos atrás
.isort.cfg 04fcb2b7a9 isort: added config to workaround persistent misclassification of paho.mqtt.client há 4 anos atrás
.pylintrc c57260b7c0 drop compatibility with pyhon3.5 há 3 anos atrás
CHANGELOG.md 41844e30d0 test & declare compatibility with python3.13 há 2 meses atrás
COPYING 1574063886 mqtt message on topic systemctl/hostname/poweroff schedules poweroff with delay of 4 seconds há 4 anos atrás
Dockerfile b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
Makefile e72910d853 refactor makefile: minor renamings for consistency with switchbot-mqtt's Makefile há 4 anos atrás
Pipfile b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
Pipfile.lock b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
README.md 6a617ffb2e readme: adapt section on mqtt authentication há 2 meses atrás
docker-apparmor-profile b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
docker-compose.yml b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
mypy.ini 1574063886 mqtt message on topic systemctl/hostname/poweroff schedules poweroff with delay of 4 seconds há 4 anos atrás
pyproject.toml b6a358880b drop compatibility with python3.8; docker image: upgrade alpine base image from 3.13.1 to 3.21.0 including upgrade of python from 3.8 to 3.12 há 2 meses atrás
setup.py 41844e30d0 test & declare compatibility with python3.13 há 2 meses atrás

README.md

systemctl-mqtt

CI Pipeline Status Coverage Status Last Release Compatible Python Versions DOI

MQTT client triggering & reporting shutdown on systemd-based systems

Setup

$ pip3 install --user --upgrade systemctl-mqtt
$ systemctl-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS

On debian-based systems, dependencies can optionally be installed via:

$ sudo apt-get install --no-install-recommends python3-dbus python3-gi python3-paho-mqtt

Usage

Schedule Poweroff

Schedule poweroff by sending a MQTT message to topic systemctl/hostname/poweroff.

$ mosquitto_pub -h MQTT_BROKER -t systemctl/hostname/poweroff -n

Shutdown Report

systemctl-mqtt subscribes to logind's PrepareForShutdown signal.

systemctl halt|poweroff|reboot triggers a message with payload true on topic systemctl/hostname/preparing-for-shutdown.

Lock Screen

Lock screen by sending a MQTT message to topic systemctl/hostname/lock-all-sessions.

$ mosquitto_pub -h MQTT_BROKER -t systemctl/hostname/lock-all-sessions -n

Home Assistant 🏡

Automatic Discovery

When MQTT Discovery is enabled (default in Home Assistant ≥0.117.0), the following entities will be added automatically:

  • binary_sensor.[hostname]_logind_preparing_for_shutdown
  • button.[hostname]_logind_lock_all_sessions
  • button.[hostname]_logind_poweroff

homeassistant entities_over_auto_discovery

Pass --homeassistant-discovery-prefix custom-prefix to systemctl-mqtt when using a custom discovery topic prefix.

Manual YAML Configuration

Send Poweroff Command

# https://www.home-assistant.io/docs/mqtt/broker/#configuration-variables
mqtt:
  broker: BROKER_HOSTNAME_OR_IP_ADDRESS
  # credentials, additional options…

script:
  poweroff_raspberry_pi:
    sequence:
      service: mqtt.publish
      data:
        topic: systemctl/raspberrypi/poweroff

homeassistant:
  customize:
    script.poweroff_raspberry_pi:
      friendly_name: poweroff pi
      icon: mdi:power

Trigger Automation on Shutdown

automation:
- trigger:
    platform: mqtt
    topic: systemctl/raspberrypi/preparing-for-shutdown
    payload: 'true'
  action:
    service: switch.turn_off
    entity_id: switch.desk_lamp

Docker 🐳

  1. Clone this repository.
  2. Edit docker-compose.yml.
  3. Load AppArmor profile: sudo apparmor_parser ./docker-apparmor-profile
  4. sudo docker-compose up --build

Pre-built docker image are available at https://hub.docker.com/r/fphammerle/systemctl-mqtt/tags

Annotation of signed tags docker/* contains docker image digests: https://github.com/fphammerle/systemctl-mqtt/tags

MQTT via TLS

TLS is enabled by default. Run systemctl-mqtt --mqtt-disable-tls … to disable TLS.

MQTT Authentication

systemctl-mqtt --mqtt-username me --mqtt-password-file /run/secrets/password …
# or for testing (unsafe):
systemctl-mqtt --mqtt-username me --mqtt-password secret …

Adapt Poweroff Delay

systemctl-mqtt --poweroff-delay-seconds 60 …