Skip to content

Commit

Permalink
Add Total Consumption (kWh) support for outlets/switches. (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e authored Feb 24, 2023
1 parent da08568 commit e7816f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/accessory/SwitchAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const SCHEMA_CODE = {
CURRENT: ['cur_current'],
POWER: ['cur_power'],
VOLTAGE: ['cur_voltage'],
TOTAL_POWER: ['add_ele'],
CURRENT_TEMP: ['va_temperature', 'temp_current'],
CURRENT_HUMIDITY: ['va_humidity', 'humidity_value'],
};
Expand Down Expand Up @@ -65,6 +66,7 @@ export default class SwitchAccessory extends BaseAccessory {
this.getSchema(...SCHEMA_CODE.CURRENT),
this.getSchema(...SCHEMA_CODE.POWER),
this.getSchema(...SCHEMA_CODE.VOLTAGE),
this.getSchema(...SCHEMA_CODE.TOTAL_POWER),
);
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/accessory/characteristic/EnergyUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function configureEnergyUsage(
currentSchema?: TuyaDeviceSchema,
powerSchema?: TuyaDeviceSchema,
voltageSchema?: TuyaDeviceSchema,
totalSchema?: TuyaDeviceSchema,
) {

if (currentSchema) {
Expand Down Expand Up @@ -46,6 +47,17 @@ export function configureEnergyUsage(
accessory.log.warn('Unsupported voltage unit %p', currentSchema);
}
}

if (totalSchema) {
if (isUnit(totalSchema, 'kWh', 'kwh')) {
const kwh = createKilowattHourCharacteristic(api);
if (!service.testCharacteristic(kwh)) {
service.addCharacteristic(kwh).onGet(createStatusGetter(accessory, totalSchema));
}
} else {
accessory.log.warn('Unsupported total power unit %p', totalSchema);
}
}
}

function isUnit(schema: TuyaDeviceSchema, ...units: string[]): boolean {
Expand Down Expand Up @@ -103,3 +115,17 @@ function createVoltsCharacteristic(api: API) {
}
};
}

function createKilowattHourCharacteristic(api: API) {
return class Watts extends api.hap.Characteristic {
static readonly UUID = 'E863F10C-079E-48FF-8F27-9C2605A29F52';

constructor() {
super('Total Consumption', Watts.UUID, {
format: api.hap.Formats.FLOAT,
perms: [api.hap.Perms.NOTIFY, api.hap.Perms.PAIRED_READ],
unit: 'kWh',
});
}
};
}

0 comments on commit e7816f1

Please sign in to comment.