Skip to content
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

Implement a companion integration that publishes sensors and notifications #805

Open
Anto79-ops opened this issue Feb 28, 2023 · 5 comments
Assignees
Labels
consider in the future Maybe it will happen, maybe it won't. time will tell

Comments

@Anto79-ops
Copy link

Anto79-ops commented Feb 28, 2023

Hi,

I reported an issue here after using @frenck Spook integration, but turns out it found an issue with this addon.

long story short, you may wish to check out this thread.

thanks!

frenck/spook#18

@sabeechen
Copy link
Owner

Thanks for bringing that thread to my attention, the language there (abuse, inject) implies I'm doing something nefarious and I feel the need to defend my methods. I'll post my counter-rant there.

The "ghost" entities are something I'm aware of, but haven't had time to fix in light of other issues. People get a degraded experience with the entities this addon creates. They can't be customized and automations that reference them fail at startup because they don't exits until the addon republishes them (usually on a timer). As pointed out it would be better to have a companion integration installed in Home Assistant, I just need to find the time.
In the meantime, there is a workaround if you don't like the errors spook is showing. You can:

  1. In the addon settings, uncheck "Enable Backup Staleness Sensor" and "Enable Backup State Sensor". This will make the addon stop calling Home Assistant's /api/states/ API.
  2. You can then create a template to call the addon directly to get the staleness. For example the config below will create a rest binary sensor that queries the addon directly over docker's internal network to get the most recent backup date in Google Drive and check it isn't more than 30 hours old (24 hours plus a buffer):
binary_sensor:
  - platform: rest
    resource: http://cebe7a76-hassio-google-drive-backup:8099/getstatus
    name: Backups Stale But Better
    value_template: "{{ (now() - as_datetime(value_json.Sources.GoogleDrive.latest)) > timedelta(hours=30)}}"
    device_class: problem

The /getstatus endpoint there isn't exactly intended for people to query, but using it this way is unlikely to break in the future. It might though.

@sabeechen
Copy link
Owner

Lets make this the official issue to implement a companion integration in Home Assistant to support the addon. The goals would:

  • Create a HA integration that handles the lifecycle for:
    • The binary_sensor.backups_stale sensor
    • The sensor.backup_state sensor
    • The persistent notification that pops up when backups are stale
  • Provide an upgrade path for existing users that either:
    • Installs the companion silently in the background (not sure if this possible or even acceptable)
    • Prompts users to install the integration separately (and continues publishing the old way in the background).

The idea would be to implement this in a way that doesn't break existing user's workflows even if they silently upgrade to a version aware of the companion integration.

@sabeechen sabeechen changed the title Issue from Spook - Message from Frenck Implement a companion integration that publishes sensors and notifications Mar 1, 2023
@sabeechen sabeechen self-assigned this Mar 1, 2023
@sabeechen sabeechen added the consider in the future Maybe it will happen, maybe it won't. time will tell label Mar 1, 2023
@Anto79-ops
Copy link
Author

Anto79-ops commented Mar 2, 2023

thank you for this information!

Your addon is arguably the most important in HA, and glad its getting some friendly banter :)

@erkr
Copy link

erkr commented May 7, 2024

Thanks for the great add on
Will apply the rest sensor solution.

Update:
The rest example is not fully correct. Sources should be sources in the value template.

@sabeechen it would be nice if the days between backups could be part of the status payload. Now the max age in the value check needs to be hard coded (3 days in my example).

Rest sensor new style:


rest:
 - resource: http://cebe7a76-hassio-google-drive-backup:8099/getstatus
   scan_interval: 3600
   binary_sensor:
    - name: Backups Stale
      unique_id: backups_stale
      value_template: "{{ (now() - as_datetime(value_json.sources.GoogleDrive.latest)) > timedelta(days=3, hours=12) }}"
      device_class: problem

@erkr
Copy link

erkr commented May 9, 2024

For those interested, This command line sensor creates a similar sensor.backup_state.
The JSON returned by the Rest call is very large and detailed. This is preferably not copied as such into an attribute.
This command line sensor filters the JSON payload with JQ to reduce it to just the most relevant info:

  • A condensed backup list (Similar to original sensor)
  • Sources with Home assistant and GoogleDrive summaries
  • The last error info
command_line:
  - sensor:
      name: Backup State
      unique_id: backup_state
      command: >
        curl -s http://cebe7a76-hassio-google-drive-backup:8099/getstatus | jq '{ "sources": .sources, "backups": ([ .backups[] | {"name": .name, "size": .size, "status": .status, "date": .date}] | reverse), "last_error": {"count": .last_error_count, "message": .last_error} }' 
      value_template: >
        {%- if value_json.last_error.count > 0 -%}
        Error(s)
        {%- elif value_json.backups | count == 0 -%}
        No backups
        {%- elif (now() - as_datetime(value_json.sources.GoogleDrive.latest)) > timedelta(days=3, hours=6) -%}
        Stale
        {%- else -%}
        Ok
        {%- endif -%}
      json_attributes:
        - sources
        - backups
        - last_error
      scan_interval: 3600  

The result in the dev tools:

state: OK

Attributes:

friendly_name: Backup State
sources:
  HomeAssistant:
    backups: 4
    retained: 0
    deletable: 4
    name: HomeAssistant
    title: Home Assistant
    latest: "2024-05-07T02:45:00Z"
    max: 4
    enabled: true
    icon: home-assistant
    ignored: 9
    detail: ""
    size: 3.7 GB
    ignored_size: 458.5 MB
    free_space: 146.6 GB
  GoogleDrive:
    backups: 10
    retained: 0
    deletable: 10
    name: GoogleDrive
    title: Google Drive
    latest: "2024-05-07T02:45:00Z"
    max: 10
    enabled: true
    icon: google-drive
    ignored: 0
    detail: <user>@gmail.com
    size: 9.7 GB
    ignored_size: 0.0 B
    free_space: 20.9 GB
backups:
  - name: addon_d5369777_music_assistant_beta_2.0.0b147
    size: 65.7 MB
    status: HA Only
    date: Thu May  9 09:09:54 2024
  - name: addon_d5369777_music_assistant_beta_2.0.0b146
    size: 65.5 MB
    status: HA Only
    date: Wed May  8 08:44:54 2024
...
 - name: Partial Backup 2024-04-10 04:45:00
    size: 1001.9 MB
    status: Drive Only
    date: Wed Apr 10 04:45:00 2024
last_error:
  count: 0
  message: null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consider in the future Maybe it will happen, maybe it won't. time will tell
Projects
None yet
Development

No branches or pull requests

3 participants