|
@@ -143,15 +143,17 @@ class _State:
|
|
self._shutdown_lock = None
|
|
self._shutdown_lock = None
|
|
|
|
|
|
def _publish_preparing_for_shutdown(
|
|
def _publish_preparing_for_shutdown(
|
|
- self, mqtt_client: paho.mqtt.client.Client, active: bool
|
|
+ self, mqtt_client: paho.mqtt.client.Client, active: bool, block: bool,
|
|
) -> None:
|
|
) -> None:
|
|
|
|
|
|
topic = self.mqtt_topic_prefix + "/preparing-for-shutdown"
|
|
topic = self.mqtt_topic_prefix + "/preparing-for-shutdown"
|
|
payload = json.dumps(active)
|
|
payload = json.dumps(active)
|
|
_LOGGER.info("publishing %r on %s", payload, topic)
|
|
_LOGGER.info("publishing %r on %s", payload, topic)
|
|
msg_info = mqtt_client.publish(
|
|
msg_info = mqtt_client.publish(
|
|
- topic=topic, payload=payload,
|
|
+ topic=topic, payload=payload, retain=True,
|
|
)
|
|
)
|
|
|
|
+ if not block:
|
|
|
|
+ return
|
|
msg_info.wait_for_publish()
|
|
msg_info.wait_for_publish()
|
|
if msg_info.rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
|
|
if msg_info.rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
|
|
_LOGGER.error(
|
|
_LOGGER.error(
|
|
@@ -163,7 +165,9 @@ class _State:
|
|
) -> None:
|
|
) -> None:
|
|
assert isinstance(active, dbus.Boolean)
|
|
assert isinstance(active, dbus.Boolean)
|
|
active = bool(active)
|
|
active = bool(active)
|
|
- self._publish_preparing_for_shutdown(mqtt_client=mqtt_client, active=active)
|
|
+ self._publish_preparing_for_shutdown(
|
|
|
|
+ mqtt_client=mqtt_client, active=active, block=True,
|
|
|
|
+ )
|
|
if active:
|
|
if active:
|
|
self.release_shutdown_lock()
|
|
self.release_shutdown_lock()
|
|
else:
|
|
else:
|
|
@@ -179,6 +183,29 @@ class _State:
|
|
),
|
|
),
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+ def publish_preparing_for_shutdown(
|
|
|
|
+ self, mqtt_client: paho.mqtt.client.Client,
|
|
|
|
+ ) -> None:
|
|
|
|
+ try:
|
|
|
|
+ active = self._login_manager.Get(
|
|
|
|
+ "org.freedesktop.login1.Manager",
|
|
|
|
+ "PreparingForShutdown",
|
|
|
|
+ dbus_interface="org.freedesktop.DBus.Properties",
|
|
|
|
+ )
|
|
|
|
+ except dbus.DBusException as exc:
|
|
|
|
+ _LOGGER.error(
|
|
|
|
+ "failed to read logind's PreparingForShutdown property: %s",
|
|
|
|
+ exc.get_dbus_message(),
|
|
|
|
+ )
|
|
|
|
+ return
|
|
|
|
+ assert isinstance(active, dbus.Boolean), active
|
|
|
|
+ self._publish_preparing_for_shutdown(
|
|
|
|
+ mqtt_client=mqtt_client,
|
|
|
|
+ active=bool(active),
|
|
|
|
+
|
|
|
|
+ block=False,
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
|
|
class _MQTTAction:
|
|
class _MQTTAction:
|
|
|
|
|
|
@@ -226,6 +253,7 @@ def _mqtt_on_connect(
|
|
_LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
|
|
_LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
|
|
state.acquire_shutdown_lock()
|
|
state.acquire_shutdown_lock()
|
|
state.register_prepare_for_shutdown_handler(mqtt_client=mqtt_client)
|
|
state.register_prepare_for_shutdown_handler(mqtt_client=mqtt_client)
|
|
|
|
+ state.publish_preparing_for_shutdown(mqtt_client=mqtt_client)
|
|
for topic_suffix, action in _MQTT_TOPIC_SUFFIX_ACTION_MAPPING.items():
|
|
for topic_suffix, action in _MQTT_TOPIC_SUFFIX_ACTION_MAPPING.items():
|
|
topic = state.mqtt_topic_prefix + "/" + topic_suffix
|
|
topic = state.mqtt_topic_prefix + "/" + topic_suffix
|
|
_LOGGER.info("subscribing to %s", topic)
|
|
_LOGGER.info("subscribing to %s", topic)
|