Skip to content

Coordinate graceful shutdown with Home Assistant OS#6851

Open
sairon wants to merge 1 commit into
mainfrom
haos-pre-shutdown
Open

Coordinate graceful shutdown with Home Assistant OS#6851
sairon wants to merge 1 commit into
mainfrom
haos-pre-shutdown

Conversation

@sairon
Copy link
Copy Markdown
Member

@sairon sairon commented May 19, 2026

Proposed change

On a reboot or power off triggered from the API, enter the haos-pre-shutdown.target systemd target before stopping Home Assistant Core, so the OS can stop the console CLI cleanly instead of letting it restart-loop while the CLI plugin is torn down. Entering the target is best-effort and a no-op on OS releases that don't have it.

For host shutdowns not initiated by Supervisor, the SIGTERM handler now inspects the systemd manager state. When it is "stopping", Supervisor runs the managed Core.shutdown() to stop Core, apps and plugins gracefully before tearing itself down; on a plain Supervisor restart the state is "running", so only Supervisor stops as before. The manager state is exposed by every systemd version, so this works regardless of the OS release.

Add the SystemState D-Bus enum and Systemd.get_system_state() for the state lookup.

Refs home-assistant/operating-system#4642
Refs home-assistant/operating-system#4719

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:
  • Link to client library pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints or add-on configuration are added/changed:

On a reboot or power off triggered from the API, enter the
haos-pre-shutdown.target systemd target before stopping Home Assistant
Core, so the OS can stop the console CLI cleanly instead of letting it
restart-loop while the CLI plugin is torn down. Entering the target is
best-effort and a no-op on OS releases that don't have it.

For host shutdowns not initiated by Supervisor, the SIGTERM handler now
inspects the systemd manager state. When it is "stopping", Supervisor
runs the managed Core.shutdown() to stop Core, apps and plugins
gracefully before tearing itself down; on a plain Supervisor restart the
state is "running", so only Supervisor stops as before. The manager
state is exposed by every systemd version, so this works regardless of
the OS release.

Add the SystemState D-Bus enum and Systemd.get_system_state() for the
state lookup.

Refs home-assistant/operating-system#4642
Refs home-assistant/operating-system#4719

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sairon sairon added the new-feature A new feature label May 19, 2026
Copy link
Copy Markdown
Contributor

@mdegat01 mdegat01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants