BangleApps/apps/ha/ha.app.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-06-27 14:36:26 +00:00
/**
* This app uses the ha library to send trigger to HomeAssistant.
*/
var ha = require("ha.lib.js");
2022-06-20 11:36:49 +00:00
var W = g.getWidth(), H = g.getHeight();
var position=0;
2022-06-27 14:36:26 +00:00
var triggers = ha.getTriggers();
2022-06-24 10:45:11 +00:00
2022-06-20 11:36:49 +00:00
function draw() {
g.reset().clearRect(Bangle.appRect);
2022-06-23 18:32:39 +00:00
var h = 22;
2022-06-24 06:33:14 +00:00
g.setFont("Vector", h);
var trigger = triggers[position];
var w = g.stringWidth(trigger.display);
2022-06-20 11:36:49 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(-1,-1);
2022-06-27 14:36:26 +00:00
var icon = ha.getIcon(trigger.getIcon());
2022-06-24 06:33:14 +00:00
g.setColor(g.theme.fg).drawImage(icon, 12, H/5-2);
g.drawString("Home", icon.width + 20, H/5);
g.drawString("Assistant", icon.width + 18, H/5+24);
2022-06-23 18:06:53 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(0,0);
var ypos = H/5*3+20;
2022-06-24 06:46:31 +00:00
g.drawRect(W/2-w/2-8, ypos-h/2-8, W/2+w/2+5, ypos+h/2+5);
g.fillRect(W/2-w/2-6, ypos-h/2-6, W/2+w/2+3, ypos+h/2+3);
g.setColor(g.theme.bg).drawString(trigger.display, W/2, ypos);
2022-06-20 11:36:49 +00:00
}
Bangle.on('touch', function(btn, e){
var left = parseInt(g.getWidth() * 0.3);
var right = g.getWidth() - left;
var isLeft = e.x < left;
var isRight = e.x > right;
if(isRight){
2022-06-23 18:06:53 +00:00
Bangle.buzz(40, 0.6);
2022-06-20 11:36:49 +00:00
position += 1;
position = position >= triggers.length ? 0 : position;
draw();
2022-06-20 11:36:49 +00:00
}
if(isLeft){
2022-06-23 18:06:53 +00:00
Bangle.buzz(40, 0.6);
2022-06-20 11:36:49 +00:00
position -= 1;
position = position < 0 ? triggers.length-1 : position;
draw();
2022-06-20 11:36:49 +00:00
}
if(!isRight && !isLeft){
// Now send the selected trigger
2022-06-23 18:06:53 +00:00
Bangle.buzz(80, 0.6).then(()=>{
2022-06-27 14:36:26 +00:00
ha.sendTrigger(triggers[position].trigger);
2022-06-23 18:06:53 +00:00
setTimeout(()=>{
Bangle.buzz(80, 0.6);
}, 250);
});
2022-06-20 11:36:49 +00:00
}
});
2022-06-27 14:36:26 +00:00
2022-06-24 10:45:11 +00:00
// Send intent that the we started the app.
2022-06-27 14:36:26 +00:00
ha.sendTrigger("APP_STARTED");
2022-06-24 06:33:14 +00:00
// Next load the widgets and draw the app
2022-06-20 11:36:49 +00:00
Bangle.loadWidgets();
2022-06-24 06:33:14 +00:00
Bangle.drawWidgets();
2022-06-27 14:36:26 +00:00
// Draw app
2022-06-24 06:33:14 +00:00
draw();
setWatch(_=>load(), BTN1);