Skip to content

Commit

Permalink
Add a bunch of error logging for capability promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Joolee committed Feb 28, 2022
1 parent 054934c commit eadc3d5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 50 deletions.
9 changes: 5 additions & 4 deletions drivers/gpio_output_bool/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ module.exports = class GPIO_Bool_Device extends GPIODevice {
if ((pinStatus.state === 1) != homeyStatus) {
this.log('Pin', this.id, 'Updating state from ESP unit:', pinStatus.state);
this.setCapabilityValue(
this.capability,
(pinStatus.state == 1),
this.setAvailable
);
this.capability,
(pinStatus.state == 1),
this.setAvailable
)
.catch(this.error.bind(this, `Error setting capability [${this.capability}] value to ${pinStatus.state == 1} for gpio_output_bool device`));;
}

if (!this.getAvailable()) {
Expand Down
7 changes: 5 additions & 2 deletions drivers/p1/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,15 @@ module.exports = class P1_Device extends GeneralDevice {
setValue(capability, value) {
let hasCapability = this.hasCapability(capability);
if (!hasCapability && value !== undefined && value > 0) {
this.addCapability(capability);
this.addCapability(capability)
.catch(this.error.bind(this, `Error adding capability [${capability}] for P1 device`));;
hasCapability = true;
}

if (hasCapability && value !== undefined && this.getCapabilityValue(capability) != value)
if (hasCapability && value !== undefined && this.getCapabilityValue(capability) != value) {
this.setCapabilityValue(capability, value)
.catch(this.error.bind(this, `Error setting capability [${capability}] value to ${value} for p1 device`));
}
}

get port() {
Expand Down
25 changes: 16 additions & 9 deletions drivers/pulse_counter/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ module.exports = class Pulse_Counter_Device extends SensorDevice {
onSettings(oldSettings, newSettings, changedKeys, callback) {
if (changedKeys.includes("set_total")) {
const capability = this.getSetting("capability-0");
this.setCapabilityValue(capability, newSettings["set_total"]);
this.setCapabilityValue(capability, newSettings["set_total"])
.catch(this.error.bind(this, `Error setting capability [${capability}] value to new total '${newSettings["set_total"]}' for pulse_counter`));

const multiplier = Number(this.getSetting("value_multiplier"));
const raw = newSettings["set_total"] / multiplier;
this.setCapabilityValue("measure_raw_number", raw);
this.setCapabilityValue("measure_raw_number", raw)
.catch(this.error.bind(this, `Error setting capability [measure_raw_number] value to new total '${raw}' for pulse_counter`));

this.log("Total value manually set to", newSettings["set_total"], raw);
}
Expand All @@ -41,10 +43,12 @@ module.exports = class Pulse_Counter_Device extends SensorDevice {
const hasTotal = this.hasCapability("measure_sensor_value");
if (needsTotal && !hasTotal) {
this.log("Adding capability 'measure_sensor_value'");
this.addCapability("measure_sensor_value");
this.addCapability("measure_sensor_value")
.catch(this.error.bind(this, `Error adding capability [measure_sensor_value] for pulse_counter`));
} else if (hasTotal && !needsTotal) {
this.log("Removing capability 'measure_sensor_value'");
this.removeCapability("measure_sensor_value");
this.removeCapability("measure_sensor_value")
.catch(this.error.bind(this, `Error removing capability [measure_sensor_value] for pulse_counter`));
}
}

Expand All @@ -68,13 +72,15 @@ module.exports = class Pulse_Counter_Device extends SensorDevice {
// Total is the only thing I get so work with it
case "total-2":
total = Number(value);
this.setCapabilityValue("measure_sensor_value", total);
this.setCapabilityValue("measure_sensor_value", total)
.catch(this.error.bind(this, `Error setting capability [measure_sensor_value:1] value to '${total}' for pulse_counter`));
break;

// If I get delta's, only use totals when wanted
case "delta_total-2":
case "delta_total_time-2":
this.setCapabilityValue("measure_sensor_value", Number(value));
this.setCapabilityValue("measure_sensor_value", Number(value))
.catch(this.error.bind(this, `Error setting capability [measure_sensor_value:2] value to '${Number(value)}' for pulse_counter`));
if (this.getSetting("use_totals") || !oldValue) {
total = Number(value);
}
Expand All @@ -94,7 +100,8 @@ module.exports = class Pulse_Counter_Device extends SensorDevice {
}

if (total != this.getCapabilityValue("measure_raw_number")) {
this.setCapabilityValue("measure_raw_number", total);
this.setCapabilityValue("measure_raw_number", total)
.catch(this.error.bind(this, `Error setting capability [measure_raw_number] value to '${Number(value)}' for pulse_counter`));
}

value = Number(this.getSetting("value_multiplier")) * total;
Expand All @@ -105,10 +112,10 @@ module.exports = class Pulse_Counter_Device extends SensorDevice {
.catch((...args) => {
if (args[0].message && args[0].message == "invalid_type") {
const capabilityProperties = Homey.app.getCapability(capability);
this.log(`Invalid value type for capability '${capability}'. Got value '${value}' (${typeof value}) but expected type ${capabilityProperties.type}`);
this.error(`Invalid value type for capability '${capability}'. Got value '${value}' (${typeof value}) but expected type ${capabilityProperties.type}`);
} else {
args.push(capability, value);
this.log.apply(this, args);
this.error.apply(this, `Error setting capability [${capability}] value to ${value} for pulse_counter device`, args);
}
});
}
Expand Down
61 changes: 40 additions & 21 deletions drivers/unit/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,32 @@ module.exports = class UnitDevice extends Homey.Device {
this.migrateCapability("custom_load", "measure_load");
this.migrateCapability("custom_ram", "measure_ram");

if (!this.hasCapability("measure_signal_strength"))
this.addCapability("measure_signal_strength");
if (!this.hasCapability("measure_signal_strength")) {
this.addCapability("measure_signal_strength")
.catch(this.error.bind(this, `Error adding capability [measure_signal_strength] for unit`));
}

// I want both capabilities one is pretty-printed, the other is for tags and insights
if (!this.hasCapability("measure_uptime")) {
this.addCapability("measure_uptime");
this.addCapability("measure_uptime")
.catch(this.error.bind(this, `Error adding capability [measure_uptime] for unit`));

this.setCapabilityOptions("unit_uptime", {
"preventInsights": true,
"preventTag ": true
});
"preventInsights": true,
"preventTag ": true
})
.catch(this.error.bind(this, `Error setting capability options [measure_uptime] for unit`));
}

// I want both capabilities one is pretty-printed, the other is for tags and insights
if (!this.hasCapability("measure_idle_time")) {
this.addCapability("measure_idle_time");
this.addCapability("measure_idle_time")
.catch(this.error.bind(this, `Error adding capability [measure_idle_time] for unit`));
this.setCapabilityOptions("device_heartbeat", {
"preventInsights": true,
"preventTag ": true
});
"preventInsights": true,
"preventTag ": true
})
.catch(this.error.bind(this, `Error setting capability options [device_heartbeat] for unit`));
}
}

Expand All @@ -95,18 +104,24 @@ module.exports = class UnitDevice extends Homey.Device {

const idleTime = Math.floor((new Date().getTime() - this.unit.lastEvent.getTime()) / 60);
if (this.getCapabilityValue("measure_idle_time") != idleTime) {
this.setCapabilityValue("device_heartbeat", this.unit.lastEvent.toLocaleString());
this.setCapabilityValue("measure_idle_time", idleTime);
this.setCapabilityValue("device_heartbeat", this.unit.lastEvent.toLocaleString())
.catch(this.error.bind(this, `Error setting capability [device_heartbeat] value to '${this.unit.lastEvent.toLocaleString()}' for unit`));
this.setCapabilityValue("measure_idle_time", idleTime)
.catch(this.error.bind(this, `Error setting capability [measure_idle_time] value to '${idleTime}' for unit`));
}

const uptime = this.unit.json.System["Uptime"];
if (this.getCapabilityValue("measure_uptime") != uptime) {
this.setCapabilityValue("unit_uptime", uptime + " " + Homey.__("minutes"));
this.setCapabilityValue("measure_uptime", uptime);
this.setCapabilityValue("unit_uptime", uptime + " " + Homey.__("minutes"))
.catch(this.error.bind(this, `Error setting capability [unit_uptime] value to '${uptime} ${Homey.__("minutes")}' for unit`));
this.setCapabilityValue("measure_uptime", uptime)
.catch(this.error.bind(this, `Error setting capability [measure_uptime] value to '${uptime}' for unit`));
}
} else {
this.setCapabilityValue("measure_idle_time", null);
this.setCapabilityValue("measure_uptime", null);
this.setCapabilityValue("measure_idle_time", null)
.catch(this.error.bind(this, `Error setting capability [measure_idle_time] value to null for unit`));
this.setCapabilityValue("measure_uptime", null)
.catch(this.error.bind(this, `Error setting capability [measure_uptime] value to null for unit`));
}
}

Expand Down Expand Up @@ -177,14 +192,16 @@ module.exports = class UnitDevice extends Homey.Device {

// ESP32 chips don't supply this value
if (!this.hasCapability("measure_heap") && json.System["Heap Max Free Block"]) {
this.addCapability("measure_heap");
this.addCapability("measure_heap")
.catch(this.error.bind(this, `Error adding capability [measure_heap] for unit`));
}

if (this.hasCapability("measure_heap")) {
if (json.System["Heap Max Free Block"]) {
this.setValue("measure_heap", json.System["Heap Max Free Block"]);
} else {
this.removeCapability("measure_heap");
this.removeCapability("measure_heap")
.catch(this.error.bind(this, `Error removing capability [measure_heap] for unit`));
}
}

Expand All @@ -194,15 +211,17 @@ module.exports = class UnitDevice extends Homey.Device {
setValue(key, value) {
if (this.getCapabilityValue(key) != value) {
this.setCapabilityValue(key, value)
.catch(this.log);
.catch(this.error.bind(this, `Error setting capability [${key}] value to ${value} for unit`));
}
}

migrateCapability(oldCapability, newCapability) {
if (this.hasCapability(oldCapability)) {
this.log("Migrate capability", oldCapability, newCapability);
this.removeCapability(oldCapability);
this.addCapability(newCapability);
this.removeCapability(oldCapability)
.catch(this.error.bind(this, `Error removing old capability [${oldCapability}] for unit`));
this.addCapability(newCapability)
.catch(this.error.bind(this, `Error adding new capability [${newCapability}] for unit`));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ESPEasyUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,13 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass {
try {
fn(null, this)
} catch (error) {
this.log("Failed running callback", fn, error);
this.error("Failed running callback", fn, error);
}
});
this.updateJSONCallbacks = [];
this.emit("jsonUpdate", this, esp);
}).catch((error) => {
this.log("Error in updating json: ", error.message ? error.message : error);
this.error("Error in updating json: ", error);

if (error.message && error.message == "timeout") {
if (this.timeouts++ > 5) {
Expand Down
25 changes: 16 additions & 9 deletions lib/GeneralDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ module.exports = class GeneralDevice extends Homey.Device {
this.migrateCapability("custom_heartbeat", "device_heartbeat");

if (this.hasCapability("device_heartbeat") && !this.hasCapability("measure_idle_time")) {
this.addCapability("measure_idle_time");
this.addCapability("measure_idle_time")
.catch(this.error.bind(this, `Error adding capability [measure_idle_time] for general device ${this.constructor.name}`));;
this.setCapabilityOptions("device_heartbeat", {
"preventInsights": true,
"preventTag ": true
});
"preventInsights": true,
"preventTag ": true
})
.catch(this.error.bind(this, `Error setting capability values [measure_uptime] for unit`));
}
}

Expand All @@ -74,11 +76,14 @@ module.exports = class GeneralDevice extends Homey.Device {

const idleTime = Math.floor((new Date().getTime() - this.lastEvent.getTime()) / 60);
if (this.getCapabilityValue("measure_idle_time") != idleTime) {
this.setCapabilityValue("device_heartbeat", this.lastEvent.toLocaleString());
this.setCapabilityValue("measure_idle_time", idleTime);
this.setCapabilityValue("device_heartbeat", this.lastEvent.toLocaleString())
.catch(this.error.bind(this, `Error setting capability [device_heartbeat] value to '${this.lastEvent.toLocaleString()}' for general_device ${this.constructor.name}`));
this.setCapabilityValue("measure_idle_time", idleTime)
.catch(this.error.bind(this, `Error setting capability [measure_idle_time] value to '${idleTime}' for general_device ${this.constructor.name}`));
}
} else {
this.setCapabilityValue("measure_idle_time", null);
this.setCapabilityValue("measure_idle_time", null)
.catch(this.error.bind(this, `Error setting capability [measure_idle_time] value to 'null' for general_device ${this.constructor.name}`));
}
}

Expand All @@ -101,8 +106,10 @@ module.exports = class GeneralDevice extends Homey.Device {
migrateCapability(oldCapability, newCapability) {
if (this.hasCapability(oldCapability)) {
this.log("Migrate capability", oldCapability, newCapability);
this.removeCapability(oldCapability);
this.addCapability(newCapability);
this.removeCapability(oldCapability)
.catch(this.error.bind(this, `Error removing old capability [${oldCapability}] for general device ${this.constructor.name}`));
this.addCapability(newCapability)
.catch(this.error.bind(this, `Error adding new capability [${newCapability}] for unit ${this.constructor.name}`));
}
}
}
7 changes: 4 additions & 3 deletions lib/SensorDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ module.exports = class SensorDevice extends GeneralDevice {
case "number":
value = Number(value);
if (this.hasCapability("measure_raw_number")) {
this.setCapabilityValue("measure_raw_number", value);
this.setCapabilityValue("measure_raw_number", value)
.catch(this.error.bind(this, `Error setting capability [measure_raw_number] value to '${value}' for sensor ${this.constructor.name}`));
}
break;
}
Expand All @@ -192,10 +193,10 @@ module.exports = class SensorDevice extends GeneralDevice {
this.setCapabilityValue(capability, value)
.catch((...args) => {
if (args[0].message && args[0].message == "invalid_type") {
this.log(`Invalid value type for capability '${capability}'. Got value '${value}' (${typeof value}) from ${source} but expected type ${capabilityProperties.type}`);
this.error(`Invalid value type for capability '${capability}'. Got value '${value}' (${typeof value}) from ${source} but expected type ${capabilityProperties.type} for ${this.constructor.name}`);
} else {
args.push(capability, value);
this.log.apply(this, args);
this.error.apply(this, `Error setting capability [${capability}] value to ${value} for sensor device ${this.constructor.name}`, args);
}
});
}
Expand Down

0 comments on commit eadc3d5

Please sign in to comment.