🔨 Add status entities to home assistant close #1

This commit is contained in:
Sebastien Clement 2021-09-07 15:37:04 +02:00
parent f2c9561d9b
commit efcdcd65b1
No known key found for this signature in database
GPG Key ID: 6C78A38312BBA4B6
6 changed files with 82 additions and 1 deletions

View File

@ -35,6 +35,7 @@ Auto backup can be configured via the web interface.
- __Restore__ : Upload backed-up snapshot to Home assistant. - __Restore__ : Upload backed-up snapshot to Home assistant.
- __Auto Stop__ : This addon can stop addons before backup and restart them after backup - __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. - __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:__ > __Info:__
> Auto Clean is executed after every upload and every day at 00h30 > Auto Clean is executed after every upload and every day at 00h30

View File

@ -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 - __Auto Stop__ : This addon can stop addons before backup and restart them after backup
- __Restore__ : Upload backed-up snapshot to Home assistant. - __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. - __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] [Click here for the full documentation][docs]

View File

@ -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. If enabled, you can specify how much Local Snapshot and Nextcloud Backup you want to keep before deleting the older one.
> __Info:__ > __Info:__
> Auto Clean is executed after every upload and every day at 00h30 > Auto Clean is executed after every upload and every day at 00h30
## Home Assitant Os Configuration ## Home Assitant Os Configuration
**Note**: _Remember to restart the add-on when the configuration is changed._ **Note**: _Remember to restart the add-on when the configuration is changed._

View File

@ -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 - __Auto Stop__ : This addon can stop addons before backup and restart them after backup
- __Restore__ : Upload backed-up snapshot to Home assistant. - __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. - __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] [Click here for the full documentation][docs]

View File

@ -457,7 +457,7 @@ function startAddons() {
let promises = []; let promises = [];
let token = process.env.HASSIO_TOKEN; let token = process.env.HASSIO_TOKEN;
let option = { let option = {
headers: { "X-HASSIO-KEY": token }, headers: { "Authorization": `Bearer ${token}` },
responseType: "json", responseType: "json",
}; };
let addons_slug = settingsTools.getSettings().auto_stop_addon 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.getVersion = getVersion;
exports.getAddonList = getAddonList; exports.getAddonList = getAddonList;
exports.getFolderList = getFolderList; exports.getFolderList = getFolderList;
@ -504,3 +574,4 @@ exports.uploadSnapshot = uploadSnapshot;
exports.stopAddons = stopAddons; exports.stopAddons = stopAddons;
exports.startAddons = startAddons; exports.startAddons = startAddons;
exports.clean = clean; exports.clean = clean;
exports.publish_state = publish_state;

View File

@ -1,4 +1,5 @@
const fs = require("fs"); const fs = require("fs");
const hassioApiTools = require("./hassioApiTools");
const statusPath = "/data/status.json"; const statusPath = "/data/status.json";
@ -33,11 +34,14 @@ function setStatus(state) {
let old_state_str = fs.readFileSync(statusPath).toString(); let old_state_str = fs.readFileSync(statusPath).toString();
if(old_state_str !== JSON.stringify(state)){ if(old_state_str !== JSON.stringify(state)){
fs.writeFileSync(statusPath, JSON.stringify(state)); fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
} }
}else{ }else{
fs.writeFileSync(statusPath, JSON.stringify(state)); fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
} }
} }
exports.init = init; exports.init = init;