From 15a1626d25189d7d18a8bf2d2435c07c421585b2 Mon Sep 17 00:00:00 2001 From: shaarkys Date: Sat, 24 Feb 2024 23:35:19 +0100 Subject: [PATCH] Battery readings for Smart Garden Irrigation controller New repair required (maintance) --- .../smart_garden_irrigation_control/device.js | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/drivers/smart_garden_irrigation_control/device.js b/drivers/smart_garden_irrigation_control/device.js index 045035b..f9fb733 100644 --- a/drivers/smart_garden_irrigation_control/device.js +++ b/drivers/smart_garden_irrigation_control/device.js @@ -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); @@ -59,59 +59,58 @@ 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); const batteryThreshold = this.getSetting('batteryThreshold') || 20; - parsedValue = value; - this.log("measure_battery | powerConfiguration - batteryPercentageRemaining (%): ", parsedValue); - - this.setCapabilityValue('measure_battery', parsedValue).catch(this.error); - this.setCapabilityValue('alarm_battery', (parsedValue < batteryThreshold)).catch(this.error); + 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; default: - this.log('dp value', dp, value) + this.log('Irrigation dp value - not processed', dp, value) } } catch (error) { this.error('Error in handleTuyaResponse:', error);