From b500e96b3601a2cee80f06d7f8101023d519707a Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Tue, 16 May 2023 22:54:48 +0200 Subject: [PATCH] hasensors: add icons Home Assistant already sets a level-dependant icon, but doesn't know how to apply the charging state. --- apps/hasensors/ChangeLog | 3 ++- apps/hasensors/lib.js | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/hasensors/ChangeLog b/apps/hasensors/ChangeLog index 2c904fc71..7b3a63039 100644 --- a/apps/hasensors/ChangeLog +++ b/apps/hasensors/ChangeLog @@ -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 diff --git a/apps/hasensors/lib.js b/apps/hasensors/lib.js index 555e52dce..b8ee1bfb9 100644 --- a/apps/hasensors/lib.js +++ b/apps/hasensors/lib.js @@ -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, } }); } \ No newline at end of file