Skip to main content

ha_services

tests codecov ha_services @ PyPi Python Versions License GPL-3.0-or-later

Helpers to send periodic information via MQTT to Home Assistant

Use by:

Validation of device/state classes and units

Home Assistant supports only a defined combination of device-, state-classes and units.

see: https://www.home-assistant.io/integrations/sensor/#device-class

hs-services contains a validation of these combinations. We collect the valid combinations from Home Assistant source code.

Update of these data is done by: ./dev-cli.py update-ha-data

A Sensor inheriting from ha_services.mqtt4homeassistant.components.sensor.Sensor will be validated.

Validation can be disabled by setting validate=False in the constructor of the Sensor.

start development

At least uv is needed. Install e.g.: via pipx:

apt install pipx
pipx install uv

Clone the project and just start the CLI help commands. A virtual environment will be created/updated automatically.

~$ git clone https://github.com/jedie/ha-services.git
~$ cd ha-services
~/ha-services$ ./cli.py --help
~/ha-services$ ./dev-cli.py --help

dev CLI

usage: ./dev-cli.py [-h] {coverage,install,lint,mypy,nox,pip-audit,publish,shell-completion,test,update,update-ha-data,update-readme-history,update-test-snapshot-files,version}



╭─ options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ -h, --help     show this help message and exit                                                                       │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ subcommands ────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ (required)                                                                                                           │
│   • coverage   Run tests and show coverage report.                                                                   │
│   • install    Install requirements and 'ha_services' via pip as editable.                                           │
│   • lint       Check/fix code style by run: "ruff check --fix"                                                       │
│   • mypy       Run Mypy (configured in pyproject.toml)                                                               │
│   • nox        Run nox                                                                                               │
│   • pip-audit  Run pip-audit check against current requirements files                                                │
│   • publish    Build and upload this project to PyPi                                                                 │
│   • shell-completion                                                                                                 │
│                Setup shell completion for this CLI (Currently only for bash shell)                                   │
│   • test       Run unittests                                                                                         │
│   • update     Update dependencies (uv.lock) and git pre-commit hooks                                                │
│   • update-ha-data                                                                                                   │
│                Update Home Assistant data files for custom components. Creates a venv in /tmp/, installs             │
│                `homeassistant` package and runs a script to collect data.                                            │
│   • update-readme-history                                                                                            │
│                Update project history base on git commits/tags in README.md Will be exited with 1 if the README.md   │
│                was updated otherwise with 0.                                                                         │
│                                                                                                                      │
│                Also, callable via e.g.:                                                                              │
│                    python -m cli_base update-readme-history -v                                                       │
│   • update-test-snapshot-files                                                                                       │
│                Update all test snapshot files (by remove and recreate all snapshot files)                            │
│   • version    Print version and exit                                                                                │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

DEMO app CLI

usage: ./cli.py [-h] {edit-settings,info,print-settings,publish-loop,shell-completion,systemd-debug,systemd-logs,systemd-remove,systemd-setup,systemd-status,systemd-stop,test-mqtt-connection,update-readme-history,version,wifi-info}



╭─ options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ -h, --help        show this help message and exit                                                                    │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ subcommands ────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ (required)                                                                                                           │
│   • edit-settings                                                                                                    │
│                   Edit the settings file. On first call: Create the default one.                                     │
│   • info          Publish data once and display information about all registered components.                         │
│   • print-settings                                                                                                   │
│                   Display (anonymized) MQTT server username and password                                             │
│   • publish-loop  Publish data via MQTT for Home Assistant (endless loop)                                            │
│   • shell-completion                                                                                                 │
│                   Setup shell completion for this CLI (Currently only for bash shell)                                │
│   • systemd-debug                                                                                                    │
│                   Print Systemd service template + context + rendered file content.                                  │
│   • systemd-logs  List and follow logs of systemd service. (May need sudo)                                           │
│   • systemd-remove                                                                                                   │
│                   Write Systemd service file, enable it and (re-)start the service. (May need sudo)                  │
│   • systemd-setup                                                                                                    │
│                   Write Systemd service file, enable it and (re-)start the service. (May need sudo)                  │
│   • systemd-status                                                                                                   │
│                   Display status of systemd service. (May need sudo)                                                 │
│   • systemd-stop  Stops the systemd service. (May need sudo)                                                         │
│   • test-mqtt-connection                                                                                             │
│                   Test connection to MQTT Server                                                                     │
│   • update-readme-history                                                                                            │
│                   Update project history base on git commits/tags in README.md Will be exited with 1 if the          │
│                   README.md was updated otherwise with 0.                                                            │
│                                                                                                                      │
│                   Also, callable via e.g.:                                                                           │
│                       python -m cli_base update-readme-history -v                                                    │
│   • version       Print version and exit                                                                             │
│   • wifi-info     Just display the WiFi info                                                                         │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Backwards-incompatible changes

v2.10.0

For new validation the main loop should catch InvalidStateValue exceptions, log it as warning and continue the loop. See ha_services.example.publish_forever() for an example ;)

v2.0.0

Complete refactor of mqtt4homeassistant module. New usage, see: ha_services/example.py

History

  • v2.15.4
    • 2026-04-10 - Add method to cleanup of global registry #103
  • v2.15.3
    • 2026-04-10 - fix code style
    • 2026-04-10 - Update requirements
    • 2026-04-08 - Bump cryptography from 46.0.6 to 46.0.7
    • 2026-03-30 - Bump pygments from 2.19.2 to 2.20.0
    • 2026-03-29 - Bump cryptography from 46.0.4 to 46.0.6
  • v2.15.2
    • 2026-02-08 - Apply manageprojects updates: Use "uv" direct and update requirements
    • 2025-12-09 - Update requirements
  • v2.15.1
    • 2025-10-25 - Update requirements and fix CI tests
    • 2025-10-25 - Apply manageprojects updates
    • 2025-10-25 - Enhance logging in WifiInfo2Mqtt if "iwconfig" not found
Expand older history entries ...
  • v2.15.0
    • 2025-10-08 - Update requirements and ignore GHSA-4xh5-x5gv-qwph
    • 2025-10-08 - Catch error if "iwconfig" not found and other problems with internal sensor data
  • v2.14.0
    • 2025-09-25 - NEW: Validation of device/state classes and units
    • 2025-09-24 - Apply manageprojects updates
  • v2.13.0
    • 2025-08-20 - NEW: Add "info" command to display information about all components
    • 2025-08-20 - Bugfix: Make registry of all components "global"
    • 2025-08-19 - Individual "throttle_sec" and "config_throttle_sec"
    • 2025-08-19 - Update requirements
  • v2.12.0
    • 2025-06-17 - Add "systemd-logs"
  • v2.11.0
    • 2025-06-17 - Add and send "qos" and "retain"
    • 2025-06-16 - Update requirements
  • v2.10.0
    • 2025-04-08 - Optional validation of sensor states
  • v2.9.0
    • 2025-04-08 - Add Wifi info into MainMqttDevice
  • v2.8.0
    • 2025-04-08 - Fix get_system_start_datetime()
    • 2025-04-08 - pip-tools -> uv
  • v2.7.0
    • 2024-09-20 - Apply manageprojects updates and replace safety with pip-audit
    • 2024-09-20 - Update requirements
    • 2024-04-22 - Bugfix: "Mhz" -> "MHz"
  • v2.6.0
    • 2024-04-22 - fix tests using freezegun
    • 2024-04-22 - Add MQTT Select component for Home Assistant
  • v2.5.0
    • 2024-03-27 - Skip config publising for a while and add more system sensors
  • v2.4.0
    • 2024-03-26 - Enhance system sensors
  • v2.3.0
    • 2024-03-26 - Replace up_time, running_time and add cpu_freq_sensor
    • 2024-03-25 - Update README.md
  • v2.2.0
    • 2024-03-25 - Add main_uid as unique "prefix" that defaults to the current hostname
  • v2.1.0
    • 2024-03-25 - Remove Python 3.9 support
    • 2024-03-25 - Update requirements
    • 2024-03-25 - Add "via_device"
  • v2.0.1
    • 2024-03-24 - Fix #59 Don't crash if command topic can't be subscribed
  • v2.0.0
    • 2024-03-23 - Add device class to BinarySensor
    • 2024-03-23 - fix test with python 3.10
    • 2024-03-23 - Add BinarySensor
    • 2024-03-22 - Complete refactor mqtt4homeassistant module
  • v0.6.0
    • 2024-03-15 - Bugfix publish command
    • 2024-03-15 - Enhance MQTT data structure and defaults
    • 2024-03-15 - Update project by split CLI
    • 2024-03-15 - Update requirements
  • v0.5.0
    • 2024-03-09 - Migrate to new paho api
    • 2024-03-09 - Apply cookiecutter template updates
    • 2024-02-22 - Update requirements
    • 2023-12-17 - Apply manageprojects updates
    • 2023-12-17 - Fix useless tuple creation
  • v0.4.0
  • v0.3.4
    • 2023-08-08 - move "subprocess_utils" to "cli_tools"
  • v0.3.3
    • 2023-08-08 - Update requirements
    • 2023-08-08 - toml-settings: Expand ~ and ~user constructs for path configs
    • 2023-08-08 - Display more frames in tracebacks
  • v0.3.2
    • 2023-05-21 - Bugfix systemd.api if Systemd is not available (e.g. on a Mac)
  • v0.3.1
    • 2023-05-20 - Better error message in open_editor_for() and add test for it.
  • v0.3.0
    • 2023-05-19 - Move unittest/tox commands and add a coverage fix
    • 2023-05-19 - Refactor logging setup and verbosity levels
  • v0.2.0
    • 2023-05-18 - Use term width == 100 for README examples.
    • 2023-05-18 - Fix github CI run by apply strip_ansi() to CLI stdout
    • 2023-05-18 - try to fix CI
    • 2023-05-18 - add: AssertCliHelpInReadme() and CliMock()
    • 2023-05-18 - Add MockTomlSettings
    • 2023-05-18 - update requirements
    • 2023-05-18 - Refactor MockSystemdServiceInfo and add some tests tools
    • 2023-05-17 - Refactor SystemdServiceInfo dataclass
    • 2023-05-11 - Rafactor toml settings and systemd and other stuff ;)
    • 2023-05-11 - Bugfix sudo calls by expand_user() that has special handling for sudo calls
  • v0.1.0
    • 2023-05-09 - Add systemd service helper
    • 2023-05-08 - Update README.md
    • 2023-05-07 - fix CI
  • v0.0.1
    • 2023-05-07 - fix packaging
    • 2023-05-07 - Implement "publish-loop"
    • 2023-05-07 - sensible editor ;)
    • 2023-05-07 - Add "mqtt4homeassistant" and "toml_settings"
    • 2023-05-07 - init

Metadata

Release files for ha-services 2.15.4

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

Source distribution (sdist)

Source distribution for ha-services 2.15.4
File Size Uploaded
ha_services-2.15.4.tar.gz 129.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ha-services 2.15.4
File Interpreter ABI Platform
ha_services-2.15.4-py3-none-any.whl Python 3 none any Details

Total release size: 205.5 kB

Release files / ha_services-2.15.4.tar.gz

Download URL ha_services-2.15.4.tar.gz
Size 129.7 kB
Tags Source
SHA-256 checksum
How to use checksums
890821463e9f8548940e25574a27d7ed3bea0a278e7abe8a5a0365eeefbae94e
BLAKE2b-256 checksum
How to use checksums
6cea63fddc90b25fba1b4aaf5e8c92bca50e13c4f575195870f92371645a1c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ha_services-2.15.4-py3-none-any.whl

Download URL ha_services-2.15.4-py3-none-any.whl
Size 75.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51822c189c3942ae8765f2ebdb05794039d1d3bbeae12fa9b9db16dd8aba3be0
BLAKE2b-256 checksum
How to use checksums
f1938bac80671150aafaf4c2a3f3e397686d1b92580833d1a61118e89b3db975
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.15.4 This release

2 release files

2.15.3

2 release files

2.15.1

2 release files

2.14.0

2 release files

2.13.0

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

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