Skip to content

Commit

Permalink
feat: Add outputDevices Support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Jan 15, 2024
1 parent 60a414d commit cf194fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/lib/device-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ export default class NodePyATVDeviceEvents extends EventEmitter {
applyStateAndEmitEvents(newState: NodePyATVState): void {
let keys = Object.keys(this.state);

if('power_state' in newState && newState.power_state) {
keys = ['power_state'];
if('powerState' in newState && newState.powerState) {
keys = ['powerState'];
}
if('focus_state' in newState && newState.focus_state) {
keys = ['focus_state'];
if('focusState' in newState && newState.focusState) {
keys = ['focusState'];
}
if('outputDevices' in newState && newState.outputDevices) {
keys = ['outputDevices'];
}

// Volume events don't hold the complete state…
Expand Down
10 changes: 8 additions & 2 deletions src/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
appId: null,
powerState: null,
volume: null,
focusState: null
focusState: null,
outputDevices: null
};
if (!input || typeof input !== 'object') {
return result;
Expand Down Expand Up @@ -390,7 +391,7 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
if(typeof input.focus_state === 'string') {
const validValues = Object.keys(NodePyATVFocusState).map(o => String(o));
if (validValues.includes(input.focus_state)) {
result.focusState = NodePyATVFocusState[input.power_state as NodePyATVFocusState];
result.focusState = NodePyATVFocusState[input.focus_state as NodePyATVFocusState];
}
else {
d(`Unsupported focusState value ${input.focus_state}, ignore attribute`);
Expand All @@ -399,5 +400,10 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
d(`No focusState value found in input (${JSON.stringify(input)})`);
}

// outputDevices
if (Array.isArray(input.output_devices)) {
result.outputDevices = input.output_devices;
}

return result;
}
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export interface NodePyATVInternalState {
connection?: string | unknown;
volume?: number | unknown;
focus_state?: string | unknown;
output_devices?: Array<{ name: string; identifier: string; }>;
}

export interface NodePyATVState {
Expand All @@ -234,6 +235,7 @@ export interface NodePyATVState {
powerState: NodePyATVPowerState | null;
volume: number | null;
focusState: NodePyATVFocusState | null;
outputDevices: Array<{ name: string; identifier: string; }> | null;
}

export interface NodePyATVApp {
Expand Down
3 changes: 2 additions & 1 deletion test/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ describe('NodePyATVDevice', function () {
repeat: NodePyATVRepeatState.off,
app: 'Disney+',
appId: 'com.disney.disneyplus',
powerState: null
powerState: null,
outputDevices: null
});
});
it('should reject with error if pyatv fails', 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 @@ -141,7 +141,8 @@ describe('Tools', function () {
appId: null,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
it('should work without data', function () {
Expand All @@ -164,7 +165,8 @@ describe('Tools', function () {
appId: null,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
it('should work with example data', function () {
Expand All @@ -186,7 +188,8 @@ describe('Tools', function () {
app_id: 'com.disney.disneyplus',
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
};
const result = parseState(input, '', {});
assert.deepStrictEqual(result, {
Expand All @@ -206,7 +209,8 @@ describe('Tools', function () {
appId: 'com.disney.disneyplus',
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
it('should throw an error for pyatv exceptions', function () {
Expand Down Expand Up @@ -240,7 +244,8 @@ describe('Tools', function () {
appId: null,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
it('should ignore data if unsupported type', function () {
Expand All @@ -262,7 +267,8 @@ describe('Tools', function () {
app_id: 891645381647289,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
};
const result = parseState(input, '', {});
assert.deepStrictEqual(result, {
Expand All @@ -282,7 +288,8 @@ describe('Tools', function () {
appId: null,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
it('should ignore enums with unsupported valid', function () {
Expand Down Expand Up @@ -310,7 +317,8 @@ describe('Tools', function () {
appId: null,
powerState: null,
focusState: null,
volume: null
volume: null,
outputDevices: null
});
});
});
Expand Down

0 comments on commit cf194fd

Please sign in to comment.