Skip to content

Commit

Permalink
fix(ignore): Refactor skipTimeResponse to customReadResponse (#7242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Mar 19, 2024
1 parent 988e680 commit 6b3911a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,19 @@ export async function onEvent(type: OnEventType, data: OnEventData, device: Zh.D

// Aqara feeder C1 polls the time during the interview, need to send back the local time instead of the UTC.
// The device.definition has not yet been set - therefore the device.definition.onEvent method does not work.
if (type === 'message' && data.type === 'read' && data.cluster === 'genTime' &&
device.modelID === 'aqara.feeder.acn001') {
device.skipTimeResponse = true;
const oneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const secondsUTC = Math.round(((new Date()).getTime() - oneJanuary2000) / 1000);
const secondsLocal = secondsUTC - (new Date()).getTimezoneOffset() * 60;
await device.getEndpoint(1).readResponse('genTime', data.meta.zclTransactionSequenceNumber, {time: secondsLocal});
if (device.modelID === 'aqara.feeder.acn001' && !device.customReadResponse) {
device.customReadResponse = (frame, endpoint) => {
if (frame.isCluster('genTime')) {
const oneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const secondsUTC = Math.round(((new Date()).getTime() - oneJanuary2000) / 1000);
const secondsLocal = secondsUTC - (new Date()).getTimezoneOffset() * 60;
device.getEndpoint(1).readResponse('genTime', data.meta.zclTransactionSequenceNumber, {time: secondsLocal}).catch((e) => {
logger.logger.warn(`ZNCWWSQ01LM custom time response failed: ${e}`);
})
return true;
}
return false;
}
}
}

Expand Down
44 changes: 26 additions & 18 deletions src/lib/modernExtend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getFromLookup, getEndpointName, assertNumber, postfixWithEndpointName,
noOccupancySince, precisionRound, batteryVoltageToPercentage, getOptions,
} from './utils';
import * as logger from '../lib/logger';

function getEndpointsWithInputCluster(device: Zh.Device, cluster: string | number) {
if (!device.endpoints) {
Expand Down Expand Up @@ -666,25 +667,32 @@ export function reconfigureReportingsOnDeviceAnnounce(): ModernExtend {
}

export function customTimeResponse(start: '1970_UTC' | '2000_LOCAL'): ModernExtend {
// The Zigbee Cluster Library specification states that the genTime.time response should be the
// number of seconds since 1st Jan 2000 00:00:00 UTC. This extend modifies that:
// 1970_UTC: number of seconds since the Unix Epoch (1st Jan 1970 00:00:00 UTC)
// 2000_LOCAL: seconds since 1 January in the local time zone.
// Disable the responses of zigbee-herdsman and respond here instead.
const onEvent: OnEvent = async (type, data, device, options, state: KeyValue) => {
device.skipTimeResponse = true;
// The Zigbee Cluster Library specification states that the genTime.time response should be the
// number of seconds since 1st Jan 2000 00:00:00 UTC. This extend modifies that:
// 1970_UTC: number of seconds since the Unix Epoch (1st Jan 1970 00:00:00 UTC)
// 2000_LOCAL: seconds since 1 January in the local time zone.
// Disable the responses of zigbee-herdsman and respond here instead.
if (type === 'message' && data.type === 'read' && data.cluster === 'genTime') {
const payload: KeyValue = {};
if (start === '1970_UTC') {
const time = Math.round(((new Date()).getTime()) / 1000);
payload.time = time;
payload.localTime = time - (new Date()).getTimezoneOffset() * 60;
} else if (start === '2000_LOCAL') {
const oneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const secondsUTC = Math.round(((new Date()).getTime() - oneJanuary2000) / 1000);
payload.time = secondsUTC - (new Date()).getTimezoneOffset() * 60;
}
await data.endpoint.readResponse('genTime', data.meta.zclTransactionSequenceNumber, payload);
if (!device.customReadResponse) {
device.customReadResponse = (frame, endpoint) => {
if (frame.isCluster('genTime')) {
const payload: KeyValue = {};
if (start === '1970_UTC') {
const time = Math.round(((new Date()).getTime()) / 1000);
payload.time = time;
payload.localTime = time - (new Date()).getTimezoneOffset() * 60;
} else if (start === '2000_LOCAL') {
const oneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const secondsUTC = Math.round(((new Date()).getTime() - oneJanuary2000) / 1000);
payload.time = secondsUTC - (new Date()).getTimezoneOffset() * 60;
}
data.endpoint.readResponse('genTime', data.meta.zclTransactionSequenceNumber, payload).catch((e) => {
logger.logger.warn(`Custom time response failed for '${device.ieeeAddr}': ${e}`);
});
return true;
}
return false;
};
}
};

Expand Down

0 comments on commit 6b3911a

Please sign in to comment.