Skip to content

Add last automatic backup state sensor #144187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

mib1185
Copy link
Contributor

@mib1185 mib1185 commented May 4, 2025

Proposed change

This adds the last_automatic_backup_state sensor

  • the state of the last automatic backup, matches the state in the frontend of the Settings > System > Backup page
  • is a string-enum entity, so is well suited to be automated on, as it has defined possible states
  • is based on the backup internal KnownBackups data to avoid further calls to the backup agents

image

image

image

image

image

reference: feature request

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented May 4, 2025

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (backup) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of backup can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign backup Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

# Last backup has failed locations
return LastBackupState.BACKUP_FAILED_LOCATIONS

return LastBackupState.BACKUP_SUCCESSFUL
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this calculation. It's an arbitrary decision to create just these states from the available data.

I'd rather return the underlying data as is from different sensors or service action calls.

Copy link
Contributor Author

@mib1185 mib1185 May 4, 2025

Choose a reason for hiding this comment

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

it's not as arbitrary as it seems, it is inspired from the way how the frontend calculates this state - the difference is, that we use the KnownBackups data to avoid further calls against the backup agents and the KnownBackups data should be reliable, as they are updated every time a backup is createdtriggered

Copy link
Member

Choose a reason for hiding this comment

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

Still, there's nothing in the underlying data saying we should limit it to these states. If we start calculating states we can come up with lots of possible calculations. I don't think we should go there.

Copy link
Contributor Author

@mib1185 mib1185 May 4, 2025

Choose a reason for hiding this comment

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

I agree, that we could add countless states based on calculations, but the intention was to provide the user a sensor which represents equal states, like the frontend does, but with the advantage for the user, that they can create use it as an automation trigger. Unfortunately these states are based on calculations at the moment, as the state of the last automatic backup job is just not fully tracked/stored by the backup manager (we only know, when was the last attempt and when was the last success attempt).

So to not rely on calculations, we could extend the backup manager to track the state of the last automatic backup job - to keep it simple, we could limit this to a binary state true=success, false=failed and none=no state available 🤔
What do you think? Or should we put this into an arch-discussion?

@MartinHjelmare
Copy link
Member

I suggest you split the PR in two, one for each new sensor.

@MartinHjelmare MartinHjelmare marked this pull request as draft May 4, 2025 09:57
@mib1185 mib1185 changed the title Add further sensors to the Backup integration Add last automatic backup state sensor May 4, 2025
@mib1185
Copy link
Contributor Author

mib1185 commented May 4, 2025

I suggest you split the PR in two, one for each new sensor.

the last attempted sensor is now split into #144194 - will remove it from the current PR with a merge commit after the other PR got merged removed it now, to avoid dependencies between these PRs

@mib1185 mib1185 marked this pull request as ready for review May 4, 2025 14:38
@mib1185 mib1185 requested a review from Quentame as a code owner May 4, 2025 14:38
@MartinHjelmare MartinHjelmare marked this pull request as draft May 4, 2025 15:37
Comment on lines +15 to +21
class LastBackupState(StrEnum):
"""Last backup state type."""

NO_BACKUP = "no_backup"
BACKUP_FAILED_LOCATIONS = "last_backup_failed_locations"
BACKUP_FAILED = "last_backup_failed"
BACKUP_SUCCESSFUL = "last_backup_successful"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this, and the calculation in coordinator, should be moved to sensor.py since it's only used by the proposed new sensor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds absolutely reasonable, but I'll wait until the discussion above (#144187 (comment)) is resolved 👍

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

Successfully merging this pull request may close these issues.

3 participants