hasensors: inline config

There is no need for a config file: we can customize the code directly.
master
Richard de Boer 2023-05-16 22:29:44 +02:00
parent 1f30066c7d
commit 5428e482b8
No known key found for this signature in database
4 changed files with 31 additions and 20 deletions

View File

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

View File

@ -39,14 +39,27 @@
<a href="https://my.home-assistant.io/redirect/profile/" target="_blank">your user profile</a>.</span></label>
</form>
<p>
<button id="upload" class="btn btn-primary">Upload</button>
<button id="upload" class="btn btn-primary" disabled>Upload</button>
</p>
<script src="../../core/lib/customize.js"></script>
<script>
const STORAGE_KEY = 'hasensors-config';
const fields = ['id', 'name', 'url', 'token'];
const form = document.getElementById('sensorform');
const LIBRARY_URL = './lib.js';
// fetch library code template, enable upload button once we've got it
let libTpl;
fetch(LIBRARY_URL).then(response=>{
if (! response.ok) return;
console.log(response);
response.text().then(code=>{
libTpl = code;
document.getElementById('upload').disabled = false;
});
});
// try to pre-fill form with values previously saved in localStorage
let stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
try {
@ -62,7 +75,7 @@
}
document.getElementById("upload").addEventListener("click", function () {
let config = {};
// validate form fields or bail out
for (const field of fields) {
if (!form[field].validity.valid) {
form[field].focus();
@ -70,18 +83,21 @@
return;
}
}
let config = {};
for (const field of fields) {
config[field] = form[field].value
config[field] = form[field].value;
}
console.log('config:', config, JSON.stringify(config));
// save config to localStorage for re-use next time
localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
// replace {placeholders} in library code template
const lib = libTpl.replace(/\{(\w+)\}/g, (_,f) => config[f]);
console.log('config:', config, JSON.stringify(config));
sendCustomizedApp({
id: "hasensors",
storage: [
{name: "hasensors.boot.js", url: "boot.js"},
{name: "hasensors", url: "lib.js"},
{name: "hasensors.settings.json", content: JSON.stringify(config)},
]
{name: "hasensors", content: lib},
],
});
});
</script>

View File

@ -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",
}
});
}

View File

@ -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"}
]
}