Skip to content

Instantly share code, notes, and snippets.

@felixtsu
Forked from m33x/hass.js
Last active February 25, 2024 10:02
Show Gist options
  • Select an option

  • Save felixtsu/15c62e968beea820a447bddb1dd84e69 to your computer and use it in GitHub Desktop.

Select an option

Save felixtsu/15c62e968beea820a447bddb1dd84e69 to your computer and use it in GitHub Desktop.
Simple Home Assistant (HASS) iOS Widget via Scriptable App
let widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function createWidget(items) {
let req = new Request("http://<HA SERVER>/api/states")
req.headers = { "Authorization": "Bearer <REFRESH TOKEN>", "content-type": "application/json" }
let json = await req.loadJSON();
/* Parse data received from API */
let data = {dishWasher : {}, washer : {}, dryer : {}}
data.dishWasher = findEntity(json, "sensor.dishwasher_time_remaining")
data.washer = findEntity(json, "sensor.washer_time_remaining")
data.dryer = findEntity(json, "sensor.dryer_time_remaining")
/* Create the widget */
const widget = new ListWidget();
//widget.backgroundColor = new Color("#03a9f4", 1.0);
/* Design the widget header */
let headerStack = widget.addStack();
const titleStack = headerStack.addStack();
headerStack.addSpacer(7);
/* Add the name of this Home Assistant */
const titleLabel = titleStack.addText("Time Remaining");
titleStack.setPadding(2, 0, 10, 0);
titleLabel.font = Font.heavyMonospacedSystemFont(16);
titleLabel.textColor = Color.dynamic(Color.black(), Color.white());
/* Add the sensor entries */
const bodyStack = widget.addStack();
/* First, the label column */
const labelStack = bodyStack.addStack();
labelStack.setPadding(0, 0, 0, 0);
labelStack.borderWidth = 0;
labelStack.layoutVertically();
addLabel(labelStack, "洗碗机")
addLabel(labelStack, "洗衣机")
addLabel(labelStack, "干衣机")
/* Second, the temperature column */
const remainingStack = bodyStack.addStack();
remainingStack.setPadding(0, 3, 0, 0);
remainingStack.borderWidth = 0;
remainingStack.layoutVertically();
addRemaining(remainingStack, data.dishWasher)
addRemaining(remainingStack, data.washer)
addRemaining(remainingStack, data.dryer)
//addRemainingOrSchedule(remainingStack, data, "washer");
//addRemainingOrSchedule(remainingStack, data, "dryer");
/* Done: Widget is now ready to be displayed */
return widget;
}
async function addRemainingOrSchedule(stack, data, entity_name) {
if (entity_name == "washer" ) {
// 再有time remaining的前提下,判断start是否已经开始,yes则是运行中,no则是预约中
addRemaining(remainingStack, data.washer)
} else if (entity_name == "dryer") {
addRemaining(remainingStack, data.dryer)
}
}
/* Adds the entries to the label column */
async function addLabel(labelStack, label) {
const mytext = labelStack.addText(label + ":");
mytext.font = Font.regularSystemFont(14);
mytext.textColor = Color.dynamic(Color.black(), Color.white());
}
async function addRemaining(remaingStack, data) {
let time_remaining = data.state
let mytext = null
if (time_remaining == 0 || time_remaining == 'unknown') {
mytext = remaingStack.addText("Done");
} else {
mytext = remaingStack.addText(time_remaining + " mins");
}
mytext.font = Font.regularMonospacedSystemFont(14);
mytext.textColor = Color.dynamic(Color.black(), Color.white());
}
function findEntity(json, entity_name) {
for (let i = 0; i < json.length; i++) {
if (json[i]['entity_id'] == entity_name) {
return json[i]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment