Skip to content

Commit

Permalink
delete no longer existing smart scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Jan 7, 2025
1 parent 14be8d0 commit e162f2c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
23 changes: 20 additions & 3 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/main.js.map

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as tools from './lib/tools';
import Api from 'node-hue-api/lib/api/Api';
import GroupState from 'node-hue-api/lib/model/lightstate/GroupState';
import HuePushClient from 'hue-push-client';
import { HueV2Client, Response, RoomData, ZoneData } from './lib/v2/v2-client';
import { HueV2Client, Response, RoomData, SmartSceneData, ZoneData } from './lib/v2/v2-client';
import { MAX_CT, MIN_CT } from './lib/constants';

interface PollSensor {
Expand Down Expand Up @@ -181,7 +181,7 @@ class Hue extends utils.Adapter {
this.clientV2 = new HueV2Client({ user: this.config.user, address: this.config.bridge });

try {
await this.getSmartScenes();
await this.syncSmartScenes();
} catch (e: any) {
this.log.warn(`Could not create smart scenes: ${e.message}`);
}
Expand All @@ -192,10 +192,29 @@ class Hue extends utils.Adapter {
}

/**
* Creates smart scenes for existing groups
* Creates smart scenes for existing groups and deletes no longer existing ones
*/
private async getSmartScenes(): Promise<void> {
private async syncSmartScenes(): Promise<void> {
const scenesData = await this.clientV2.getSmartScenes();
const res = await this.getObjectViewAsync('system', 'state', {
startkey: this.namespace,
endkey: `${this.namespace}\u9999`
});

for (const row of res.rows) {
if (row.value.native?.data?.type !== 'smart_scene') {
continue;
}

const smartSceneId = (row.value.native.data as SmartSceneData).id;
const sceneExistsInBridge = scenesData.data.some(smartScene => smartScene.id === smartSceneId);

if (!sceneExistsInBridge) {
this.log.info(`Deleted smart scene "${smartSceneId}"`);
const groupUuid = (row.value.native.data as SmartSceneData).group.rid;
await this.delObjectAsync(`${groupUuid}.${smartSceneId}`);
}
}

for (const sceneData of scenesData.data) {
const groupUuid = sceneData.group.rid;
Expand Down

0 comments on commit e162f2c

Please sign in to comment.