From a33cceda7b5b72137c26fc03e3997b8cbd1546b4 Mon Sep 17 00:00:00 2001 From: Florian Schaal Date: Thu, 16 Feb 2023 20:23:35 +0100 Subject: [PATCH 1/2] started work on driver settings persist --- .../device.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/adurosmart-eria-power-analytics-plug/device.ts b/drivers/adurosmart-eria-power-analytics-plug/device.ts index 2b127e2..9a899e8 100644 --- a/drivers/adurosmart-eria-power-analytics-plug/device.ts +++ b/drivers/adurosmart-eria-power-analytics-plug/device.ts @@ -107,6 +107,23 @@ class PowerAnalyticsPlug extends ZigBeeDevice { }, }); } + + async OnSettings({ oldSettings, newSettings, changedKeys }: any) { + try{ + console.log(newSettings); + if (changedKeys.includes('report_interval_OnOff')) { + + } + + if (changedKeys.includes('report_interval_measure')) { + + } + + }catch (err) { + // reset settings values on failed update + throw new Error(`failed to update settings. Message:${err}`); + } + } } module.exports = PowerAnalyticsPlug; From 303dde615243919dfc2b17478afad30d15c7faf0 Mon Sep 17 00:00:00 2001 From: Florian Schaal Date: Wed, 1 Mar 2023 14:16:41 +0100 Subject: [PATCH 2/2] Added functions for setting device capability --- app.json | 12 +-- drivers/adurosmart-eria-dim-plug/device.ts | 30 ++++-- .../driver.compose.json | 22 +---- .../driver.settings.compose.json | 20 ++++ drivers/adurosmart-eria-on-off-plug/device.ts | 17 +++- .../driver.compose.json | 22 +---- .../driver.settings.compose.json | 20 ++++ .../device.ts | 99 ++++++++++--------- .../driver.compose.json | 40 +------- .../driver.settings.compose.json | 38 +++++++ 10 files changed, 180 insertions(+), 140 deletions(-) create mode 100644 drivers/adurosmart-eria-dim-plug/driver.settings.compose.json create mode 100644 drivers/adurosmart-eria-on-off-plug/driver.settings.compose.json create mode 100644 drivers/adurosmart-eria-power-analytics-plug/driver.settings.compose.json diff --git a/app.json b/app.json index 72fd6f3..343e195 100644 --- a/app.json +++ b/app.json @@ -130,6 +130,7 @@ "template": "add_devices" } ], + "id": "adurosmart-eria-dim-plug", "settings": [ { "id": "report_interval_OnOff", @@ -149,8 +150,7 @@ "max": 86400 } } - ], - "id": "adurosmart-eria-dim-plug" + ] }, { "name": { @@ -212,6 +212,7 @@ "template": "add_devices" } ], + "id": "adurosmart-eria-on-off-plug", "settings": [ { "id": "report_interval_OnOff", @@ -231,8 +232,7 @@ "max": 86400 } } - ], - "id": "adurosmart-eria-on-off-plug" + ] }, { "name": { @@ -300,6 +300,7 @@ "template": "add_devices" } ], + "id": "adurosmart-eria-power-analytics-plug", "settings": [ { "id": "report_interval_OnOff", @@ -337,8 +338,7 @@ "max": 86400 } } - ], - "id": "adurosmart-eria-power-analytics-plug" + ] }, { "name": { diff --git a/drivers/adurosmart-eria-dim-plug/device.ts b/drivers/adurosmart-eria-dim-plug/device.ts index f860035..2353f5f 100644 --- a/drivers/adurosmart-eria-dim-plug/device.ts +++ b/drivers/adurosmart-eria-dim-plug/device.ts @@ -14,21 +14,37 @@ class DimPlug extends ZigBeeDevice { // print the node's info to the console //this.printNode(); + this.registerOnOffCapability(this.getSettings('report_interval_OnOff')); + + if (this.hasCapability('dim')) { + this.registerCapability("dim", CLUSTER.LEVEL_CONTROL, { + endpoint: 1 + }); + } + } + + async onSettings({oldSettings, newSettings, changedKeys}: any) { + + try { + if (changedKeys.includes('report_interval_OnOff')) { + this.registerOnOffCapability(newSettings.report_interval_OnOff); + } + } catch (err) { + // reset settings values on failed update + throw new Error(`failed to update settings. Message:${err}`); + } + } + + registerOnOffCapability(pollInterval: number = 60000) { if (this.hasCapability('onoff')) { this.registerCapability('onoff', CLUSTER.ON_OFF, { getOpts: { // If the power plug is controlled by homey the state will be switch directly // If power plug is toggled on the plug itself often this value can be changed to fit the users needs in the settings - pollInterval: this.getSetting('report_interval_OnOff') * 1000 || 60000, + pollInterval: pollInterval * 1000 || 60000, }, }); } - - if (this.hasCapability('dim')) { - this.registerCapability("dim", CLUSTER.LEVEL_CONTROL, { - endpoint: 1 - }); - } } } diff --git a/drivers/adurosmart-eria-dim-plug/driver.compose.json b/drivers/adurosmart-eria-dim-plug/driver.compose.json index 674ff87..6445c7d 100644 --- a/drivers/adurosmart-eria-dim-plug/driver.compose.json +++ b/drivers/adurosmart-eria-dim-plug/driver.compose.json @@ -47,25 +47,5 @@ "id": "add_devices", "template": "add_devices" } - ], - "settings": [ - { - "id": "report_interval_OnOff", - "type": "number", - "label": { - "en": "Report Interval On / Off", - "nl": "Report Interval Aan / Uit" - }, - "hint": { - "en": "This setting determines the interval on which Homey will check the current state of the device.", - "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." - }, - "value": 60, - "attr": { - "step": 1, - "min": 1, - "max": 86400 - } - } ] -} \ No newline at end of file +} diff --git a/drivers/adurosmart-eria-dim-plug/driver.settings.compose.json b/drivers/adurosmart-eria-dim-plug/driver.settings.compose.json new file mode 100644 index 0000000..f51b212 --- /dev/null +++ b/drivers/adurosmart-eria-dim-plug/driver.settings.compose.json @@ -0,0 +1,20 @@ +[ + { + "id": "report_interval_OnOff", + "type": "number", + "label": { + "en": "Report Interval On / Off", + "nl": "Report Interval Aan / Uit" + }, + "hint": { + "en": "This setting determines the interval on which Homey will check the current state of the device.", + "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." + }, + "value": 60, + "attr": { + "step": 1, + "min": 1, + "max": 86400 + } + } +] diff --git a/drivers/adurosmart-eria-on-off-plug/device.ts b/drivers/adurosmart-eria-on-off-plug/device.ts index 3489c96..82f0d21 100644 --- a/drivers/adurosmart-eria-on-off-plug/device.ts +++ b/drivers/adurosmart-eria-on-off-plug/device.ts @@ -14,12 +14,27 @@ class OnOffPlug extends ZigBeeDevice { // print the node's info to the console //this.printNode(); + this.registerOnOffCapability(this.getSettings('report_interval_OnOff')); + } + async onSettings({oldSettings, newSettings, changedKeys}: any) { + + try { + if (changedKeys.includes('report_interval_OnOff')) { + this.registerOnOffCapability(newSettings.report_interval_OnOff); + } + } catch (err) { + // reset settings values on failed update + throw new Error(`failed to update settings. Message:${err}`); + } + } + + registerOnOffCapability(pollInterval: number = 60000) { if (this.hasCapability('onoff')) { this.registerCapability('onoff', CLUSTER.ON_OFF, { getOpts: { // If the power plug is controlled by homey the state will be switch directly // If power plug is toggled on the plug itself often this value can be changed to fit the users needs in the settings - pollInterval: this.getSetting('report_interval_OnOff') * 1000 || 60000, + pollInterval: pollInterval * 1000 || 60000, }, }); } diff --git a/drivers/adurosmart-eria-on-off-plug/driver.compose.json b/drivers/adurosmart-eria-on-off-plug/driver.compose.json index 15fa81a..cacd201 100644 --- a/drivers/adurosmart-eria-on-off-plug/driver.compose.json +++ b/drivers/adurosmart-eria-on-off-plug/driver.compose.json @@ -57,25 +57,5 @@ "id": "add_devices", "template": "add_devices" } - ], - "settings": [ - { - "id": "report_interval_OnOff", - "type": "number", - "label": { - "en": "Report Interval On / Off", - "nl": "Report Interval Aan / Uit" - }, - "hint": { - "en": "This setting determines the interval on which Homey will check the current state of the device.", - "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." - }, - "value": 60, - "attr": { - "step": 1, - "min": 1, - "max": 86400 - } - } ] -} \ No newline at end of file +} diff --git a/drivers/adurosmart-eria-on-off-plug/driver.settings.compose.json b/drivers/adurosmart-eria-on-off-plug/driver.settings.compose.json new file mode 100644 index 0000000..f51b212 --- /dev/null +++ b/drivers/adurosmart-eria-on-off-plug/driver.settings.compose.json @@ -0,0 +1,20 @@ +[ + { + "id": "report_interval_OnOff", + "type": "number", + "label": { + "en": "Report Interval On / Off", + "nl": "Report Interval Aan / Uit" + }, + "hint": { + "en": "This setting determines the interval on which Homey will check the current state of the device.", + "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." + }, + "value": 60, + "attr": { + "step": 1, + "min": 1, + "max": 86400 + } + } +] diff --git a/drivers/adurosmart-eria-power-analytics-plug/device.ts b/drivers/adurosmart-eria-power-analytics-plug/device.ts index 9a899e8..6f29f7e 100644 --- a/drivers/adurosmart-eria-power-analytics-plug/device.ts +++ b/drivers/adurosmart-eria-power-analytics-plug/device.ts @@ -6,23 +6,17 @@ class PowerAnalyticsPlug extends ZigBeeDevice { async onNodeInit({zclNode}: any) { // enable debugging - this.enableDebug(); + //this.enableDebug(); // Enables debug logging in zigbee-clusters - debug(true); + //debug(true); // print the node's info to the console - this.printNode(); + //this.printNode(); - if (this.hasCapability('onoff')) { - this.registerCapability('onoff', CLUSTER.ON_OFF, { - getOpts: { - // If the power plug is controlled by homey the state will be switch directly - // If power plug is toggled on the plug itself often this value can be changed to fit the users needs in the settings - pollInterval: this.getSetting('report_interval_OnOff') * 1000 || 60000, - }, - }); - } + this.registerOnOffCapability(this.getSettings('report_interval_OnOff')); + + this.registerMeasurePowerCapability(this.getSetting('report_interval_measure')); if (this.hasCapability('meter_power')) { this.registerCapability('meter_power', CLUSTER.METERING, { @@ -49,33 +43,6 @@ class PowerAnalyticsPlug extends ZigBeeDevice { }); } - if (this.hasCapability('measure_power')) { - this.registerCapability('measure_power', CLUSTER.ELECTRICAL_MEASUREMENT, { - get: 'activePower', - getOpts: { - getOnStart: true, - getOnOnline: true, - // If you are not actively triggering based on power change it's not needed to fetch data every second. - // This can be changed in settings to fit the users needs - pollInterval: this.getSetting('report_interval_measure') * 1000 || 60000, - }, - reportOpts: { - configureAttributeReporting: { - minInterval: 0, // No minimum reporting interval - maxInterval: 300, // Maximally every ~16 hours - minChange: 1, // Report when value changed by 1 - }, - }, - report: 'activePower', - reportParser(report: number) { - console.log(`reported active power: ${report}`) - if(report < 0) return 0; - return report / 10 - }, - endpoint: this.getClusterEndpoint(CLUSTER.ELECTRICAL_MEASUREMENT), - }); - } - if (this.hasCapability('measure_voltage')) { this.registerCapability("measure_voltage", CLUSTER.ELECTRICAL_MEASUREMENT, { get: 'rmsVoltage', @@ -108,22 +75,64 @@ class PowerAnalyticsPlug extends ZigBeeDevice { }); } - async OnSettings({ oldSettings, newSettings, changedKeys }: any) { - try{ - console.log(newSettings); - if (changedKeys.includes('report_interval_OnOff')) { + async onSettings({oldSettings, newSettings, changedKeys}: any) { + try { + if (changedKeys.includes('report_interval_OnOff')) { + this.registerOnOffCapability(newSettings.report_interval_OnOff) } if (changedKeys.includes('report_interval_measure')) { - + this.registerMeasurePowerCapability(newSettings.report_interval_measure); } - }catch (err) { + } catch (err) { // reset settings values on failed update throw new Error(`failed to update settings. Message:${err}`); } } + + registerOnOffCapability(pollInterval: number = 60000) { + if (this.hasCapability('onoff')) { + this.registerCapability('onoff', CLUSTER.ON_OFF, { + getOpts: { + // If the power plug is controlled by homey the state will be switch directly + // If power plug is toggled on the plug itself often this value can be changed to fit the users needs in the settings + pollInterval: pollInterval * 1000 || 60000, + }, + }); + } + } + + registerMeasurePowerCapability(pollInterval: number = 60000) { + if (this.hasCapability('measure_power')) { + this.registerCapability('measure_power', CLUSTER.ELECTRICAL_MEASUREMENT, { + get: 'activePower', + getOpts: { + getOnStart: true, + getOnOnline: true, + // If you are not actively triggering based on power change it's not needed to fetch data every second. + // This can be changed in settings to fit the users needs + pollInterval: pollInterval * 1000 || 60000, + }, + reportOpts: { + configureAttributeReporting: { + minInterval: 0, // No minimum reporting interval + maxInterval: 300, // Maximally every ~16 hours + minChange: 1, // Report when value changed by 1 + }, + }, + report: 'activePower', + reportParser(report: number) { + console.log(`reported active power: ${report}`) + console.log(`Current interval from settings: ${pollInterval}`) + if (report < 0) return 0; + return report / 10 + }, + endpoint: this.getClusterEndpoint(CLUSTER.ELECTRICAL_MEASUREMENT), + }); + } + } } module.exports = PowerAnalyticsPlug; diff --git a/drivers/adurosmart-eria-power-analytics-plug/driver.compose.json b/drivers/adurosmart-eria-power-analytics-plug/driver.compose.json index 4cc9224..8287d8f 100644 --- a/drivers/adurosmart-eria-power-analytics-plug/driver.compose.json +++ b/drivers/adurosmart-eria-power-analytics-plug/driver.compose.json @@ -63,43 +63,5 @@ "id": "add_devices", "template": "add_devices" } - ], - "settings": [ - { - "id": "report_interval_OnOff", - "type": "number", - "label": { - "en": "Report Interval On / Off", - "nl": "Report Interval Aan / Uit" - }, - "hint": { - "en": "This setting determines the interval on which Homey will check the current state of the device.", - "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." - }, - "value": 60, - "attr": { - "step": 1, - "min": 1, - "max": 86400 - } - }, - { - "id": "report_interval_measure", - "type": "number", - "label": { - "en": "Report Interval Current Power Consumption", - "nl": "Report Interval Huidige Stroom Verbruik" - }, - "hint": { - "en": "This setting determines the interval on which Homey will check the current state of the device.", - "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." - }, - "value": 60, - "attr": { - "step": 1, - "min": 1, - "max": 86400 - } - } ] -} \ No newline at end of file +} diff --git a/drivers/adurosmart-eria-power-analytics-plug/driver.settings.compose.json b/drivers/adurosmart-eria-power-analytics-plug/driver.settings.compose.json new file mode 100644 index 0000000..2819f81 --- /dev/null +++ b/drivers/adurosmart-eria-power-analytics-plug/driver.settings.compose.json @@ -0,0 +1,38 @@ +[ + { + "id": "report_interval_OnOff", + "type": "number", + "label": { + "en": "Report Interval On / Off", + "nl": "Report Interval Aan / Uit" + }, + "hint": { + "en": "This setting determines the interval on which Homey will check the current state of the device.", + "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." + }, + "value": 60, + "attr": { + "step": 1, + "min": 1, + "max": 86400 + } + }, + { + "id": "report_interval_measure", + "type": "number", + "label": { + "en": "Report Interval Current Power Consumption", + "nl": "Report Interval Huidige Stroom Verbruik" + }, + "hint": { + "en": "This setting determines the interval on which Homey will check the current state of the device.", + "nl": "Deze instelling bepaalt de waarde waarop Homey de huidige status ophaalt bij het apparaat." + }, + "value": 60, + "attr": { + "step": 1, + "min": 1, + "max": 86400 + } + } +]