Skip to content

Commit

Permalink
feat(Events): Add powerState events
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Nov 29, 2020
1 parent 05cecdc commit 0890009
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
18 changes: 16 additions & 2 deletions lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NodePyATVFindAndInstanceOptions,
NodePyATVInstanceOptions,
NodePyATVInternalState,
NodePyATVMediaType, NodePyATVRepeatState, NodePyATVShuffleState,
NodePyATVMediaType, NodePyATVPowerState, NodePyATVRepeatState, NodePyATVShuffleState,
NodePyATVState
} from './types';

Expand Down Expand Up @@ -240,7 +240,8 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
};
if (!input || typeof input !== 'object') {
return result;
Expand Down Expand Up @@ -347,5 +348,18 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
parseStateStringAttr(input, result, 'app', 'app', d);
parseStateStringAttr(input, result, 'app_id', 'appId', d);

// powerState
if(typeof input.power_state === 'string') {
const validValues = Object.keys(NodePyATVPowerState).map(o => String(o));
if (validValues.includes(input.power_state)) {
result.powerState = NodePyATVPowerState[input.power_state as NodePyATVPowerState];
}
else {
d(`Unsupported powerState value ${input.power_state}, ignore attribute`);
}
} else {
d(`No powerState value found in input (${JSON.stringify(input)})`);
}

return result;
}
10 changes: 8 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export enum NodePyATVShuffleState {
off = 'off'
}

export enum NodePyATVPowerState {
on = 'on',
off = 'off'
}

export enum NodePyATVKeys {
down = 'down',
home = 'home',
Expand Down Expand Up @@ -101,7 +106,7 @@ export enum NodePyATVListenerState {


export type NodePyATVStateIndex = ('dateTime' | 'hash' | 'mediaType' | 'deviceState' | 'title' | 'artist' | 'album' |
'genre' | 'totalTime' | 'position' | 'shuffle' | 'repeat' | 'app' | 'appId');
'genre' | 'totalTime' | 'position' | 'shuffle' | 'repeat' | 'app' | 'appId' | 'powerState');

export type NodePyATVEventValueType = (string | number | NodePyATVMediaType | NodePyATVDeviceState |
NodePyATVShuffleState | NodePyATVRepeatState);
Expand Down Expand Up @@ -182,5 +187,6 @@ export interface NodePyATVState {
shuffle: NodePyATVShuffleState | null,
repeat: NodePyATVRepeatState | null,
app: string | null,
appId: string | null
appId: string | null,
powerState: NodePyATVPowerState | null
}
31 changes: 30 additions & 1 deletion test/device-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import assert from 'assert';
import NodePyATVDevice from '../lib/device';
import {createFakeSpawn} from '../lib/fake-spawn';
import NodePyATVDeviceEvent from '../lib/device-event';
import {NodePyATVPowerState} from '../lib/types';

describe('NodePyATVDeviceEvents', function () {
describe('applyStateAndEmitEvents()', function () {
Expand Down Expand Up @@ -154,6 +155,34 @@ describe('NodePyATVDeviceEvents', function () {

assert.strictEqual(callCounter, 1);
});
it('should also work with powerState', async function () {
const device = new NodePyATVDevice({
name: 'My Testdevice',
host: '192.168.178.2',
spawn: createFakeSpawn(cp => {
cp.onStdIn(() => cp.end());
cp.stdout({
result: 'success',
datetime: new Date().toJSON(),
power_state: 'off'
});
})
});

await new Promise(cb => {
device.once('update:powerState', event => {
assert.ok(event instanceof NodePyATVDeviceEvent);
assert.strictEqual(event.key, 'powerState');
assert.strictEqual(event.oldValue, null);
assert.strictEqual(event.newValue, 'off');
assert.strictEqual(event.newValue, NodePyATVPowerState.off);
assert.strictEqual(event.value, 'off');
assert.strictEqual(event.value, NodePyATVPowerState.off);
assert.deepStrictEqual(event.device, device);
cb(undefined);
});
});
});
});

describe('start|stopListening()', function () {
Expand Down Expand Up @@ -223,7 +252,7 @@ describe('NodePyATVDeviceEvents', function () {
device.once('error', err => {
assert.ok(err instanceof Error);
assert.ok(err.toString().includes(
'Unable to parse stdout json: SyntaxError: '+
'Unable to parse stdout json: SyntaxError: ' +
'Unexpected token # in JSON at position 0'
));
cb(undefined);
Expand Down
3 changes: 2 additions & 1 deletion test/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ describe('NodePyATVDevice', function () {
shuffle: NodePyATVShuffleState.off,
repeat: NodePyATVRepeatState.off,
app: 'Disney+',
appId: 'com.disney.disneyplus'
appId: 'com.disney.disneyplus',
powerState: null
});
});
it('should cache requests for a bit', async function () {
Expand Down
24 changes: 16 additions & 8 deletions test/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ describe('Tools', function () {
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
});
});
it('should work without data', function () {
Expand All @@ -154,7 +155,8 @@ describe('Tools', function () {
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
});
});
it('should work with example data', function () {
Expand All @@ -173,7 +175,8 @@ describe('Tools', function () {
shuffle: 'off',
repeat: 'off',
app: 'Disney+',
app_id: 'com.disney.disneyplus'
app_id: 'com.disney.disneyplus',
powerState: null
};
const result = parseState(input, '', {});
assert.deepStrictEqual(result, {
Expand All @@ -190,7 +193,8 @@ describe('Tools', function () {
shuffle: NodePyATVShuffleState.off,
repeat: NodePyATVRepeatState.off,
app: 'Disney+',
appId: 'com.disney.disneyplus'
appId: 'com.disney.disneyplus',
powerState: null
});
});
it('should ignore date if it\'s an invalid date', function () {
Expand All @@ -210,7 +214,8 @@ describe('Tools', function () {
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
});
});
it('should ignore data if unsupported type', function () {
Expand All @@ -229,7 +234,8 @@ describe('Tools', function () {
shuffle: false,
repeat: true,
app: 0,
app_id: 891645381647289
app_id: 891645381647289,
powerState: null
};
const result = parseState(input, '', {});
assert.deepStrictEqual(result, {
Expand All @@ -246,7 +252,8 @@ describe('Tools', function () {
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
});
});
it('should ignore enums with unsupported valid', function () {
Expand All @@ -271,7 +278,8 @@ describe('Tools', function () {
shuffle: null,
repeat: null,
app: null,
appId: null
appId: null,
powerState: null
});
});
});
Expand Down

0 comments on commit 0890009

Please sign in to comment.