Skip to content

Commit

Permalink
feat: Add scene_rename command (#6092)
Browse files Browse the repository at this point in the history
* Build now works regardless of setTimeout return type

For unknown (to me) reasons, I saw builds failing because the
browser-compatible version of setTimeout/clearTimeout was being
used, instead of the Node-version, resulting in errors like:

```
src/lib/ota/common.ts:405:43 - error TS2345: Argument of type 'Timer' is not assignable to parameter of type 'string | number | Timeout'.
405                             clearInterval(timer);
```

As I'm unsure of the cause and the desired behaviour, my fix
is to support both.

* Added scene_rename support

---------

Co-authored-by: Frank van Viegen <frank@vanviegen.net>
  • Loading branch information
vanviegen and Frank van Viegen committed Aug 20, 2023
1 parent 5264bd0 commit bc696cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
30 changes: 30 additions & 0 deletions src/converters/toZigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -4530,6 +4530,36 @@ const converters = {
meta.logger.info('Successfully removed all scenes');
},
},
scene_rename: {
key: ['scene_rename'],
convertSet: (entity, key, value, meta) => {
if (typeof value !== 'object') {
throw new Error('Payload should be object.');
}
const isGroup = entity.constructor.name === 'Group';
const sceneid = value.ID;
const scenename = value.name;
const groupid = isGroup ? entity.groupID : value.hasOwnProperty('group_id') ? value.group_id : 0;

if (isGroup) {
if (meta.membersState) {
for (const member of entity.members) {
const state = utils.getSceneState(member, sceneid, groupid);
if (state) {
utils.saveSceneState(member, sceneid, groupid, state, scenename);
}
}
}
} else {
const state = utils.getSceneState(entity, sceneid, groupid);
if (!state) {
throw new Error(`No such scene in device meta data`);
}
utils.saveSceneState(entity, sceneid, groupid, state, scenename);
}
meta.logger.info('Successfully renamed scene');
},
},
TS0003_curtain_switch: {
key: ['state'],
convertSet: async (entity, key, value, meta) => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function addDefinition(definition) {
};
}

definition.toZigbee.push(tz.scene_store, tz.scene_recall, tz.scene_add, tz.scene_remove, tz.scene_remove_all, tz.read, tz.write,
definition.toZigbee.push(tz.scene_store, tz.scene_recall, tz.scene_add, tz.scene_remove, tz.scene_remove_all, tz.scene_rename, tz.read, tz.write,
tz.command, tz.factory_reset);

if (definition.exposes && Array.isArray(definition.exposes) && !definition.exposes.find((e) => e.name === 'linkquality')) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ota/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export async function updateToLatest(device: Zh.Device, logger: Logger, onProgre
logger.debug(`Update succeeded, waiting for device announce`);
onProgress(100, null);

let timer: NodeJS.Timer = null;
let timer: ReturnType<typeof setTimeout> = null;
const cb = () => {
logger.debug('Got device announce or timed out, call resolve');
clearInterval(timer);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export function normalizeCelsiusVersionOfFahrenheit(value: number) {
export function noOccupancySince(endpoint: Zh.Endpoint, options: KeyValueAny, publish: Publish, action: 'start' | 'stop') {
if (options && options.no_occupancy_since) {
if (action == 'start') {
globalStore.getValue(endpoint, 'no_occupancy_since_timers', []).forEach((t: NodeJS.Timer) => clearTimeout(t));
globalStore.getValue(endpoint, 'no_occupancy_since_timers', []).forEach((t: ReturnType<typeof setInterval>) => clearTimeout(t));
globalStore.putValue(endpoint, 'no_occupancy_since_timers', []);

options.no_occupancy_since.forEach((since: number) => {
Expand All @@ -449,7 +449,7 @@ export function noOccupancySince(endpoint: Zh.Endpoint, options: KeyValueAny, pu
globalStore.getValue(endpoint, 'no_occupancy_since_timers').push(timer);
});
} else if (action === 'stop') {
globalStore.getValue(endpoint, 'no_occupancy_since_timers', []).forEach((t: NodeJS.Timer) => clearTimeout(t));
globalStore.getValue(endpoint, 'no_occupancy_since_timers', []).forEach((t: ReturnType<typeof setInterval>) => clearTimeout(t));
globalStore.putValue(endpoint, 'no_occupancy_since_timers', []);
}
}
Expand Down

0 comments on commit bc696cf

Please sign in to comment.