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

Added Amina S device #8191

Merged
merged 10 commits into from
Oct 31, 2024
355 changes: 355 additions & 0 deletions src/devices/amina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,355 @@
import {Zcl} from 'zigbee-herdsman';

import fz from '../converters/fromZigbee';
import * as exposes from '../lib/exposes';
import {binary, deviceAddCustomCluster, electricityMeter, numeric, onOff} from '../lib/modernExtend';
import * as ota from '../lib/ota';
import * as reporting from '../lib/reporting';
import {DefinitionWithExtend, Fz, KeyValue, Tz} from '../lib/types';
import * as utils from '../lib/utils';

const e = exposes.presets;
const ea = exposes.access;

const manufacturerOptions = {manufacturerCode: 0x143b};

const aminaControlAttributes = {
cluster: 0xfee7,
alarms: 0x02,
ev_status: 0x03,
connect_status: 0x04,
single_phase: 0x05,
offline_current: 0x06,
offline_single_phase: 0x07,
time_to_offline: 0x08,
enable_offline: 0x09,
total_active_energy: 0x10,
last_session_energy: 0x11,
};

const aminaAlarms = [
'Welded relay(s)',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make all of these snake_case:

Suggested change
'Welded relay(s)',
'welded_relays',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what is the purpose of converting those to snake case but not status texts for ev_status for instance?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually text is free format, but for this use case it may be better to use a e.enum()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably enum would work, but as the value from the device is «bit encoded» one could in theory have multiple alarms as once? Probably not very likely tho, but possible.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, e.list() with an e.enum() as the type would be the best then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any examples of how to do this, because whatever I do, the GUI only shows an array of strings, or is this expected behaviour?
I defined:

const aminaAlarms = [
    'no_alarm',
    'welded_relay', // Bit 0
    'wrong_voltage_balance',
    'rdc_dd_dc_leakage',
    'rdc_dd_ac_leakage',
    'high_temperature',
    'overvoltage',
    'undervoltage',
    'overcurrent',
    'car_communication_error',
    'charger_processing_error',
    'critical_overcurrent',
    'critical_powerloss',
    'unknown_alarm_bit_12',
    'unknown_alarm_bit_13',
    'unknown_alarm_bit_14',
    'unknown_alarm_bit_15',
];

const aminaAlarmsEnum = e.enum('alarms', ea.STATE_GET, aminaAlarms);

In the fzLocal.alarms converter:

if (msg.data.alarms !== undefined) {
    const activeAlarms = [];
    result.alarm_active = false;

    for (let i = 0; i < 16; i++) {
        if ((msg.data['alarms'] >> i) & 0x01) {
            activeAlarms.push(aminaAlarmsEnum.values[i+1]);
            result.alarm_active = true;
        }
    }

    if (result.alarm_active === false) {
        activeAlarms.push(aminaAlarmsEnum.values[0]);
    }

    result.alarms = activeAlarms;
    return result;
}

And finally the expose:

e.list('alarms', ea.STATE_GET, aminaAlarmsEnum).withDescription('List of active alarms'),

Example result with bit 1, 3 and 12 enabled:
image

Example result with no alarms:
image

I would perhaps expect something like this, not editable of course:
image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expected indeed, the frontend should be improved a bit here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then, this is implemented in 8263316

'Wrong voltage balance',
'RDC-DD DC Leakage',
'RDC-DD AC Leakage',
'High Temperature',
'Overvoltage',
'Overcurrent',
'Car communication error',
'Charger processing error',
'Critical overcurrent',
'Critical powerloss',
];

const fzLocal = {
charge_limit: {
cluster: 'genLevelCtrl',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const result: KeyValue = {};

if (msg.data.currentLevel !== undefined) {
result.charge_limit = msg.data['currentLevel'];
}

return result;
},
} satisfies Fz.Converter,

ev_status: {
cluster: 'aminaControlCluster',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const result: KeyValue = {};

if (msg.data.evStatus !== undefined) {
let statusText = '';
const evStatus = msg.data['evStatus'];

result.ev_connected = (evStatus & (1 << 0)) !== 0;
if (result.ev_connected) {
statusText = 'EV Connected';
} else {
statusText = 'Not Connected';
}
if ((evStatus & (1 << 1)) !== 0) statusText = 'Ready to charge';
if ((evStatus & (1 << 2)) !== 0) statusText = 'Charging';
if ((evStatus & (1 << 3)) !== 0) statusText = 'Charging Paused';

result.derated = (evStatus & (1 << 15)) !== 0;
if (result.derated) statusText += ', Derated';

result.ev_status = statusText;

return result;
}
},
} satisfies Fz.Converter,

alarms: {
cluster: 'aminaControlCluster',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const result: KeyValue = {};

if (msg.data.alarms !== undefined) {
result.alarms = [];
result.alarm_active = false;

for (let i = 0; i < 16; i++) {
if ((msg.data['alarms'] >> i) & 0x01) {
let alarm = aminaAlarms[i];
if (alarm === undefined) {
alarm = `Unknown Alarm bit #${i}`;
}

(result.alarms as string[]).push(alarm);
result.alarm_active = true;
}
}

if (result.alarm_active === false) {
result.alarms = 'No Alarm';
}

return result;
}
},
} satisfies Fz.Converter,
};

const tzLocal = {
charge_limit: {
key: ['charge_limit'],
convertSet: async (entity, key, value, meta) => {
const payload = {level: value, transtime: 0};
await entity.command('genLevelCtrl', 'moveToLevel', payload, utils.getOptions(meta.mapped, entity));
},

convertGet: async (entity, key, meta) => {
await entity.read('genLevelCtrl', ['currentLevel'], manufacturerOptions);
},
} satisfies Tz.Converter,

ev_status: {
key: ['ev_status'],
convertGet: async (entity, key, meta) => {
await entity.read('aminaControlCluster', ['evStatus'], manufacturerOptions);
},
} satisfies Tz.Converter,

alarms: {
key: ['alarms'],
convertGet: async (entity, key, meta) => {
await entity.read('aminaControlCluster', ['alarms'], manufacturerOptions);
},
} satisfies Tz.Converter,

total_active_power: {
key: ['total_active_power'],
convertGet: async (entity, key, meta) => {
await entity.read('haElectricalMeasurement', ['totalActivePower'], manufacturerOptions);
},
} satisfies Tz.Converter,
};

const definitions: DefinitionWithExtend[] = [
{
zigbeeModel: ['amina S'],
model: 'amina S',
vendor: 'Amina Distribution AS',
description: 'Amina S EV Charger',
ota: ota.zigbeeOTA,
fromZigbee: [fzLocal.charge_limit, fzLocal.ev_status, fzLocal.alarms, fz.electrical_measurement],
toZigbee: [tzLocal.charge_limit, tzLocal.ev_status, tzLocal.alarms, tzLocal.total_active_power],
exposes: [
e
.numeric('charge_limit', ea.ALL)
.withUnit('A')
.withValueMin(6)
.withValueMax(32)
.withValueStep(1)
.withDescription('Maximum allowed amperage draw'),
e.text('ev_status', ea.STATE_GET).withDescription('Current charging status'),
e.binary('ev_connected', ea.STATE, 'true', 'false').withDescription('An EV is connected to the charger'),
e.binary('derated', ea.STATE, 'true', 'false').withDescription('Charging derated due to high temperature'),
e.text('alarms', ea.STATE_GET).withDescription('Alarms reported by EV Charger'),
e.binary('alarm_active', ea.STATE, 'true', 'false').withDescription('An active alarm is present'),
],

extend: [
electricityMeter({
cluster: 'electrical',
acFrequency: true,
threePhase: true,
}),

deviceAddCustomCluster('aminaControlCluster', {
ID: aminaControlAttributes.cluster,
manufacturerCode: manufacturerOptions.manufacturerCode,
attributes: {
alarms: {ID: aminaControlAttributes.alarms, type: Zcl.DataType.BITMAP16},
evStatus: {ID: aminaControlAttributes.ev_status, type: Zcl.DataType.BITMAP16},
connectStatus: {ID: aminaControlAttributes.connect_status, type: Zcl.DataType.BITMAP16},
singlePhase: {ID: aminaControlAttributes.single_phase, type: Zcl.DataType.UINT8},
offlineCurrent: {ID: aminaControlAttributes.offline_current, type: Zcl.DataType.UINT8},
offlineSinglePhase: {ID: aminaControlAttributes.offline_single_phase, type: Zcl.DataType.UINT8},
timeToOffline: {ID: aminaControlAttributes.time_to_offline, type: Zcl.DataType.UINT16},
enableOffline: {ID: aminaControlAttributes.enable_offline, type: Zcl.DataType.UINT8},
totalActiveEnergy: {ID: aminaControlAttributes.total_active_energy, type: Zcl.DataType.UINT32},
lastSessionEnergy: {ID: aminaControlAttributes.last_session_energy, type: Zcl.DataType.UINT32},
},
commands: {},
commandsResponse: {},
}),

onOff({
powerOnBehavior: false,
}),

numeric({
name: 'total_active_power',
cluster: 'haElectricalMeasurement',
attribute: 'totalActivePower',
description: 'Instantaneous measured total active power',
reporting: {min: '10_SECONDS', max: 'MAX', change: 5},
unit: 'kW',
scale: 1000,
precision: 2,
access: 'STATE_GET',
}),

numeric({
name: 'total_active_energy',
cluster: 'aminaControlCluster',
attribute: 'totalActiveEnergy',
description: 'Sum of consumed energy',
//reporting: {min: '10_SECONDS', max: 'MAX', change: 5}, // Not Reportable atm
unit: 'kWh',
scale: 1000,
precision: 2,
access: 'STATE_GET',
}),

numeric({
name: 'last_session_energy',
cluster: 'aminaControlCluster',
attribute: 'lastSessionEnergy',
description: 'Sum of consumed energy last session',
//reporting: {min: '10_SECONDS', max: 'MAX', change: 5}, // Not Reportable atm
unit: 'kWh',
scale: 1000,
precision: 2,
access: 'STATE_GET',
}),

binary({
name: 'single_phase',
cluster: 'aminaControlCluster',
attribute: 'singlePhase',
description: 'Enable single phase charging. A restart of charging is required for the change to take effect.',
valueOn: ['Enable', 1],
valueOff: ['Disable', 0],
entityCategory: 'config',
}),

binary({
name: 'enable_offline',
cluster: 'aminaControlCluster',
attribute: 'enableOffline',
description: 'Enable offline mode when connection to the network is lost',
valueOn: ['Enable', 1],
valueOff: ['Disable', 0],
entityCategory: 'config',
}),

numeric({
name: 'time_to_offline',
cluster: 'aminaControlCluster',
attribute: 'timeToOffline',
description: 'Time until charger will behave as offline after connection has been lost',
valueMin: 0,
valueMax: 60,
valueStep: 1,
unit: 's',
entityCategory: 'config',
}),

numeric({
name: 'offline_current',
cluster: 'aminaControlCluster',
attribute: 'offlineCurrent',
description: 'Maximum allowed amperage draw when device is offline',
valueMin: 6,
valueMax: 32,
valueStep: 1,
unit: 'A',
entityCategory: 'config',
}),

binary({
name: 'offline_single_phase',
cluster: 'aminaControlCluster',
attribute: 'offlineSinglePhase',
description: 'Use single phase charging when device is offline',
valueOn: ['Enable', 1],
valueOff: ['Disable', 0],
entityCategory: 'config',
}),
],

endpoint: (device) => {
return {default: 10};
},

configure: async (device, coordinatorEndpoint) => {
const endpoint = device.getEndpoint(10);

const binds = ['genBasic', 'genOnOff', 'haElectricalMeasurement', 'genLevelCtrl', 'aminaControlCluster'];
await reporting.bind(endpoint, coordinatorEndpoint, binds);

await endpoint.configureReporting('genLevelCtrl', [
{
attribute: 'currentLevel',
minimumReportInterval: 10,
maximumReportInterval: 65000,
reportableChange: 1,
},
]);

await endpoint.configureReporting('aminaControlCluster', [
{
attribute: 'evStatus',
minimumReportInterval: 10,
maximumReportInterval: 65000,
reportableChange: 1,
},
]);

await endpoint.configureReporting('aminaControlCluster', [
{
attribute: 'alarms',
minimumReportInterval: 10,
maximumReportInterval: 65000,
reportableChange: 1,
},
]);

await endpoint.read('aminaControlCluster', [
'alarms',
'evStatus',
'connectStatus',
'singlePhase',
'offlineCurrent',
'offlineSinglePhase',
'timeToOffline',
'enableOffline',
'totalActiveEnergy',
'lastSessionEnergy',
]);
},
},
];

export default definitions;
module.exports = definitions;
2 changes: 2 additions & 0 deletions src/devices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import akuvox from './akuvox';
import alchemy from './alchemy';
import aldi from './aldi';
import alecto from './alecto';
import amina from './amina';
import anchor from './anchor';
import atlantic from './atlantic';
import atsmart from './atsmart';
Expand Down Expand Up @@ -320,6 +321,7 @@ export default [
...alchemy,
...aldi,
...alecto,
...amina,
...anchor,
...atlantic,
...atsmart,
Expand Down
Loading