Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get the group a light is in #14

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from './errors/lightErrors';
import { ER_CLIENT_INVALID_ARGUMENT } from './errors/clientErrors';
import { SetTileState64Request, SetUserPositionRequest, StateDeviceChainResponse } from './packets/tiles/tiles';
import { Group } from './packets/group/group';

export enum LightEvents {
CONECTIVITY = 'connectivity',
Expand Down Expand Up @@ -71,6 +72,7 @@ export class Light extends EventEmitter {
private _client: Client;
private _connectivity: boolean;
private _label: string;
private _group: Group;
private _power: boolean;
private _color: ColorHSBK;

Expand Down Expand Up @@ -791,6 +793,47 @@ export class Light extends EventEmitter {
});
}

public getGroup(cache = false, timeout: number = DEFAULT_MSG_REPLY_TIMEOUT): Promise<Group> {
const ctx = this;

return new Promise((resolve, reject) => {
if (!ctx.connectivity) {
return reject(new ServiceErrorBuilder(ER_LIGHT_OFFLINE).withContextualMessage(`Id: ${ctx.id}`).build());
}

if (cache === true) {
if (ctx._group) {
return resolve(ctx._group);
}
}

const cmdReq = { target: this.id };
const packetObj = createObject(packet.getGroup.type, cmdReq, ctx._client.source);
const sqnNumber = ctx._client.send(packetObj);
const timeoutHandle = setTimeout(() => {
reject(new ServiceErrorBuilder(ER_LIGHT_CMD_TIMEOUT).withContextualMessage(`Id: ${ctx.id}`).build());
}, timeout);

ctx._client.addMessageHandler(
packet.stateGroup.name,
(err: Error, data) => {
if (err) {
return reject(err);
}

clearTimeout(timeoutHandle);

return resolve({
group: data.group,
label: data.label,
updatedAt: data.updatedAt
});
},
sqnNumber
);
});
}

public getTags(timeout: number = DEFAULT_MSG_REPLY_TIMEOUT) {
const ctx = this;

Expand Down