Skip to content

Commit

Permalink
Battery readings for Smart Garden Irrigation controller
Browse files Browse the repository at this point in the history
New repair required (maintance)
  • Loading branch information
shaarkys committed Feb 24, 2024
1 parent 78ab6bf commit c340864
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions drivers/smart_garden_irrigation_control/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Homey = require('homey');
const { ZigBeeDevice } = require('homey-zigbeedriver');
const { Cluster, CLUSTER } = require('zigbee-clusters');
const { debug, Cluster, CLUSTER } = require('zigbee-clusters');
const TuyaSpecificCluster = require('../../lib/TuyaSpecificCluster');

Cluster.addCluster(TuyaSpecificCluster);
Expand All @@ -12,6 +12,7 @@ const DEFAULT_ONOFF_DURATION = 1000;
// Data point and data types definitions
const dataPoints = {
batteryLevel: 101, // Adjust the data point as per your device's specification
OnOffreport : 102
};

const dataTypes = {
Expand Down Expand Up @@ -59,59 +60,69 @@ class IrrigationController extends ZigBeeDevice {

async onNodeInit({zclNode}) {

this.printNode();
// Tuya specific cluster info

zclNode.endpoints[1].clusters.tuya.on("response", (value) => this.handleTuyaResponse(value));

zclNode.endpoints[1].clusters.tuya.on("reportingConfiguration", (value) => this.handleTuyaResponse(value));

this.registerCapability('onoff', CLUSTER.ON_OFF);

/*
this.registerCapabilityListener("onoff", async (value, options) => {
this.log("Irrigation controller value " + value);
this.log("Irrigation controller options " + options.duration);
if (value && options.duration !== undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOn();
await new Promise(resolve => setTimeout(resolve, options.duration));
await zclNode.endpoints[1].clusters['onOff'].setOff();
} else if (value && options.duration === undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOn();
} else if (!value && options.duration === undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOff();
try {
this.log("Irrigation controller value " + value);
this.log("Irrigation controller options " + options.duration);
if (value && options.duration !== undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOn();
await new Promise(resolve => setTimeout(resolve, options.duration));
await zclNode.endpoints[1].clusters['onOff'].setOff();
} else if (value && options.duration === undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOn();
} else if (!value && options.duration === undefined) {
await zclNode.endpoints[1].clusters['onOff'].setOff();
}
} catch (err) {
this.error('Error handling onoff capability:', err);
}
});
*/

// Tuya specific cluster info

zclNode.endpoints[1].clusters.tuya.on("response", (value) => this.handleTuyaResponse(value));

zclNode.endpoints[1].clusters.tuya.on("reporting", (value) => this.handleTuyaResponse(value));

//zclNode.endpoints[1].clusters.tuya.on("reportingConfiguration", (value) => this.handleTuyaResponse(value));
//zclNode.endpoints[1].clusters.tuya.on("reportingConfiguration", (value) => this.log('reportingConfiguration event received:', value));

/* zclNode.endpoints[1].clusters.basic.readAttributes(['manufacturerName', 'zclVersion', 'appVersion', 'modelId', 'powerSource', 'attributeReportingStatus'])
.catch(err => {
this.error('Error when reading device attributes ', err);
});
*/

// add battery capabilities if needed
if (!this.hasCapability('measure_battery')) {
this.addCapability('measure_battery').catch(this.error);
}
if (!this.hasCapability('alarm_battery')) {
this.addCapability('alarm_battery').catch(this.error);
}

}
}

async handleTuyaResponse(response) {
try {
const dp = response.dp;
const value = this.getDataValue(response);
this.log("Irrigation !!!! controller handleTuyaResponse dp: " + dp + " value: " + value);
const value = getDataValue(response);
this.log("Irrigation controller handleTuyaResponse dp: " + dp + " value: " + value);

switch (dp) {

case dataPoints.batteryLevel:
this.log("Battery: " + value);
this.log("Irrigation controller Battery: " + value);
const batteryThreshold = this.getSetting('batteryThreshold') || 20;
parsedValue = value;
this.log("measure_battery | powerConfiguration - batteryPercentageRemaining (%): ", parsedValue);
this.log("measure_battery | powerConfiguration - batteryPercentageRemaining (%): ", value);
this.setCapabilityValue('measure_battery', value).catch(this.error);
this.setCapabilityValue('alarm_battery', (value < batteryThreshold)).catch(this.error);
break;

this.setCapabilityValue('measure_battery', parsedValue).catch(this.error);
this.setCapabilityValue('alarm_battery', (parsedValue < batteryThreshold)).catch(this.error);
case dataPoints.OnOffreport:
this.log("Irrigation controller OnOffreport: " + value);
const isOn = value === 0; // 2 means Off, 0 means On
await this.setCapabilityValue('onoff', isOn);
break;

default:
this.log('dp value', dp, value)
this.log('Irrigation dp value - not processed', dp, value)
}
} catch (error) {
this.error('Error in handleTuyaResponse:', error);
Expand Down

0 comments on commit c340864

Please sign in to comment.