Skip to content

Commit

Permalink
Add support LED call. Koenkk/zigbee2mqtt#1429
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 16, 2019
1 parent a6d5f32 commit fc75484
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
15 changes: 14 additions & 1 deletion docs/api/classes/_controller_controller_.controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* [setLED](_controller_controller_.controller.md#setled)
* [start](_controller_controller_.controller.md#start)
* [stop](_controller_controller_.controller.md#stop)
* [supportsLED](_controller_controller_.controller.md#supportsled)

## Constructors

Expand Down Expand Up @@ -215,7 +216,7 @@ ___

**setLED**(`enabled`: boolean): *Promise‹void›*

*Defined in [controller/controller.ts:244](https://github.com/Koenkk/zigbee-herdsman/blob/master/src/controller/controller.ts#L244)*
*Defined in [controller/controller.ts:251](https://github.com/Koenkk/zigbee-herdsman/blob/master/src/controller/controller.ts#L251)*

Enable/Disable the LED

Expand Down Expand Up @@ -248,3 +249,15 @@ ___
*Defined in [controller/controller.ts:155](https://github.com/Koenkk/zigbee-herdsman/blob/master/src/controller/controller.ts#L155)*

**Returns:** *Promise‹void›*

___

### supportsLED

**supportsLED**(): *Promise‹boolean›*

*Defined in [controller/controller.ts:244](https://github.com/Koenkk/zigbee-herdsman/blob/master/src/controller/controller.ts#L244)*

Check if the adapters supports LED

**Returns:** *Promise‹boolean›*
2 changes: 2 additions & 0 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class Adapter extends events.EventEmitter {

public abstract reset(type: 'soft' | 'hard'): Promise<void>;

public abstract supportsLED(): Promise<boolean>;

public abstract setLED(enabled: boolean): Promise<void>;

public abstract lqi(networkAddress: number): Promise<TsType.LQI>;
Expand Down
4 changes: 4 additions & 0 deletions src/adapter/z-stack/adapter/zStackAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class ZStackAdapter extends Adapter {
}
}

public async supportsLED(): Promise<boolean> {
return this.version.product !== ZnpVersion.zStack3x0;
}

public async setLED(enabled: boolean): Promise<void> {
await this.znp.request(Subsystem.UTIL, 'ledControl', {ledid: 3, mode: enabled ? 1 : 0});
}
Expand Down
8 changes: 8 additions & 0 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,18 @@ class Controller extends events.EventEmitter {
return Group.create(groupID);
}

/**
* Check if the adapters supports LED
*/
public async supportsLED(): Promise<boolean> {
return this.adapter.supportsLED();
}

/**
* Enable/Disable the LED
*/
public async setLED(enabled: boolean): Promise<void> {
if (!(await this.supportsLED())) throw new Error(`Adapter doesn't support LED`);
await this.adapter.setLED(enabled);
}

Expand Down
6 changes: 6 additions & 0 deletions test/adapter/z-stack/zStackAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,12 @@ describe('zStackAdapter', () => {
expect(mockZnpRequest).toBeCalledWith(Subsystem.UTIL, 'ledControl', {ledid: 3, mode: 1});
});

it('Supports led', async () => {
basicMocks();
await adapter.start();
expect(await adapter.supportsLED()).toBeTruthy();
});

it('Node descriptor', async () => {
basicMocks();
let result;
Expand Down
18 changes: 15 additions & 3 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const mockAdapterSupportsBackup = jest.fn().mockReturnValue(true);
const mockAdapterReset = jest.fn();
const mockAdapterStop = jest.fn();
const mockAdapterStart = jest.fn().mockReturnValue('resumed');
const mockSetLED = jest.fn();
const mockAdapterSetLED = jest.fn();
const mockAdapterSupportsLED = jest.fn().mockReturnValue(true);
const mockAdapterBind = jest.fn();
const mockSendZclFrameGroup = jest.fn();
const mockAdapterUnbind = jest.fn();
Expand Down Expand Up @@ -130,7 +131,8 @@ jest.mock('../src/adapter/z-stack/adapter/zStackAdapter', () => {
backup: () => {return {version: 'dummybackup'}},
getCoordinatorVersion: () => {return {type: 'zStack', meta: {version: 1}}},
getNetworkParameters: () => {return {panID: 1, extenedPanID: 3, channel: 15}},
setLED: mockSetLED,
setLED: mockAdapterSetLED,
supportsLED: mockAdapterSupportsLED,
nodeDescriptor: async (networkAddress) => {
if (mockDevices[networkAddress].nodeDescriptor === 'xiaomi') {
await mockAdapterEvents['zclData']({
Expand Down Expand Up @@ -306,7 +308,17 @@ describe('Controller', () => {
it('Disable led', async () => {
await controller.start();
await controller.setLED(false);
expect(mockSetLED).toBeCalledTimes(1);
expect(mockAdapterSetLED).toBeCalledTimes(1);
});

it('Throw error when not supports LED', async () => {
await controller.start();
mockAdapterSupportsLED.mockReturnValueOnce(false);
expect(await controller.supportsLED()).toBeFalsy();
mockAdapterSupportsLED.mockReturnValueOnce(false);
let error;
try {await controller.setLED(false)} catch (e) {error = e;}
expect(error).toStrictEqual(new Error(`Adapter doesn't support LED`));
});

it('Get coordinator version', async () => {
Expand Down

0 comments on commit fc75484

Please sign in to comment.