Browse Source

HA: Use `default_entity_id` option instead of deprecated `object_id` in device discovery payloads. (#254)

* HA discovery message uses `default_entity_id` instead of deprecated `object_id`

cf https://github.com/home-assistant/core/pull/151775

* update changelog

* mention Home Assistant 2025.10+ requirement  for MQTT discovery

https://github.com/fphammerle/systemctl-mqtt/pull/254
frantzju 1 week ago
parent
commit
ef6f964f2c
4 changed files with 22 additions and 12 deletions
  1. 7 1
      CHANGELOG.md
  2. 2 0
      README.md
  3. 7 5
      systemctl_mqtt/__init__.py
  4. 6 6
      tests/test_state_dbus.py

+ 7 - 1
CHANGELOG.md

@@ -5,13 +5,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Changed
+- Home Assistant device discovery message uses `default_entity_id` instead of
+  deprecated `object_id` (requires Home Assistant 2025.10+).
+  ([#254](https://github.com/fphammerle/systemctl-mqtt/pull/254)
+  by Julien Frantz (julien.frantz@gmail.com))
+
 ### Fixed
 - add missing dbus rules for systemd Manager and Unit in apparmor profile.
   ([#244](https://github.com/fphammerle/systemctl-mqtt/pull/244)
   by Julien Frantz (julien.frantz@gmail.com))
 - publish Home Assistant device discovery config whenever Home Assistant sends
   its birth message on startup.
-  ([#244](https://github.com/fphammerle/systemctl-mqtt/pull/244)
+  ([#245](https://github.com/fphammerle/systemctl-mqtt/pull/245)
   by Julien Frantz (julien.frantz@gmail.com))
 
 ## [2.0.0] - 2025-11-22

+ 2 - 0
README.md

@@ -105,6 +105,8 @@ enables that a system unit can be started, stopped, restarted and isolated by a
 
 ## Home Assistant 🏡
 
+> **Note:** Home Assistant 2025.10+ is required for MQTT Discovery of `systemctl-mqtt` entities.
+
 When [MQTT Discovery](https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery)
 is enabled (default in Home Assistant ≥0.117.0), the following entities will be
 added automatically:

+ 7 - 5
systemctl_mqtt/__init__.py

@@ -204,7 +204,7 @@ class _State:
             "components": {
                 "logind/preparing-for-shutdown": {
                     "unique_id": unique_id_prefix + "-logind-preparing-for-shutdown",
-                    "object_id": f"{hostname}_logind_preparing_for_shutdown",  # entity id
+                    "default_entity_id": f"binary_sensor.{hostname}_logind_preparing_for_shutdown",
                     "name": "preparing for shutdown",  # home assistant prepends device name
                     "platform": "binary_sensor",
                     "state_topic": self._preparing_for_shutdown_topic,
@@ -219,9 +219,9 @@ class _State:
             # > Unsupported target for indexed assignment
             config["components"]["logind/" + mqtt_topic_suffix] = {  # type: ignore
                 "unique_id": unique_id_prefix + "-logind-" + mqtt_topic_suffix,
-                "object_id": hostname
+                "default_entity_id": f"button.{hostname}"
                 + "_logind_"
-                + mqtt_topic_suffix.replace("-", "_"),  # entity id
+                + mqtt_topic_suffix.replace("-", "_"),
                 "name": mqtt_topic_suffix.replace("-", " "),
                 "platform": "button",
                 "command_topic": self.mqtt_topic_prefix + "/" + mqtt_topic_suffix,
@@ -229,7 +229,7 @@ class _State:
         for unit_name in self._monitored_system_unit_names:
             config["components"]["unit/system/" + unit_name + "/active-state"] = {  # type: ignore
                 "unique_id": f"{unique_id_prefix}-unit-system-{unit_name}-active-state",
-                "object_id": f"{hostname}_unit_system_{unit_name}_active_state",
+                "default_entity_id": f"sensor.{hostname}_unit_system_{unit_name}_active_state",
                 "name": f"{unit_name} active state",
                 "platform": "sensor",
                 "state_topic": self.get_system_unit_active_state_mqtt_topic(
@@ -247,7 +247,9 @@ class _State:
                 if action_class(unit_name).is_allowed():
                     config["components"][component_prefix + "/" + action_name] = {  # type: ignore
                         "unique_id": f"{unique_id_prefix}-unit-system-{unit_name}-{action_name}",
-                        "object_id": f"{hostname}_unit_system_{unit_name}_{action_name}",
+                        "default_entity_id": (
+                            f"button.{hostname}_unit_system_{unit_name}_{action_name}"
+                        ),
                         "name": f"{unit_name} {action_name}",
                         "platform": "button",
                         "command_topic": self.get_system_unit_action_mqtt_topic(

+ 6 - 6
tests/test_state_dbus.py

@@ -212,7 +212,7 @@ async def test_publish_homeassistant_device_config(
         "components": {
             "logind/preparing-for-shutdown": {
                 "unique_id": f"systemctl-mqtt-{hostname}-logind-preparing-for-shutdown",
-                "object_id": f"{hostname}_logind_preparing_for_shutdown",
+                "default_entity_id": f"binary_sensor.{hostname}_logind_preparing_for_shutdown",
                 "name": "preparing for shutdown",
                 "platform": "binary_sensor",
                 "state_topic": topic_prefix + "/preparing-for-shutdown",
@@ -221,21 +221,21 @@ async def test_publish_homeassistant_device_config(
             },
             "logind/poweroff": {
                 "unique_id": f"systemctl-mqtt-{hostname}-logind-poweroff",
-                "object_id": f"{hostname}_logind_poweroff",
+                "default_entity_id": f"button.{hostname}_logind_poweroff",
                 "name": "poweroff",
                 "platform": "button",
                 "command_topic": f"{topic_prefix}/poweroff",
             },
             "logind/lock-all-sessions": {
                 "unique_id": f"systemctl-mqtt-{hostname}-logind-lock-all-sessions",
-                "object_id": f"{hostname}_logind_lock_all_sessions",
+                "default_entity_id": f"button.{hostname}_logind_lock_all_sessions",
                 "name": "lock all sessions",
                 "platform": "button",
                 "command_topic": f"{topic_prefix}/lock-all-sessions",
             },
             "logind/suspend": {
                 "unique_id": f"systemctl-mqtt-{hostname}-logind-suspend",
-                "object_id": f"{hostname}_logind_suspend",
+                "default_entity_id": f"button.{hostname}_logind_suspend",
                 "name": "suspend",
                 "platform": "button",
                 "command_topic": f"{topic_prefix}/suspend",
@@ -244,7 +244,7 @@ async def test_publish_homeassistant_device_config(
         | {
             f"unit/system/{n}/active-state": {
                 "unique_id": f"systemctl-mqtt-{hostname}-unit-system-{n}-active-state",
-                "object_id": f"{hostname}_unit_system_{n}_active_state",
+                "default_entity_id": f"sensor.{hostname}_unit_system_{n}_active_state",
                 "name": f"{n} active state",
                 "platform": "sensor",
                 "state_topic": f"{topic_prefix}/unit/system/{n}/active-state",
@@ -254,7 +254,7 @@ async def test_publish_homeassistant_device_config(
         | {
             f"unit/system/{n}/{action}": {
                 "unique_id": f"systemctl-mqtt-{hostname}-unit-system-{n}-{action}",
-                "object_id": f"{hostname}_unit_system_{n}_{action}",
+                "default_entity_id": f"button.{hostname}_unit_system_{n}_{action}",
                 "name": f"{n} {action}",
                 "platform": "button",
                 "command_topic": f"{topic_prefix}/unit/system/{n}/{action}",