Skip to content

Commit

Permalink
feat: Support focus state and volume events
Browse files Browse the repository at this point in the history
node-pyatv now supports `focusState` events as well as `volume` events, which are available since [pyatv 0.12.0](https://github.com/postlund/pyatv/releases/tag/v0.12.0).
  • Loading branch information
sebbo2002 committed Jun 14, 2023
1 parent 1662639 commit 9fb10c6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
28 changes: 26 additions & 2 deletions src/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import {
NodePyATVDeviceState,
NodePyATVExecutableType,
NodePyATVFindAndInstanceOptions,
NodePyATVFocusState,
NodePyATVInstanceOptions,
NodePyATVInternalState,
NodePyATVMediaType, NodePyATVPowerState, NodePyATVRepeatState, NodePyATVShuffleState,
NodePyATVMediaType,
NodePyATVPowerState,
NodePyATVRepeatState,
NodePyATVShuffleState,
NodePyATVState
} from './types.js';

Expand Down Expand Up @@ -247,7 +251,9 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
repeat: null,
app: null,
appId: null,
powerState: null
powerState: null,
volume: null,
focusState: null
};
if (!input || typeof input !== 'object') {
return result;
Expand Down Expand Up @@ -375,5 +381,23 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
d(`No powerState value found in input (${JSON.stringify(input)})`);
}

// volume
if (typeof input.volume === 'number') {
result.volume = input.volume;
}

// focusState
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];
}
else {
d(`Unsupported focusState value ${input.focus_state}, ignore attribute`);
}
} else {
d(`No focusState value found in input (${JSON.stringify(input)})`);
}

return result;
}
75 changes: 42 additions & 33 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export enum NodePyATVPowerState {
off = 'off'
}

export enum NodePyATVFocusState {
focued = 'focused',
unfocused = 'unfocused'
}

export enum NodePyATVKeys {
down = 'down',
home = 'home',
Expand Down Expand Up @@ -183,44 +188,48 @@ export interface NodePyATVInternalScanDevice {
* @internal
*/
export interface NodePyATVInternalState {
result?: string | unknown,
datetime?: string | unknown,
hash?: string | unknown,
media_type?: string | unknown,
device_state?: string | unknown,
title?: string | unknown,
artist?: string | unknown,
album?: string | unknown,
genre?: string | unknown,
total_time?: number | unknown,
position?: 1 | unknown,
shuffle?: string | unknown,
repeat?: string | unknown,
app?: string | unknown,
app_id?: string | unknown,
power_state?: string | unknown,
push_updates?: string | unknown,
exception?: string | unknown,
stacktrace?: string | unknown,
connection?: string | unknown
result?: string | unknown;
datetime?: string | unknown;
hash?: string | unknown;
media_type?: string | unknown;
device_state?: string | unknown;
title?: string | unknown;
artist?: string | unknown;
album?: string | unknown;
genre?: string | unknown;
total_time?: number | unknown;
position?: 1 | unknown;
shuffle?: string | unknown;
repeat?: string | unknown;
app?: string | unknown;
app_id?: string | unknown;
power_state?: string | unknown;
push_updates?: string | unknown;
exception?: string | unknown;
stacktrace?: string | unknown;
connection?: string | unknown;
volume?: number | unknown;
focus_state?: string | unknown;
}

export interface NodePyATVState {
dateTime: Date | null;
hash: string | null;
mediaType: NodePyATVMediaType | null,
deviceState: NodePyATVDeviceState | null,
title: string | null,
artist: string | null,
album: string | null,
genre: string | null,
totalTime: number | null,
position: number | null,
shuffle: NodePyATVShuffleState | null,
repeat: NodePyATVRepeatState | null,
app: string | null,
appId: string | null,
powerState: NodePyATVPowerState | null
mediaType: NodePyATVMediaType | null;
deviceState: NodePyATVDeviceState | null;
title: string | null;
artist: string | null;
album: string | null;
genre: string | null;
totalTime: number | null;
position: number | null;
shuffle: NodePyATVShuffleState | null;
repeat: NodePyATVRepeatState | null;
app: string | null;
appId: string | null;
powerState: NodePyATVPowerState | null;
volume: number | null;
focusState: NodePyATVFocusState | null;
}

export interface NodePyATVApp {
Expand Down

0 comments on commit 9fb10c6

Please sign in to comment.