|
| 1 | +// Home Assistant Lockscreen Widget |
| 2 | +// Details |
| 3 | +// - Used on lockscreen to show 2 temperature sensors with binary state |
| 4 | + |
| 5 | +// Confguration |
| 6 | +// EDIT HERE |
| 7 | + |
| 8 | +const hassUrl = "<hass base url>" |
| 9 | +const hassToken = "<your long lived Bearer token>" |
| 10 | + |
| 11 | +let widget = await createWidget(); |
| 12 | +if (!config.runsInWidget) { |
| 13 | + // await widget.presentSmall(); |
| 14 | + await widget.presentMedium(); |
| 15 | +} |
| 16 | + |
| 17 | +Script.setWidget(widget); |
| 18 | +Script.complete(); |
| 19 | + |
| 20 | +async function createWidget(items) { |
| 21 | + let req = new Request(`${hassUrl}/api/states`) |
| 22 | + req.headers = { |
| 23 | + "Authorization": `Bearer ${hassToken}`, |
| 24 | + "content-type": "application/json" |
| 25 | + } |
| 26 | + let json = await req.loadJSON(); |
| 27 | + |
| 28 | + /* Parse data received from API */ |
| 29 | + let data = {livingroom: {}} |
| 30 | + |
| 31 | + data.livingroom = addData(json, data.livingroom, [ |
| 32 | + 'sensor.boiler_temp' |
| 33 | + ,'sensor.storage_temp' |
| 34 | + ,'binary_sensor.blower' |
| 35 | + ]); |
| 36 | + |
| 37 | + /* Create the widget */ |
| 38 | + const widget = new ListWidget(); |
| 39 | + widget.backgroundColor = new Color("#03a9f4", 1.0); |
| 40 | + |
| 41 | + /* Add the sensor entries */ |
| 42 | + const bodyStack = widget.addStack(); |
| 43 | + |
| 44 | + /* First, the label column */ |
| 45 | + const labelStack = bodyStack.addStack(); |
| 46 | + labelStack.centerAlignContent(); |
| 47 | + labelStack.setPadding(0, 0, 0, 0); |
| 48 | + labelStack.borderWidth = 0; |
| 49 | + labelStack.size = new Size(50,50); |
| 50 | + addLabel(labelStack, "🔥") |
| 51 | + |
| 52 | + /* Second, the temperature column */ |
| 53 | + const tempStack = bodyStack.addStack(); |
| 54 | + tempStack.centerAlignContent(); |
| 55 | + tempStack.setPadding(0, 11, 0, 0); |
| 56 | + tempStack.borderWidth = 0; |
| 57 | + tempStack.size = new Size(0,50); |
| 58 | + tempStack.layoutVertically(); |
| 59 | + |
| 60 | + tempStack.addText(" 🌡️") |
| 61 | + addTemp(tempStack, data.livingroom) |
| 62 | + |
| 63 | + /* Third, the humidity column */ |
| 64 | + const storageStack = bodyStack.addStack(); |
| 65 | + storageStack.centerAlignContent(); |
| 66 | + storageStack.setPadding(0, 5, 0, 0); |
| 67 | + storageStack.borderWidth = 0; |
| 68 | + storageStack.size = new Size(0,50); |
| 69 | + storageStack.layoutVertically(); |
| 70 | + |
| 71 | + storageStack.addText("💧") |
| 72 | + addStorage(storageStack, data.livingroom) |
| 73 | + |
| 74 | + /* Done: Widget is now ready to be displayed */ |
| 75 | + return widget; |
| 76 | +} |
| 77 | + |
| 78 | +/* Adds the entries to the label column */ |
| 79 | +async function addLabel(labelStack, label) { |
| 80 | + const mytext = labelStack.addText(label); |
| 81 | + mytext.font = Font.semiboldSystemFont(40); |
| 82 | + mytext.textColor = Color.black(); |
| 83 | +} |
| 84 | + |
| 85 | +/* Adds the entries to the temperature column */ |
| 86 | +async function addTemp(tempStack, data) { |
| 87 | + const mytext = tempStack.addText(data.temp + "°F"); |
| 88 | + mytext.font = Font.heavyMonospacedSystemFont(13); |
| 89 | + mytext.textColor = Color.white(); |
| 90 | +} |
| 91 | + |
| 92 | +/* Adds the entries to the humidity column */ |
| 93 | +async function addStorage(storageStack, data) { |
| 94 | + const mytext = storageStack.addText(data.humid + "°F"); |
| 95 | + mytext.font = Font.mediumMonospacedSystemFont(13); |
| 96 | + mytext.textColor = Color.white(); |
| 97 | + mytext.textOpacity = 0.8; |
| 98 | +} |
| 99 | + |
| 100 | +/* Searches for the respective sensor values ('state') in the API response of Home Assistant */ |
| 101 | +function addData(json, room, sensors) { |
| 102 | + room.temp = "N/A"; |
| 103 | + room.humid = "N/A"; |
| 104 | + var i; |
| 105 | + for (i = 0; i < json.length; i++) { |
| 106 | + if (json[i]['entity_id'] == sensors[0]) { |
| 107 | + room.temp = Number(json[i]['state']); |
| 108 | + room.temp = room.temp.toFixed(1); |
| 109 | + } |
| 110 | + if (json[i]['entity_id'] == sensors[1]) { |
| 111 | + room.humid = Math.round(json[i]['state']); |
| 112 | + } |
| 113 | + } |
| 114 | + return room; |
| 115 | +} |
0 commit comments