Skip to content

Commit

Permalink
Add meters and meters for gen 1 devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Jun 19, 2024
1 parent 2bf21de commit da3a32a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/coapServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnsiLogger, BLUE, CYAN, MAGENTA, RESET, TimestampFormat, db, debugStringify, idn, rs, wr } from 'node-ansi-logger';
import { AnsiLogger, BLUE, CYAN, MAGENTA, RESET, TimestampFormat, db, debugStringify, idn, rs } from 'node-ansi-logger';
import coap, { Server, IncomingMessage, OutgoingMessage, globalAgent } from 'coap';
import EventEmitter from 'events';

Expand Down
40 changes: 26 additions & 14 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import {
LevelControlCluster,
BooleanStateCluster,
ClusterRegistry,
electricalSensor,
ElectricalPowerMeasurement,
ElectricalEnergyMeasurement,
} from 'matterbridge';
import { AnsiLogger, BLUE, CYAN, GREEN, TimestampFormat, YELLOW, db, debugStringify, dn, er, hk, idn, nf, or, rs, wr, zb } from 'node-ansi-logger';
import { NodeStorage, NodeStorageManager } from 'node-persist-manager';
Expand Down Expand Up @@ -236,7 +233,7 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
if (pmComponent) {
mbDevice.addFixedLabel('composed', component.name);
// Add the Matter 1.3 device type with the ElectricalPowerMeasurement and ElectricalEnergyMeasurement clusters
mbDevice.addChildDeviceTypeWithClusterServer('electricalSensor', [electricalSensor], [ElectricalPowerMeasurement.Cluster.id, ElectricalEnergyMeasurement.Cluster.id]);
// mbDevice.addChildDeviceTypeWithClusterServer('electricalSensor', [electricalSensor], [ElectricalPowerMeasurement.Cluster.id, ElectricalEnergyMeasurement.Cluster.id]);
// Add the custom EveHistory cluster for HA
ClusterRegistry.register(EveHistory.Complete);
const child = mbDevice.addChildDeviceTypeWithClusterServer(key, [powerSource], [EveHistory.Cluster.id]);
Expand All @@ -245,11 +242,14 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
if (voltage !== undefined) child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setVoltageAttribute(voltage as number);
const current = pmComponent.getValue('current');
if (current !== undefined) child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setCurrentAttribute(current as number);
const power = pmComponent.getValue('apower');
const power = pmComponent.getValue('apower') ?? pmComponent.getValue('power');
if (power !== undefined) child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setConsumptionAttribute(power as number);
const energy = pmComponent.getValue('aenergy');
if (energy !== undefined && energy !== null)
child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setTotalConsumptionAttribute((energy as ShellyData).total as number);
const energy1 = pmComponent.getValue('total'); // Gen 1 devices
if (energy1 !== undefined && energy1 !== null)
child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setTotalConsumptionAttribute(energy1 as number);
const energy2 = pmComponent.getValue('aenergy'); // Gen 2 devices
if (energy2 !== undefined && energy2 !== null)
child.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy))?.setTotalConsumptionAttribute((energy2 as ShellyData).total as number);
// Add event handler
pmComponent.on('update', (component: string, property: string, value: ShellyDataType) => {
this.shellyUpdateHandler(mbDevice, device, component, property, value);
Expand Down Expand Up @@ -590,12 +590,24 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
);
}
// Update energy
if (shellyComponent.name === 'PowerMeter' && property === 'aenergy') {
const cluster = endpoint.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy));
cluster?.setTotalConsumptionAttribute((value as ShellyData).total as number);
shellyDevice.log.info(
`${db}Update endpoint ${or}${endpoint.number}${db} attribute ${hk}EveHistory-totalConsumption${db} ${YELLOW}${(value as ShellyData).total as number}${db}`,
);
if (shellyComponent.name === 'PowerMeter') {
if (property === 'power' || property === 'apower') {
const cluster = endpoint.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy));
cluster?.setConsumptionAttribute(value as number);
shellyDevice.log.info(`${db}Update endpoint ${or}${endpoint.number}${db} attribute ${hk}EveHistory-consumption${db} ${YELLOW}${value as number}${db}`);
}
if (property === 'total') {
const cluster = endpoint.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy));
cluster?.setTotalConsumptionAttribute(value as number);
shellyDevice.log.info(`${db}Update endpoint ${or}${endpoint.number}${db} attribute ${hk}EveHistory-totalConsumption${db} ${YELLOW}${value as number}${db}`);
}
if (property === 'aenergy') {
const cluster = endpoint.getClusterServer(EveHistoryCluster.with(EveHistory.Feature.EveEnergy));
cluster?.setTotalConsumptionAttribute((value as ShellyData).total as number);
shellyDevice.log.info(
`${db}Update endpoint ${or}${endpoint.number}${db} attribute ${hk}EveHistory-totalConsumption${db} ${YELLOW}${(value as ShellyData).total as number}${db}`,
);
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/shellyDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ export class ShellyDevice extends EventEmitter {
device.addComponent(new ShellyComponent(device, `roller:${index++}`, 'Roller', roller as ShellyData));
}
}
if (key === 'meters') {
let index = 0;
for (const meter of statusPayload[key] as ShellyData[]) {
device.addComponent(new ShellyComponent(device, `meter:${index++}`, 'PowerMeter', meter as ShellyData));
}
}
if (key === 'emeters') {
let index = 0;
for (const emeter of statusPayload[key] as ShellyData[]) {
device.addComponent(new ShellyComponent(device, `emeter:${index++}`, 'PowerMeter', emeter as ShellyData));
}
}
}
}

Expand Down Expand Up @@ -280,6 +292,18 @@ export class ShellyDevice extends EventEmitter {
this.updateComponent(`roller:${index++}`, roller as ShellyData);
}
}
if (key === 'meters') {
let index = 0;
for (const meter of data[key] as ShellyData[]) {
this.updateComponent(`meter:${index++}`, meter as ShellyData);
}
}
if (key === 'emeters') {
let index = 0;
for (const emeter of data[key] as ShellyData[]) {
this.updateComponent(`emeter:${index++}`, emeter as ShellyData);
}
}
}
// Update state for active components with ison
for (const key in data) {
Expand Down

0 comments on commit da3a32a

Please sign in to comment.