diff --git a/drivers/gpio_output_bool/device.js b/drivers/gpio_output_bool/device.js index d38cda1..f27ea8f 100644 --- a/drivers/gpio_output_bool/device.js +++ b/drivers/gpio_output_bool/device.js @@ -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()) { diff --git a/drivers/p1/device.js b/drivers/p1/device.js index 516e7ef..bd772eb 100644 --- a/drivers/p1/device.js +++ b/drivers/p1/device.js @@ -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() { diff --git a/drivers/pulse_counter/device.js b/drivers/pulse_counter/device.js index da405b5..85a0935 100644 --- a/drivers/pulse_counter/device.js +++ b/drivers/pulse_counter/device.js @@ -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); } @@ -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`)); } } @@ -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); } @@ -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; @@ -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); } }); } diff --git a/drivers/unit/device.js b/drivers/unit/device.js index 4386df6..63bfcfa 100644 --- a/drivers/unit/device.js +++ b/drivers/unit/device.js @@ -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`)); } } @@ -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`)); } } @@ -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`)); } } @@ -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`)); } } diff --git a/lib/ESPEasyUnit.js b/lib/ESPEasyUnit.js index 03ca191..070e8ce 100644 --- a/lib/ESPEasyUnit.js +++ b/lib/ESPEasyUnit.js @@ -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) { diff --git a/lib/GeneralDevice.js b/lib/GeneralDevice.js index d217332..d177a90 100644 --- a/lib/GeneralDevice.js +++ b/lib/GeneralDevice.js @@ -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`)); } } @@ -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}`)); } } @@ -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}`)); } } } diff --git a/lib/SensorDevice.js b/lib/SensorDevice.js index f470fff..6b18e54 100644 --- a/lib/SensorDevice.js +++ b/lib/SensorDevice.js @@ -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; } @@ -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); } }); }