Skip to content

Commit

Permalink
Issue workaround: Update group onOff state when device state changes …
Browse files Browse the repository at this point in the history
…from remote controller

AlCalzone/node-tradfri-client#390
  • Loading branch information
mertaksoy committed Jun 2, 2020
1 parent 723f852 commit bf759bb
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DiscoveredGateway, discoverGateway, TradfriClient} from "node-tradfri-client";
import {Accessory, DiscoveredGateway, discoverGateway, Group, GroupInfo, TradfriClient} from "node-tradfri-client";

const cors = require('cors');
const express = require('express');
Expand Down Expand Up @@ -44,12 +44,22 @@ app.listen(port, () => {
tradfri = new TradfriClient(tradfriGateway.host);
tradfri.authenticate(securityCode).then((authToken) => {
tradfri.connect(authToken.identity, authToken.psk);
tradfri.observeDevices();
tradfri.observeGroupsAndScenes();
tradfri.on('device updated', deviceUpdated).observeDevices();

console.log('Authenticated and connected successfully');
console.log(`Example app listening at http://localhost:${port}`)
});
}
}
});
});

function deviceUpdated(device: Accessory) {
Object.keys(tradfri.groups).forEach((key: string) => {
const deviceInGroup = tradfri.groups[key].group.deviceIDs.find((deviceId: number) => deviceId === device.instanceId);
if (deviceInGroup) {
tradfri.groups[key].group.onOff = device.lightList[0].onOff;
}
})
}

0 comments on commit bf759bb

Please sign in to comment.