From 34abd907d40afe17e18262ebccfaae6fb8713428 Mon Sep 17 00:00:00 2001 From: Robin Bolscher Date: Wed, 13 Nov 2024 16:43:48 +0100 Subject: [PATCH] feat: add `meter_power.imported` and `meter_power.exported` accumulated capabilities (#100) --- app.json | 16 ++++++++++++---- drivers/pulse/device.ts | 26 ++++++++++++++++++++++++++ drivers/pulse/driver.compose.json | 8 ++++++-- drivers/watty/device.ts | 26 ++++++++++++++++++++++++++ drivers/watty/driver.compose.json | 8 ++++++-- lib/api.ts | 2 ++ lib/queries.ts | 2 ++ 7 files changed, 80 insertions(+), 8 deletions(-) diff --git a/app.json b/app.json index acd59bc..62798c4 100644 --- a/app.json +++ b/app.json @@ -1208,10 +1208,14 @@ "accumulatedCost", "measure_current.L1", "measure_current.L2", - "measure_current.L3" + "measure_current.L3", + "meter_power.imported", + "meter_power.exported" ], "energy": { - "cumulative": true + "cumulative": true, + "cumulativeImportedCapability": "meter_power.imported", + "cumulativeExportedCapability": "meter_power.exported" }, "capabilitiesOptions": { "accumulatedCost": { @@ -1451,10 +1455,14 @@ "accumulatedCost", "measure_current.L1", "measure_current.L2", - "measure_current.L3" + "measure_current.L3", + "meter_power.imported", + "meter_power.exported" ], "energy": { - "cumulative": true + "cumulative": true, + "cumulativeImportedCapability": "meter_power.imported", + "cumulativeExportedCapability": "meter_power.exported" }, "capabilitiesOptions": { "accumulatedCost": { diff --git a/drivers/pulse/device.ts b/drivers/pulse/device.ts index 85f6df4..2ef38f7 100644 --- a/drivers/pulse/device.ts +++ b/drivers/pulse/device.ts @@ -348,6 +348,32 @@ class PulseDevice extends Device { .catch(console.error); }); } + + const lastMeterConsumption = + result.data?.liveMeasurement?.lastMeterConsumption; + if (typeof lastMeterConsumption === 'number') { + if (this.hasCapability('meter_power.imported') !== true) + await this.addCapability('meter_power.imported').catch(console.error); + + const fixedLastMeterConsumption = Number(lastMeterConsumption.toFixed(2)); + this.setCapabilityValue( + 'meter_power.imported', + fixedLastMeterConsumption, + ).catch(console.error); + } + + const lastMeterProduction = + result.data?.liveMeasurement?.lastMeterProduction; + if (typeof lastMeterProduction === 'number') { + if (this.hasCapability('meter_power.exported') !== true) + await this.addCapability('meter_power.exported').catch(console.error); + + const fixedLastMeterProduction = Number(lastMeterProduction.toFixed(2)); + this.setCapabilityValue( + 'meter_power.exported', + fixedLastMeterProduction, + ).catch(console.error); + } } onDeleted() { diff --git a/drivers/pulse/driver.compose.json b/drivers/pulse/driver.compose.json index 08d6b68..e2a2e88 100644 --- a/drivers/pulse/driver.compose.json +++ b/drivers/pulse/driver.compose.json @@ -10,10 +10,14 @@ "accumulatedCost", "measure_current.L1", "measure_current.L2", - "measure_current.L3" + "measure_current.L3", + "meter_power.imported", + "meter_power.exported" ], "energy": { - "cumulative": true + "cumulative": true, + "cumulativeImportedCapability": "meter_power.imported", + "cumulativeExportedCapability": "meter_power.exported" }, "capabilitiesOptions": { "accumulatedCost": { diff --git a/drivers/watty/device.ts b/drivers/watty/device.ts index a1ea5de..705acbd 100644 --- a/drivers/watty/device.ts +++ b/drivers/watty/device.ts @@ -350,6 +350,32 @@ class WattyDevice extends Device { }); } } + + const lastMeterConsumption = + result.data?.liveMeasurement?.lastMeterConsumption; + if (typeof lastMeterConsumption === 'number') { + if (this.hasCapability('meter_power.imported') !== true) + await this.addCapability('meter_power.imported').catch(console.error); + + const fixedLastMeterConsumption = Number(lastMeterConsumption.toFixed(2)); + this.setCapabilityValue( + 'meter_power.imported', + fixedLastMeterConsumption, + ).catch(console.error); + } + + const lastMeterProduction = + result.data?.liveMeasurement?.lastMeterProduction; + if (typeof lastMeterProduction === 'number') { + if (this.hasCapability('meter_power.exported') !== true) + await this.addCapability('meter_power.exported').catch(console.error); + + const fixedLastMeterProduction = Number(lastMeterProduction.toFixed(2)); + this.setCapabilityValue( + 'meter_power.exported', + fixedLastMeterProduction, + ).catch(console.error); + } } onDeleted() { diff --git a/drivers/watty/driver.compose.json b/drivers/watty/driver.compose.json index a1d74ea..ac7f94a 100644 --- a/drivers/watty/driver.compose.json +++ b/drivers/watty/driver.compose.json @@ -10,10 +10,14 @@ "accumulatedCost", "measure_current.L1", "measure_current.L2", - "measure_current.L3" + "measure_current.L3", + "meter_power.imported", + "meter_power.exported" ], "energy": { - "cumulative": true + "cumulative": true, + "cumulativeImportedCapability": "meter_power.imported", + "cumulativeExportedCapability": "meter_power.exported" }, "capabilitiesOptions": { "accumulatedCost": { diff --git a/lib/api.ts b/lib/api.ts index aca649c..6a6997a 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -31,6 +31,8 @@ export interface LiveMeasurement { power: number; accumulatedConsumption: number; accumulatedCost: number | null; + lastMeterConsumption: number | null; + lastMeterProduction: number | null; currency: string | null; minPower: number; averagePower: number; diff --git a/lib/queries.ts b/lib/queries.ts index 20eedf1..d97f81b 100644 --- a/lib/queries.ts +++ b/lib/queries.ts @@ -106,6 +106,8 @@ export const queries = { power accumulatedConsumption accumulatedCost + lastMeterConsumption + lastMeterProduction currency minPower averagePower