Skip to content

Commit

Permalink
feat: Add turnOn() and turnOff() commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Jan 21, 2022
1 parent 3ba9694 commit 82e52d4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
34 changes: 28 additions & 6 deletions src/lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import {
NodePyATVDeviceState,
NodePyATVExecutableType,
NodePyATVGetStateOptions,
NodePyATVInternalKeys, NodePyATVKeys,
NodePyATVInternalKeys,
NodePyATVKeys,
NodePyATVMediaType,
NodePyATVProtocol,
NodePyATVRepeatState,
NodePyATVShuffleState,
NodePyATVState
} from './types';

import {addRequestId, getParamters, parseState, removeRequestId, request} from './tools';
import {NodePyATVDeviceEvents, NodePyATVDeviceEvent} from '../lib';
import { addRequestId, getParamters, parseState, removeRequestId, request } from './tools';
import { NodePyATVDeviceEvent, NodePyATVDeviceEvents } from '../lib';
import { EventEmitter } from 'events';

/**
Expand Down Expand Up @@ -315,12 +316,15 @@ export default class NodePyATVDevice implements EventEmitter{
return state.appId;
}

private async _pressKey(key: NodePyATVInternalKeys) {
private async _pressKey(key: NodePyATVInternalKeys, executableType = NodePyATVExecutableType.atvscript) {
const id = addRequestId();
const parameters = getParamters(this.options);

const result = await request(id, NodePyATVExecutableType.atvscript, [...parameters, key], this.options);
if (typeof result !== 'object' || result.result !== 'success') {
const result = await request(id, executableType, [...parameters, key], this.options);
if (
executableType === NodePyATVExecutableType.atvscript &&
(typeof result !== 'object' || result.result !== 'success')
) {
throw new Error(`Unable to parse pyatv response: ${JSON.stringify(result, null, ' ')}`);
}

Expand Down Expand Up @@ -478,6 +482,7 @@ export default class NodePyATVDevice implements EventEmitter{
/**
* Send the "suspend" command
* @category Control
* @deprecated
*/
async suspend(): Promise<void> {
await this._pressKey(NodePyATVInternalKeys.suspend);
Expand Down Expand Up @@ -518,11 +523,28 @@ export default class NodePyATVDevice implements EventEmitter{
/**
* Send the "wakeup" command
* @category Control
* @deprecated
*/
async wakeup(): Promise<void> {
await this._pressKey(NodePyATVInternalKeys.wakeup);
}

/**
* Send the "turn_off" command
* @category Control
*/
async turnOff(): Promise<void> {
await this._pressKey(NodePyATVInternalKeys.turnOff, NodePyATVExecutableType.atvremote);
}

/**
* Send the "turn_on" command
* @category Control
*/
async turnOn(): Promise<void> {
await this._pressKey(NodePyATVInternalKeys.turnOn, NodePyATVExecutableType.atvremote);
}

/**
* Add an event listener. Will start the event subscription with the
* Apple TV as long as there are listeners for any event registered.
Expand Down
4 changes: 3 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export enum NodePyATVInternalKeys {
up = 'up',
volumeDown = 'volume_down',
volumeUp = 'volume_up',
wakeup = 'wakeup'
wakeup = 'wakeup',
turnOff = 'turn_off',
turnOn = 'turn_on'
}

export enum NodePyATVListenerState {
Expand Down

0 comments on commit 82e52d4

Please sign in to comment.