diff --git a/README.md b/README.md index da28260..fbc40ee 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Auto backup can be configured via the web interface. - __Restore__ : Upload backed-up snapshot to Home assistant. - __Auto Stop__ : This addon can stop addons before backup and restart them after backup - __Web UI__ : All the configuration is based on an easy-to-use web interface, no yaml needed. +- __Home Assistant State Entities__ : This addon create 2 entite in HA : `binary_sensor.nextcloud_backup_error` and `sensor.nextcloud_backup_status` > __Info:__ > Auto Clean is executed after every upload and every day at 00h30 diff --git a/nextcloud_backup/.README.ejs b/nextcloud_backup/.README.ejs index d4353b7..64c8366 100644 --- a/nextcloud_backup/.README.ejs +++ b/nextcloud_backup/.README.ejs @@ -17,6 +17,7 @@ Easily backup your Home Assistant snapshots to Nextcloud. - __Auto Stop__ : This addon can stop addons before backup and restart them after backup - __Restore__ : Upload backed-up snapshot to Home assistant. - __Web UI__ : All the configuration is based on an easy-to-use web interface, no yaml needed. +- __Home Assistant State Entities__ : This addon create 2 entite in HA : `binary_sensor.nextcloud_backup_error` and `sensor.nextcloud_backup_status` [Click here for the full documentation][docs] diff --git a/nextcloud_backup/DOCS.md b/nextcloud_backup/DOCS.md index 4348c1e..8069bf0 100644 --- a/nextcloud_backup/DOCS.md +++ b/nextcloud_backup/DOCS.md @@ -42,6 +42,8 @@ You can now configure the automatic backup. If enabled, you can specify how much Local Snapshot and Nextcloud Backup you want to keep before deleting the older one. > __Info:__ > Auto Clean is executed after every upload and every day at 00h30 + + ## Home Assitant Os Configuration **Note**: _Remember to restart the add-on when the configuration is changed._ diff --git a/nextcloud_backup/README.md b/nextcloud_backup/README.md index cd80eca..c43001b 100644 --- a/nextcloud_backup/README.md +++ b/nextcloud_backup/README.md @@ -18,6 +18,8 @@ Easily backup your Home Assistant snapshots to Nextcloud. - __Auto Stop__ : This addon can stop addons before backup and restart them after backup - __Restore__ : Upload backed-up snapshot to Home assistant. - __Web UI__ : All the configuration is based on an easy-to-use web interface, no yaml needed. +- __Home Assistant State Entities__ : This addon create 2 entite in HA : `binary_sensor.nextcloud_backup_error` and `sensor.nextcloud_backup_status` + [Click here for the full documentation][docs] diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js index 64ce9a0..cad43eb 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js @@ -457,7 +457,7 @@ function startAddons() { let promises = []; let token = process.env.HASSIO_TOKEN; let option = { - headers: { "X-HASSIO-KEY": token }, + headers: { "Authorization": `Bearer ${token}` }, responseType: "json", }; let addons_slug = settingsTools.getSettings().auto_stop_addon @@ -494,6 +494,76 @@ function startAddons() { })); } +function publish_state(state){ + + let data_error_sensor = { + state: state.status == "error" ? "on" : "off", + attributes: { + friendly_name: "Nexcloud Backup Error", + device_class: "problem", + error_code: state.error_code, + message: state.message, + icon: state.status == "error" ? "mdi:cloud-alert" : "mdi:cloud-check" + }, + } + + + let token = process.env.HASSIO_TOKEN; + let option = { + headers: { "Authorization": `Bearer ${token}` }, + responseType: "json", + json: data_error_sensor + }; + got.post(`http://hassio/core/api/states/binary_sensor.nextcloud_backup_error`, option) + .then((result) => { + logger.debug('Home assistant sensor updated (error status)'); + }) + .catch((error) => { + logger.error(error); + }); + + let icon = "" + switch(state.status){ + case "error": + icon = "mdi:cloud-alert"; + break; + case "download": + case "download-b": + icon = "mdi:cloud-download"; + break; + case "upload": + case "upload-b": + icon = "mdi:cloud-upload"; + break; + case "idle": + icon = "mdi:cloud-check"; + break; + default: + icon = "mdi:cloud-sync"; + break; + } + + let data_state_sensor = { + state: state.status, + attributes: { + friendly_name: "Nexcloud Backup Status", + error_code: state.error_code, + message: state.message, + icon: icon, + last_backup: state.last_backup == null ? "" : new Date(state.last_backup).toISOString(), + next_backup: state.next_backup == null ? "" : new Date(state.next_backup).toISOString() + }, + } + option.json = data_state_sensor + got.post(`http://hassio/core/api/states/sensor.nextcloud_backup_status`, option) + .then((result) => { + logger.debug('Home assistant sensor updated (status)'); + }) + .catch((error) => { + logger.error(error); + }); +} + exports.getVersion = getVersion; exports.getAddonList = getAddonList; exports.getFolderList = getFolderList; @@ -504,3 +574,4 @@ exports.uploadSnapshot = uploadSnapshot; exports.stopAddons = stopAddons; exports.startAddons = startAddons; exports.clean = clean; +exports.publish_state = publish_state; diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/status.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/status.js index c66522c..fb3c826 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/status.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/status.js @@ -1,4 +1,5 @@ const fs = require("fs"); +const hassioApiTools = require("./hassioApiTools"); const statusPath = "/data/status.json"; @@ -33,10 +34,13 @@ function setStatus(state) { let old_state_str = fs.readFileSync(statusPath).toString(); if(old_state_str !== JSON.stringify(state)){ fs.writeFileSync(statusPath, JSON.stringify(state)); + hassioApiTools.publish_state(state); } }else{ fs.writeFileSync(statusPath, JSON.stringify(state)); + hassioApiTools.publish_state(state); } + }