Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/persist device settings #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"template": "add_devices"
}
],
"id": "adurosmart-eria-dim-plug",
"settings": [
{
"id": "report_interval_OnOff",
Expand All @@ -149,8 +150,7 @@
"max": 86400
}
}
],
"id": "adurosmart-eria-dim-plug"
]
},
{
"name": {
Expand Down Expand Up @@ -212,6 +212,7 @@
"template": "add_devices"
}
],
"id": "adurosmart-eria-on-off-plug",
"settings": [
{
"id": "report_interval_OnOff",
Expand All @@ -231,8 +232,7 @@
"max": 86400
}
}
],
"id": "adurosmart-eria-on-off-plug"
]
},
{
"name": {
Expand Down Expand Up @@ -300,6 +300,7 @@
"template": "add_devices"
}
],
"id": "adurosmart-eria-power-analytics-plug",
"settings": [
{
"id": "report_interval_OnOff",
Expand Down Expand Up @@ -337,8 +338,7 @@
"max": 86400
}
}
],
"id": "adurosmart-eria-power-analytics-plug"
]
},
{
"name": {
Expand Down
30 changes: 23 additions & 7 deletions drivers/adurosmart-eria-dim-plug/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
}
}

Expand Down
22 changes: 1 addition & 21 deletions drivers/adurosmart-eria-dim-plug/driver.compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
}
20 changes: 20 additions & 0 deletions drivers/adurosmart-eria-dim-plug/driver.settings.compose.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
17 changes: 16 additions & 1 deletion drivers/adurosmart-eria-on-off-plug/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}
Expand Down
22 changes: 1 addition & 21 deletions drivers/adurosmart-eria-on-off-plug/driver.compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
}
20 changes: 20 additions & 0 deletions drivers/adurosmart-eria-on-off-plug/driver.settings.compose.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
104 changes: 65 additions & 39 deletions drivers/adurosmart-eria-power-analytics-plug/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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',
Expand Down Expand Up @@ -107,6 +74,65 @@ class PowerAnalyticsPlug extends ZigBeeDevice {
},
});
}

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) {
// 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;
Loading