Skip to content

Commit

Permalink
Tried to add proper l1 to l3 measure_power values as the ones from th…
Browse files Browse the repository at this point in the history
…e API are incorrect
  • Loading branch information
robertraaijmakers committed Feb 15, 2025
1 parent 50656d3 commit f733240
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 63 deletions.
10 changes: 8 additions & 2 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"1.0.0": {
"en": "First version to support Alfen charger"
"en": "First version to support Alfen charger."
},
"1.0.1": {
"en": "Added more capabilities and added support to update settings with flow cards"
"en": "Added more capabilities and added support to update settings with flow cards."
},
"1.0.2": {
"en": "Updated error handling. Moved some capabilities to sensors to prevent accidental changes."
},
"1.0.3": {
"en": "New version, better error handling, supporting more capabilities and updating settings with flow cards."
}
}
4 changes: 2 additions & 2 deletions .homeycompose/app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "com.alfen",
"version": "1.0.1",
"version": "1.0.2",
"compatibility": ">=12.0.0",
"sdk": 3,
"platforms": [
Expand Down Expand Up @@ -48,6 +48,6 @@
},
"homepage": "https://robertraaijmakers.github.io/com.alfen/",
"support": "https://robertraaijmakers.github.io/com.alfen/",
"homeyCommunityTopicId": 76738,
"homeyCommunityTopicId": 130683,
"source": "https://github.com/robertraaijmakers/com.alfen/"
}
4 changes: 2 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_comment": "This file is generated. Please edit .homeycompose/app.json instead.",
"id": "com.alfen",
"version": "1.0.1",
"version": "1.0.2",
"compatibility": ">=12.0.0",
"sdk": 3,
"platforms": [
Expand Down Expand Up @@ -49,7 +49,7 @@
},
"homepage": "https://robertraaijmakers.github.io/com.alfen/",
"support": "https://robertraaijmakers.github.io/com.alfen/",
"homeyCommunityTopicId": 76738,
"homeyCommunityTopicId": 130683,
"source": "https://github.com/robertraaijmakers/com.alfen/",
"flow": {
"actions": [
Expand Down
126 changes: 69 additions & 57 deletions lib/AlfenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const energyMeterCapabilitiesMap: { [key: string]: string } = {
'2221_A': 'measure_current.l1', // V
'2221_B': 'measure_current.l2', // V
'2221_C': 'measure_current.l3', // V
'2221_13': 'measure_power.l1', // W
'2221_14': 'measure_power.l2', // W
'2221_15': 'measure_power.l3', // W
//'2221_13': 'measure_power.l1', // W
//'2221_14': 'measure_power.l2', // W
//'2221_15': 'measure_power.l3', // W
'2221_16': 'measure_power', // W (Power / Vermogen)
'2221_22': 'meter_power', // kWh (Energy / Energie)
'2501_2': 'operatingmode',
Expand Down Expand Up @@ -171,68 +171,80 @@ export class AlfenApi {
rejectUnauthorized: false, // Disable SSL certificate validation if needed
};

let bodyResult: PropertyResponseBody;
const capabilitiesData: Array<{
capabilityId: string;
value: number | string | boolean;
}> = [];

try {
// Make the HTTPS request using the httpsPromise method
const response = await this.#httpsPromise(options);
bodyResult = <PropertyResponseBody>response.body;
} catch (error) {
this.#log('Request failed:', error);
throw new Error(`Request failed: ${error}`);
}

// Handle the response
const bodyResult = <PropertyResponseBody>response.body;
const result = bodyResult.properties;
const capabilitiesData: Array<{
capabilityId: string;
value: number | string | boolean;
}> = [];

for (const prop of result) {
const capabilityId = energyMeterCapabilitiesMap[prop.id];

this.#log(`Property: ${prop.id}: ${prop.value}, Type: ${prop.type}, Category: ${prop.cat}, Access: ${prop.access}, Capability: ${capabilityId ?? 'Unknown'}`);

if (capabilityId) {
let value: string | number | boolean | null = null;
if (bodyResult === undefined) return capabilitiesData;

// Handle the response
const result = bodyResult.properties;

for (const prop of result) {
const capabilityId = energyMeterCapabilitiesMap[prop.id];

this.#log(`Property: ${prop.id}: ${prop.value}, Type: ${prop.type}, Category: ${prop.cat}, Access: ${prop.access}, Capability: ${capabilityId ?? 'Unknown'}`);

if (capabilityId) {
let value: string | number | boolean | null = null;

// Handle specific rounding or transformation for certain properties
switch (prop.id) {
case '2501_2':
value = this.#statusToString(prop.value);
break;
case '2221_3': // Voltage L1
case '2221_4': // Voltage L2
case '2221_5': // Voltage L3
value = Math.round(prop.value); // rounding values, no decimal
break;
case '2201_0': // Temperature
case '2221_A': // Current L1
case '2221_B': // Current L2
case '2221_C': // Current L3
case '2221_16': // Power (watts)
value = Math.round(prop.value * 10) / 10; // rounding values, one decimal
break;
case '2221_22': // Total energy
value = Math.round(prop.value / 10) / 100; // rounding values, 2 decimal (but needs to be devided by 1000)
break;
case '2126_0': // Auth mode
case '3280_1': // Charge type
value = prop.value.toString();
break;
default:
value = prop.value;
break;
}

// Handle specific rounding or transformation for certain properties
switch (prop.id) {
case '2501_2':
value = this.#statusToString(prop.value);
break;
case '2221_3': // Voltage L1
case '2221_4': // Voltage L2
case '2221_5': // Voltage L3
value = Math.round(prop.value); // rounding values, no decimal
break;
case '2201_0': // Temperature
case '2221_A': // Current L1
case '2221_B': // Current L2
case '2221_C': // Current L3
case '2221_13': // Power L1 (watts)
case '2221_14': // Power L2 (watts)
case '2221_15': // Power L3 (watts)
case '2221_16': // Power (watts)
value = Math.round(prop.value * 10) / 10; // rounding values, one decimal
break;
case '2221_22': // Total energy
value = Math.round(prop.value / 10) / 100; // rounding values, 2 decimal (but needs to be devided by 1000)
break;
case '2126_0': // Auth mode
case '3280_1': // Charge type
value = prop.value.toString();
break;
default:
value = prop.value;
break;
}
// Collect the mapped data
capabilitiesData.push({ capabilityId, value });
}
}

// Collect the mapped data
capabilitiesData.push({ capabilityId, value });
}
// Calculate power (Volt * Ampere = Watt) for L1, L2, and L3
['l1', 'l2', 'l3'].forEach((phase) => {
const voltage = capabilitiesData.find((x) => x.capabilityId === `measure_voltage.${phase}`)?.value;
const current = capabilitiesData.find((x) => x.capabilityId === `measure_current.${phase}`)?.value;
if (voltage && current && !isNaN(Number(voltage)) && !isNaN(Number(current))) {
capabilitiesData.push({ capabilityId: `measure_power.${phase}`, value: Math.round(Number(voltage) * Number(current)) });
} else {
capabilitiesData.push({ capabilityId: `measure_power.${phase}`, value: 0 });
}
});

return capabilitiesData;
} catch (error) {
this.#log('Request failed:', error);
throw new Error(`Request failed: ${error}`);
}
return capabilitiesData;
}

async apiSetCurrentLimit(currentLimit: number) {
Expand Down

0 comments on commit f733240

Please sign in to comment.