From 5428e482b8dfeea3b6109dbaff3cff0643c439cb Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Tue, 16 May 2023 22:29:44 +0200 Subject: [PATCH 1/3] hasensors: inline config There is no need for a config file: we can customize the code directly. --- apps/hasensors/ChangeLog | 3 ++- apps/hasensors/custom.html | 30 +++++++++++++++++++++++------- apps/hasensors/lib.js | 13 +++++-------- apps/hasensors/metadata.json | 5 +---- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/apps/hasensors/ChangeLog b/apps/hasensors/ChangeLog index 759f68777..2c904fc71 100644 --- a/apps/hasensors/ChangeLog +++ b/apps/hasensors/ChangeLog @@ -1 +1,2 @@ -0.01: New app! \ No newline at end of file +0.01: New app! +0.02: Customize code directly, remove config file diff --git a/apps/hasensors/custom.html b/apps/hasensors/custom.html index 805001701..1fd1bcc3c 100644 --- a/apps/hasensors/custom.html +++ b/apps/hasensors/custom.html @@ -39,14 +39,27 @@ your user profile.

- +

diff --git a/apps/hasensors/lib.js b/apps/hasensors/lib.js index 60cfb6da4..555e52dce 100644 --- a/apps/hasensors/lib.js +++ b/apps/hasensors/lib.js @@ -1,16 +1,13 @@ // split out into a separate file to keep bootcode short. -function s(key) { - return (require('Storage').readJSON('hasensors.settings.js', true) || {})[key]; -} - +// placeholders are replaced by custom.html before upload function post(sensor, data) { - const url = s('url') + '/api/states/sensor.' + s('id') + '_' + sensor; + const url = '{url}/api/states/sensor.{id}_' + sensor; Bangle.http(url, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', - Authorization: 'Bearer ' + s('token'), + Authorization: 'Bearer {token}', } }); } @@ -20,7 +17,7 @@ exports.sendBattery = function () { post('battery_level', { state: E.getBattery(), attributes: { - friendly_name: s('name') + " Battery Level", + friendly_name: "{name} Battery Level", unit_of_measurement: "%", device_class: "battery", state_class: "measurement", @@ -29,7 +26,7 @@ exports.sendBattery = function () { post('battery_state', { state: Bangle.isCharging() ? 'charging' : 'discharging', attributes: { - friendly_name: s('name') + " Battery State", + friendly_name: "{name} Battery State", } }); } \ No newline at end of file diff --git a/apps/hasensors/metadata.json b/apps/hasensors/metadata.json index 7713fadc7..106f11407 100644 --- a/apps/hasensors/metadata.json +++ b/apps/hasensors/metadata.json @@ -2,7 +2,7 @@ "id": "hasensors", "name": "Home Assistant Sensors", "shortName": "HA sensors", - "version": "0.01", + "version": "0.02", "description": "Send sensor values to Home Assistant using the Android Integration.", "icon": "ha.png", "type": "bootloader", @@ -14,8 +14,5 @@ "storage": [ {"name":"hasensors","url":"lib.js"}, {"name":"hasensors.boot.js","url":"boot.js"} - ], - "data": [ - {"name":"hasensors.settings.json"} ] } From b500e96b3601a2cee80f06d7f8101023d519707a Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Tue, 16 May 2023 22:54:48 +0200 Subject: [PATCH 2/3] 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 From b79a1caae60df3678610206773860bd493b28995 Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Tue, 16 May 2023 23:04:08 +0200 Subject: [PATCH 3/3] hasensors: cleanup: use double quotes everywhere --- apps/hasensors/boot.js | 2 +- apps/hasensors/custom.html | 14 +++++++------- apps/hasensors/lib.js | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/hasensors/boot.js b/apps/hasensors/boot.js index a9122be5d..efafbc8a3 100644 --- a/apps/hasensors/boot.js +++ b/apps/hasensors/boot.js @@ -1,5 +1,5 @@ (function () { - const sb = () => require('hasensors').sendBattery(); + const sb = () => require("hasensors").sendBattery(); Bangle.on("charging", sb); NRF.on("connect", () => setTimeout(sb, 2000)); setInterval(sb, 10 * 60 * 1000); diff --git a/apps/hasensors/custom.html b/apps/hasensors/custom.html index 1fd1bcc3c..265f80f46 100644 --- a/apps/hasensors/custom.html +++ b/apps/hasensors/custom.html @@ -43,19 +43,19 @@