hasensors: add icons

Home Assistant already sets a level-dependant icon, but doesn't know
how to apply the charging state.
master
Richard de Boer 2023-05-16 22:54:48 +02:00
parent 5428e482b8
commit b500e96b36
No known key found for this signature in database
2 changed files with 20 additions and 8 deletions

View File

@ -1,2 +1,3 @@
0.01: New app!
0.02: Customize code directly, remove config file
0.02: Add sensor icons
Customize code directly, remove config file

View File

@ -14,19 +14,30 @@ function post(sensor, data) {
exports.sendBattery = function () {
if (!NRF.getSecurityStatus().connected) return;
const b = E.getBattery(),
c = Bangle.isCharging();
let i = "mdi:battery";
if (c) i += "-charging";
post('battery_state', {
state: c ? 'charging' : 'discharging',
attributes: {
friendly_name: "{name} Battery State",
icon: i + (c ? "" : "-minus"),
}
});
if (b<10) i += "-outline"; // there is no battery-0
else if (b<100 || c) i += "-" + Math.floor(b/10)*10; // no battery-100 either
post('battery_level', {
state: E.getBattery(),
state: b,
attributes: {
friendly_name: "{name} Battery Level",
unit_of_measurement: "%",
device_class: "battery",
state_class: "measurement",
}
});
post('battery_state', {
state: Bangle.isCharging() ? 'charging' : 'discharging',
attributes: {
friendly_name: "{name} Battery State",
icon: i,
}
});
}