Compare commits
39 Commits
94dc6a37b9
...
1.2.0
Author | SHA1 | Date | |
---|---|---|---|
4519edf679 | |||
6794d58594 | |||
5470b6185d | |||
51441a706d | |||
e6e3d45b5b | |||
c22a8a0325 | |||
506c170746 | |||
4fd9341cea | |||
c65853220e | |||
1a51e4d8c5 | |||
503b4c9d59 | |||
63cd6ce3ce | |||
ff1f500fa1 | |||
686054e9b3 | |||
d7e241b381 | |||
3a2db8397b | |||
f0c4cd7824 | |||
1d8d483e8f | |||
904b8c4fab | |||
a01ae86794 | |||
f28b6429c5 | |||
5eff708b05 | |||
ece54b54e7 | |||
6b3ea14520 | |||
5dc28b2409 | |||
cdab6327f5 | |||
1121dbc52c | |||
bc2208b5cb | |||
845071c274 | |||
2612f61082 | |||
5a8b03a038 | |||
6c363f8d8c | |||
cea0c3f1cf | |||
2b13f78a0d | |||
810431d82b | |||
118a8912f6 | |||
f78c9d961c | |||
ae78cb2358 | |||
7d89f6b945 |
@ -1 +0,0 @@
|
|||||||
source /nvm.sh
|
|
80
.drone.yml
Normal file
80
.drone.yml
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
clone:
|
||||||
|
disable: true
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: clone
|
||||||
|
image: alpine/git
|
||||||
|
commands:
|
||||||
|
- git clone https://gitea.watsonlabs.net/watsonb8/homebridge-harmony-control.git .
|
||||||
|
- git checkout $DRONE_COMMIT
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- npm install
|
||||||
|
- npm run build
|
||||||
|
|
||||||
|
- name: version
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- export version=`node -p "require('./package.json').version"`
|
||||||
|
- export commit=`echo $DRONE_COMMIT | cut -c1-5`
|
||||||
|
- npm version prerelease --preid=$commit --git-tag-version=false --allow-same-version=true
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- tag
|
||||||
|
- pull_request
|
||||||
|
branch:
|
||||||
|
include:
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: publish pre
|
||||||
|
image: plugins/npm
|
||||||
|
settings:
|
||||||
|
username:
|
||||||
|
from_secret: npm_username
|
||||||
|
password:
|
||||||
|
from_secret: npm_password
|
||||||
|
email: brandon@watsonlabs.net
|
||||||
|
registry: "http://10.44.1.6:4873/"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- tag
|
||||||
|
- pull_request
|
||||||
|
branch:
|
||||||
|
include:
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: publish tagged version
|
||||||
|
image: plugins/npm
|
||||||
|
settings:
|
||||||
|
username:
|
||||||
|
from_secret: npm_username
|
||||||
|
password:
|
||||||
|
from_secret: npm_password
|
||||||
|
email: brandon@watsonlabs.net
|
||||||
|
registry: "http://10.44.1.6:4873/"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- tag
|
||||||
|
exclude:
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
- name: Notify
|
||||||
|
image: drillster/drone-email
|
||||||
|
settings:
|
||||||
|
host: 10.44.1.13
|
||||||
|
username: srvGitea
|
||||||
|
password:
|
||||||
|
from_secret: smtp_password
|
||||||
|
from: drone@watsonlabs.net
|
||||||
|
skip_verify: true
|
||||||
|
when:
|
||||||
|
status:
|
||||||
|
- failure
|
22
deploy.sh
22
deploy.sh
@ -1,22 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
remote_user="bmw"
|
|
||||||
remote_server="linuxhost.me"
|
|
||||||
deploy_location="/home/bmw/homebridge-harmony-control"
|
|
||||||
homebridge_location="/var/lib/homebridge/"
|
|
||||||
|
|
||||||
#build
|
|
||||||
tsc --build
|
|
||||||
#copy files to remote machine
|
|
||||||
scp -r bin $remote_user@$remote_server:$deploy_location
|
|
||||||
scp package.json $remote_user@$remote_server:$deploy_location
|
|
||||||
|
|
||||||
#install package
|
|
||||||
ssh -t $remote_user@$remote_server "sudo npm install -g --unsafe-perm $deploy_location"
|
|
||||||
|
|
||||||
#restart service
|
|
||||||
ssh -t
|
|
||||||
ssh -t $remote_user@$remote_server "sudo systemctl restart homebridge.service"
|
|
||||||
ssh -t $remote_user@$remote_server "sudo systemctl status homebridge.service"
|
|
||||||
|
|
||||||
echo done
|
|
||||||
exit
|
|
@ -3,5 +3,8 @@
|
|||||||
{
|
{
|
||||||
"path": "."
|
"path": "."
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"settings": {
|
||||||
|
"editor.tabSize": 2
|
||||||
|
}
|
||||||
}
|
}
|
4570
package-lock.json
generated
4570
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
85
package.json
85
package.json
@ -1,42 +1,47 @@
|
|||||||
{
|
{
|
||||||
"name": "homebridge-harmony-control",
|
"name": "@watsonb8/homebridge-harmony-control",
|
||||||
"version": "1.0.7",
|
"version": "1.2.0",
|
||||||
"description": "Homebridge platform to control smart home equipment by room.",
|
"description": "Homebridge platform to control smart home equipment by room.",
|
||||||
"main": "bin/index.js",
|
"main": "bin/index.js",
|
||||||
"scripts": {
|
"publishConfig": {
|
||||||
"build": "tsc --build",
|
"registry": "http://10.44.1.6:4873/"
|
||||||
"prepublishOnly": "npm run build"
|
},
|
||||||
},
|
"scripts": {
|
||||||
"repository": {
|
"build": "tsc --build",
|
||||||
"type": "git",
|
"prepublishOnly": "npm run build"
|
||||||
"url": "ssh://git@watsonb8.ddns.net:2122/misc/homebridge-harmony-control.git"
|
},
|
||||||
},
|
"repository": {
|
||||||
"author": "Brandon Watson",
|
"type": "git",
|
||||||
"license": "ISC",
|
"url": "ssh://git@watsonb8.ddns.net:2122/misc/homebridge-harmony-control.git"
|
||||||
"keywords": [
|
},
|
||||||
"homebridge-plugin",
|
"author": "Brandon Watson",
|
||||||
"harmony",
|
"license": "ISC",
|
||||||
"websocket",
|
"keywords": [
|
||||||
"harmonyhub",
|
"homebridge-plugin",
|
||||||
"homekit"
|
"harmony",
|
||||||
],
|
"websocket",
|
||||||
"engines": {
|
"harmonyhub",
|
||||||
"homebridge": ">=1.1.6",
|
"homekit"
|
||||||
"node": ">=7.6.0"
|
],
|
||||||
},
|
"engines": {
|
||||||
"devDependencies": {
|
"homebridge": ">=1.1.6",
|
||||||
"rollup-plugin-typescript": "^1.0.1",
|
"node": ">=7.6.0"
|
||||||
"tslib": "^1.10.0",
|
},
|
||||||
"tslint": "^5.17.0",
|
"devDependencies": {
|
||||||
"typescript": "^3.5.1"
|
"homebridge": "^1.2.5",
|
||||||
},
|
"rollup-plugin-typescript": "^1.0.1",
|
||||||
"dependencies": {
|
"tslib": "^1.10.0",
|
||||||
"@babel/core": "^7.4.5",
|
"tslint": "^5.17.0",
|
||||||
"@babel/preset-env": "^7.4.5",
|
"typescript": "^3.9.10"
|
||||||
"@babel/preset-typescript": "^7.3.3",
|
},
|
||||||
"@types/node": "^12.0.7",
|
"dependencies": {
|
||||||
"harmony-websocket": "^1.1.0",
|
"@babel/core": "^7.4.5",
|
||||||
"homebridge": "^1.1.16",
|
"@babel/preset-env": "^7.4.5",
|
||||||
"request": "^2.88.0"
|
"@babel/preset-typescript": "^7.3.3",
|
||||||
}
|
"@types/node": "^12.0.7",
|
||||||
|
"harmony-websocket": "^1.1.0",
|
||||||
|
"reflect-metadata": "^0.1.13",
|
||||||
|
"request": "^2.88.0",
|
||||||
|
"tsyringe": "^4.6.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
export { ControlUnit } from './ControlUnit';
|
|
||||||
export { DeviceButton } from './DeviceButton';
|
|
@ -1,404 +0,0 @@
|
|||||||
import { IActivity } from "../Models/Config/IActivity";
|
|
||||||
import { IDeviceSetupItem } from "../Models/Config/IDeviceSetupItem";
|
|
||||||
import { IInput, IMatrix, IOutput } from "../Models/Config/IMatrix";
|
|
||||||
import { RemoteKey } from "../Accessories/ControlUnit";
|
|
||||||
import { EventEmitter } from "events";
|
|
||||||
import { IHub } from "../Models/Config/IHub";
|
|
||||||
import { IDeviceConfig } from "../Models/Config/IDeviceConfig";
|
|
||||||
import { HarmonyDevice } from "../Models/HarmonyDevice";
|
|
||||||
import { HarmonyHub } from "../Models/HarmonyHub";
|
|
||||||
|
|
||||||
const Harmony = require("harmony-websocket");
|
|
||||||
|
|
||||||
interface IActivityState {
|
|
||||||
currentActivity: IActivity;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IHarmonyDataProviderProps {
|
|
||||||
hubs: Array<IHub>;
|
|
||||||
deviceConfigs: Array<IDeviceConfig>;
|
|
||||||
log: any;
|
|
||||||
matrix: IMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
class HarmonyDataProvider extends EventEmitter {
|
|
||||||
private _log: any;
|
|
||||||
private _hubsByDevice: { [deviceName: string]: string } = {};
|
|
||||||
private _hubs: { [hubName: string]: HarmonyHub } = {};
|
|
||||||
// private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {};
|
|
||||||
private _states: {
|
|
||||||
[controlUnitName: string]: IActivityState | undefined;
|
|
||||||
} = {};
|
|
||||||
|
|
||||||
private _matrix: IMatrix;
|
|
||||||
|
|
||||||
constructor(props: IHarmonyDataProviderProps) {
|
|
||||||
super();
|
|
||||||
this._log = props.log;
|
|
||||||
this._matrix = props.matrix;
|
|
||||||
props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => {
|
|
||||||
this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub;
|
|
||||||
});
|
|
||||||
// this._deviceConfigs = props.deviceConfigs;
|
|
||||||
|
|
||||||
this.connect(props.hubs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } {
|
|
||||||
// return this._devicesByHub;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public get hubs(): { [hubName: string]: HarmonyHub } {
|
|
||||||
return this._hubs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Power on all devices in an activity.
|
|
||||||
*/
|
|
||||||
public powerOn = async (controlUnitName: string, activity: IActivity) => {
|
|
||||||
//Only power on if not alread on
|
|
||||||
let currentActivity = this._states[controlUnitName]
|
|
||||||
? this._states[controlUnitName]!.currentActivity
|
|
||||||
: undefined;
|
|
||||||
if (!currentActivity) {
|
|
||||||
await this.startActivity(controlUnitName, activity);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Power off all devices in an activity that aren't being used.
|
|
||||||
*/
|
|
||||||
public powerOff = async (controlUnitName: string) => {
|
|
||||||
if (!this._states[controlUnitName]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//Build potential list of devices to turn off
|
|
||||||
let devicesToTurnOff: Array<HarmonyDevice> = this._states[
|
|
||||||
controlUnitName
|
|
||||||
]!.currentActivity.DeviceSetupList.map(
|
|
||||||
(value: IDeviceSetupItem): HarmonyDevice => {
|
|
||||||
return this.getDeviceFromName(value.DeviceName);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
//Resolve device conflicts with other controlUnits
|
|
||||||
devicesToTurnOff = this.sanitizeDeviceList(
|
|
||||||
devicesToTurnOff,
|
|
||||||
controlUnitName
|
|
||||||
);
|
|
||||||
|
|
||||||
//Turn off devices
|
|
||||||
devicesToTurnOff.forEach(async (device: HarmonyDevice) => {
|
|
||||||
if (device) {
|
|
||||||
await device.powerOff();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this._states[controlUnitName] = undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start an activity
|
|
||||||
*/
|
|
||||||
public startActivity = async (
|
|
||||||
controlUnitName: string,
|
|
||||||
activity: IActivity
|
|
||||||
) => {
|
|
||||||
this._log(
|
|
||||||
`Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}`
|
|
||||||
);
|
|
||||||
let lastActivity: IActivity | undefined = undefined;
|
|
||||||
if (this._states[controlUnitName]) {
|
|
||||||
lastActivity = this._states[controlUnitName]!.currentActivity;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Build potential list of devices to to turn on
|
|
||||||
let devicesToTurnOn: Array<HarmonyDevice> = activity.DeviceSetupList.map(
|
|
||||||
(value: IDeviceSetupItem): HarmonyDevice => {
|
|
||||||
return this.getDeviceFromName(value.DeviceName);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
//Resolve device conflicts with other controlUnits
|
|
||||||
devicesToTurnOn = this.sanitizeDeviceList(devicesToTurnOn, controlUnitName);
|
|
||||||
|
|
||||||
//Turn on devices
|
|
||||||
await Promise.all(
|
|
||||||
devicesToTurnOn.map(async (device: HarmonyDevice) => {
|
|
||||||
if (device && device.name) {
|
|
||||||
if (!device.on) {
|
|
||||||
this._log(`Turning on device ${device.name}`);
|
|
||||||
await device.powerOn();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
//Assign correct input
|
|
||||||
await Promise.all(
|
|
||||||
activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => {
|
|
||||||
let device: HarmonyDevice = this.getDeviceFromName(value.DeviceName);
|
|
||||||
|
|
||||||
if (device && device.supportsCommand(`Input${value.Input}`)) {
|
|
||||||
await device.sendCommand(`Input${value.Input}`);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
if (activity.UseMatrix) {
|
|
||||||
//get input and output
|
|
||||||
let input: IInput = this._matrix.Inputs.filter(
|
|
||||||
(e) => e.InputDevice === activity.ControlDevice
|
|
||||||
)[0];
|
|
||||||
let output: IOutput = this._matrix.Outputs.filter(
|
|
||||||
(e) => e.OutputDevice === activity.OutputDevice
|
|
||||||
)[0];
|
|
||||||
|
|
||||||
let inputCommandName: string = `In ${input.InputNumber}`;
|
|
||||||
let outputCommandName: string = `Out ${output.OutputLetter}`;
|
|
||||||
|
|
||||||
let matrixDevice: HarmonyDevice = this.getDeviceFromName(
|
|
||||||
this._matrix.DeviceName
|
|
||||||
);
|
|
||||||
|
|
||||||
//Route hdmi
|
|
||||||
if (
|
|
||||||
matrixDevice.supportsCommand(inputCommandName) &&
|
|
||||||
matrixDevice.supportsCommand(outputCommandName)
|
|
||||||
) {
|
|
||||||
await matrixDevice.sendCommand(outputCommandName);
|
|
||||||
await matrixDevice.sendCommand(inputCommandName);
|
|
||||||
await matrixDevice.sendCommand(outputCommandName);
|
|
||||||
await matrixDevice.sendCommand(inputCommandName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Build potential list of devices to turn off
|
|
||||||
if (lastActivity) {
|
|
||||||
let devicesToTurnOff: Array<HarmonyDevice> = lastActivity.DeviceSetupList.map(
|
|
||||||
(value: IDeviceSetupItem): HarmonyDevice => {
|
|
||||||
return this.getDeviceFromName(value.DeviceName);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
//remove devices that will be used for next activity from list
|
|
||||||
//delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls
|
|
||||||
devicesToTurnOff.forEach((device: HarmonyDevice, index: number) => {
|
|
||||||
if (
|
|
||||||
device &&
|
|
||||||
device.name &&
|
|
||||||
activity.DeviceSetupList.some((e) => {
|
|
||||||
return e && e.DeviceName === device.name;
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
delete devicesToTurnOff[index];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//Resolve device conflicts with other controlUnits
|
|
||||||
devicesToTurnOff = this.sanitizeDeviceList(
|
|
||||||
devicesToTurnOff,
|
|
||||||
controlUnitName
|
|
||||||
);
|
|
||||||
|
|
||||||
this._log(
|
|
||||||
`Sanatized devices to turn off: ${JSON.stringify(
|
|
||||||
devicesToTurnOff.map((e) => (e ? e.name : ""))
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
//Turn off devices
|
|
||||||
devicesToTurnOff.map(async (device: HarmonyDevice) => {
|
|
||||||
if (device) {
|
|
||||||
if (device.on) {
|
|
||||||
this._log(`Turning off device ${device.name}`);
|
|
||||||
await device.powerOff();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Assign current activity
|
|
||||||
this._states[controlUnitName] = { currentActivity: activity };
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Turn the volume up for the current running activity.
|
|
||||||
*/
|
|
||||||
public volumeUp = async (controlUnitName: string) => {
|
|
||||||
let volumeUpCommand: string = "Volume Up";
|
|
||||||
if (this._states[controlUnitName]) {
|
|
||||||
let volumeDevice: HarmonyDevice = this.getDeviceFromName(
|
|
||||||
this._states[controlUnitName]!.currentActivity.VolumeDevice
|
|
||||||
);
|
|
||||||
await volumeDevice.sendCommand(volumeUpCommand);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Volume down for current running activity.
|
|
||||||
*/
|
|
||||||
public volumeDown = async (controlUnitName: string) => {
|
|
||||||
let volumeDownCommand: string = "Volume Down";
|
|
||||||
if (this._states[controlUnitName]) {
|
|
||||||
let volumeDevice: HarmonyDevice = this.getDeviceFromName(
|
|
||||||
this._states[controlUnitName]!.currentActivity.VolumeDevice
|
|
||||||
);
|
|
||||||
await volumeDevice.sendCommand(volumeDownCommand);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send key press for current activity.
|
|
||||||
*
|
|
||||||
* @param controlUnitName The name of the control unit to act on.
|
|
||||||
* @param key The key to send.
|
|
||||||
*/
|
|
||||||
public sendKeyPress = async (controlUnitName: string, key: any) => {
|
|
||||||
if (this._states[controlUnitName]) {
|
|
||||||
let commandName: string = "";
|
|
||||||
|
|
||||||
let device: HarmonyDevice = this.getDeviceFromName(
|
|
||||||
this._states[controlUnitName]!.currentActivity.ControlDevice
|
|
||||||
);
|
|
||||||
switch (key) {
|
|
||||||
case RemoteKey.ARROW_UP: {
|
|
||||||
commandName = "Direction Up";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.ARROW_DOWN: {
|
|
||||||
commandName = "Direction Down";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.ARROW_LEFT: {
|
|
||||||
commandName = "Direction Left";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.ARROW_RIGHT: {
|
|
||||||
commandName = "Direction Right";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.SELECT: {
|
|
||||||
commandName = "Select";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.PLAY_PAUSE: {
|
|
||||||
commandName = "Pause";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.INFORMATION: {
|
|
||||||
commandName = "Menu";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.BACK: {
|
|
||||||
commandName = "Back";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RemoteKey.EXIT: {
|
|
||||||
commandName = "Back";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await device.sendCommand(commandName);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return if a control unit is active
|
|
||||||
* @param controlUnitName
|
|
||||||
*/
|
|
||||||
public getIsActive(controlUnitName: string): IActivity | undefined {
|
|
||||||
return this._states[controlUnitName]
|
|
||||||
? this._states[controlUnitName]!.currentActivity
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the IDevice by name.
|
|
||||||
* @param deviceName The device to retrieve.
|
|
||||||
*/
|
|
||||||
public getDeviceFromName(deviceName: string): HarmonyDevice {
|
|
||||||
let device: HarmonyDevice | undefined;
|
|
||||||
try {
|
|
||||||
device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(
|
|
||||||
deviceName
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
this._log(`Error retrieving device from hub: ${err}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return device!;
|
|
||||||
}
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Gets device button commands
|
|
||||||
// * @param deviceCommandName The device command name
|
|
||||||
// * @param deviceName The device name
|
|
||||||
// */
|
|
||||||
// public getCommand(deviceCommandName: string, deviceName: string): ICommand | undefined {
|
|
||||||
// const device: HarmonyDevice = this.getDeviceFromName(deviceName);
|
|
||||||
// if (device && device.supportsCommand(deviceCommandName)) {
|
|
||||||
// return device.getCommand(deviceCommandName);
|
|
||||||
// } else {
|
|
||||||
// return undefined;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private connect = async (hubs: Array<IHub>) => {
|
|
||||||
let readyCount = 0;
|
|
||||||
await Promise.all(
|
|
||||||
hubs.map(
|
|
||||||
async (hub: IHub): Promise<void> => {
|
|
||||||
const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log);
|
|
||||||
this._hubs[hub.Name] = newHarmonyHub;
|
|
||||||
newHarmonyHub.on("Ready", () => {
|
|
||||||
readyCount++;
|
|
||||||
if (readyCount === Object.keys(this._hubs).length) {
|
|
||||||
this.emit("Ready");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await newHarmonyHub.initialize();
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to make sure no control unit depends on device list.
|
|
||||||
* @param devicesToTurnOn The list of devices to modify.
|
|
||||||
* @param controlUnitName The name of the control unit in question.
|
|
||||||
*/
|
|
||||||
private sanitizeDeviceList(
|
|
||||||
devicesToTurnOn: Array<HarmonyDevice>,
|
|
||||||
controlUnitName: string
|
|
||||||
): Array<HarmonyDevice> {
|
|
||||||
for (let controlUnitKey in this._states) {
|
|
||||||
//Skip self
|
|
||||||
if (controlUnitKey === controlUnitName) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let currentOtherState: IActivityState = this._states[controlUnitKey]!;
|
|
||||||
|
|
||||||
if (currentOtherState) {
|
|
||||||
currentOtherState.currentActivity.DeviceSetupList.forEach(
|
|
||||||
(value: IDeviceSetupItem) => {
|
|
||||||
//there are devices to remove
|
|
||||||
if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) {
|
|
||||||
let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter(
|
|
||||||
(i) => i.name === value.DeviceName
|
|
||||||
)[0];
|
|
||||||
delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return devicesToTurnOn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default HarmonyDataProvider;
|
|
@ -1,11 +0,0 @@
|
|||||||
import { IDeviceSetupItem } from './IDeviceSetupItem';
|
|
||||||
|
|
||||||
export interface IActivity {
|
|
||||||
OutputDevice: string;
|
|
||||||
VolumeDevice: string;
|
|
||||||
ControlDevice: string;
|
|
||||||
DisplayName: string;
|
|
||||||
DeviceSetupList: Array<IDeviceSetupItem>;
|
|
||||||
UseMatrix: boolean;
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
import { IMatrix } from "./IMatrix";
|
|
||||||
import { IActivity } from "./IActivity";
|
|
||||||
import { IDeviceButton } from "./IDeviceButton";
|
|
||||||
import { IDeviceConfig } from "./IDeviceConfig";
|
|
||||||
import { IHub } from './IHub';
|
|
||||||
|
|
||||||
export interface IControlUnit {
|
|
||||||
DisplayName: string;
|
|
||||||
Activities: Array<IActivity>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IConfig {
|
|
||||||
hubIp: string;
|
|
||||||
EmitDevicesOnStartup: boolean;
|
|
||||||
Matrix: IMatrix;
|
|
||||||
ControlUnits: Array<IControlUnit>;
|
|
||||||
DeviceButtons: Array<IDeviceButton>;
|
|
||||||
Devices: Array<IDeviceConfig>;
|
|
||||||
Hubs: Array<IHub>;
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
export interface IDeviceConfig {
|
|
||||||
Name: string;
|
|
||||||
Hub: string;
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
export * from './IActivity';
|
|
||||||
export * from './IConfig';
|
|
||||||
export * from './IDeviceButton';
|
|
||||||
export * from './IDeviceSetupItem';
|
|
||||||
export * from './IMatrix';
|
|
@ -1,94 +0,0 @@
|
|||||||
import { ICommand } from "./IDevice";
|
|
||||||
import { sleep } from "../Util/Sleep";
|
|
||||||
|
|
||||||
export interface IHarmonyDeviceProps {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
harmony: any;
|
|
||||||
log: any;
|
|
||||||
commands: { [name: string]: ICommand };
|
|
||||||
}
|
|
||||||
|
|
||||||
export class HarmonyDevice {
|
|
||||||
private _harmony: any;
|
|
||||||
private _log: any;
|
|
||||||
private _commands: { [name: string]: ICommand } = {};
|
|
||||||
private _on: boolean;
|
|
||||||
|
|
||||||
constructor(props: IHarmonyDeviceProps) {
|
|
||||||
this.id = props.id;
|
|
||||||
this.name = props.name;
|
|
||||||
this._harmony = props.harmony;
|
|
||||||
this._on = false;
|
|
||||||
this._commands = props.commands;
|
|
||||||
}
|
|
||||||
|
|
||||||
public id: string;
|
|
||||||
public name: string;
|
|
||||||
|
|
||||||
public get on(): boolean {
|
|
||||||
return this._on;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get commands(): { [name: string]: ICommand } {
|
|
||||||
return this._commands;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Define device methods
|
|
||||||
public supportsCommand(commandName: string): boolean {
|
|
||||||
let command = this._commands[commandName];
|
|
||||||
return (command) ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCommand(commandName: string): ICommand {
|
|
||||||
return this._commands[commandName];
|
|
||||||
}
|
|
||||||
|
|
||||||
public async powerOn(): Promise<void> {
|
|
||||||
let powerOnCommand: string = "Power On";
|
|
||||||
let powerToggleCommand: string = "Power Toggle";
|
|
||||||
if (this.supportsCommand(powerOnCommand)) {
|
|
||||||
await this.sendCommand(powerOnCommand);
|
|
||||||
this._on = true;
|
|
||||||
} else if (this.supportsCommand(powerToggleCommand)) {
|
|
||||||
await this.sendCommand(powerToggleCommand);
|
|
||||||
this._on = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async powerOff(): Promise<void> {
|
|
||||||
let powerOffCommand: string = "Power Off";
|
|
||||||
let powerToggleCommand: string = "Power Toggle";
|
|
||||||
if (this.supportsCommand(powerOffCommand)) {
|
|
||||||
await this.sendCommand(powerOffCommand);
|
|
||||||
this._on = false;
|
|
||||||
} else if (this.supportsCommand(powerToggleCommand)) {
|
|
||||||
await this.sendCommand(powerToggleCommand);
|
|
||||||
this._on = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendCommand(commandName: string): Promise<void> {
|
|
||||||
let command!: ICommand;
|
|
||||||
if (this.supportsCommand(commandName)) {
|
|
||||||
command = this.getCommand(commandName);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
//Execute command
|
|
||||||
//HACK to fix Harmon Kardon receiver not turning off
|
|
||||||
if (command.command === "PowerOff") {
|
|
||||||
for (let i = 0; i < 2; i++) {
|
|
||||||
await this._harmony.sendCommand(JSON.stringify(command));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
await this._harmony.sendCommand(JSON.stringify(command));
|
|
||||||
|
|
||||||
//Sleep
|
|
||||||
await sleep(800);
|
|
||||||
} catch (err) {
|
|
||||||
this._log(`ERROR - error sending command to harmony: ${err}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
import { HarmonyDevice } from './HarmonyDevice';
|
|
||||||
const Harmony = require("harmony-websocket");
|
|
||||||
import { ICommand } from './IDevice';
|
|
||||||
import { EventEmitter } from 'events';
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class HarmonyHub extends EventEmitter {
|
|
||||||
private _devices: { [deviceName: string]: HarmonyDevice } = {}
|
|
||||||
private _ip: string;
|
|
||||||
private _harmony: any;
|
|
||||||
private _log: any;
|
|
||||||
private _name: string;
|
|
||||||
|
|
||||||
constructor(hubName: string, ipAddress: string, log: any) {
|
|
||||||
super();
|
|
||||||
this._ip = ipAddress;
|
|
||||||
this._log = log;
|
|
||||||
this._name = hubName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get devices(): { [deviceName: string]: HarmonyDevice } {
|
|
||||||
return this._devices;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get hubName(): string {
|
|
||||||
return this._name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDeviceByName = (deviceName: string): HarmonyDevice => {
|
|
||||||
return this._devices[deviceName];
|
|
||||||
}
|
|
||||||
|
|
||||||
public initialize = async () => {
|
|
||||||
this._harmony = new Harmony();
|
|
||||||
await this._harmony.connect(this._ip);
|
|
||||||
this._harmony.on('stateDigest', (data: any) => {
|
|
||||||
console.log(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
this._harmony.on('automationState', (data: any) => {
|
|
||||||
console.log(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
//Gather devices
|
|
||||||
let devices: any = await this._harmony.getDevices();
|
|
||||||
try {
|
|
||||||
await Promise.all(
|
|
||||||
//Add each to dictionary
|
|
||||||
devices.map(async (dev: any) => {
|
|
||||||
//get commands
|
|
||||||
let commands: { [name: string]: ICommand } = {};
|
|
||||||
let deviceCommands: any = await this._harmony.getDeviceCommands(dev.id);
|
|
||||||
deviceCommands.forEach((command: any) => {
|
|
||||||
commands[command.label] = command.action;
|
|
||||||
});
|
|
||||||
this._devices[dev.label] = new HarmonyDevice({
|
|
||||||
id: dev.id,
|
|
||||||
name: dev.label,
|
|
||||||
commands: commands,
|
|
||||||
log: this._log,
|
|
||||||
harmony: this._harmony
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
this.emit("Ready");
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
this._log(`ERROR - error connecting to harmony: ${err}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private connect = async (): Promise<void> => {
|
|
||||||
await this._harmony.Connect(this._ip);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
export * from './IDevice';
|
|
@ -1,2 +0,0 @@
|
|||||||
export * from "./Callbackify";
|
|
||||||
export * from "./Sleep";
|
|
@ -1,8 +1,10 @@
|
|||||||
import { PlatformAccessory, RemoteController, Service } from "homebridge";
|
import { PlatformAccessory, Service } from "homebridge";
|
||||||
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider";
|
import { IActivity } from "../models/config";
|
||||||
import { IActivity } from "../Models/Config";
|
|
||||||
import { Platform } from "../platform";
|
import { Platform } from "../platform";
|
||||||
import callbackify from "../Util/Callbackify";
|
import { ActivityService } from "../services/activityService";
|
||||||
|
import { CommandService } from "../services/commandService";
|
||||||
|
import { VolumeService } from "../services/volumeService";
|
||||||
|
import callbackify from "../util/callbackify";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum describing remote key presses from homebridge.
|
* Enum describing remote key presses from homebridge.
|
||||||
@ -27,7 +29,9 @@ export class ControlUnit {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly _platform: Platform,
|
private readonly _platform: Platform,
|
||||||
private readonly _accessory: PlatformAccessory,
|
private readonly _accessory: PlatformAccessory,
|
||||||
private _dataProvider: HarmonyDataProvider,
|
private _activityService: ActivityService,
|
||||||
|
private _commandService: CommandService,
|
||||||
|
private _volumeService: VolumeService,
|
||||||
private _activities: Array<IActivity>
|
private _activities: Array<IActivity>
|
||||||
) {
|
) {
|
||||||
this._accessory
|
this._accessory
|
||||||
@ -129,11 +133,11 @@ export class ControlUnit {
|
|||||||
private onSetAccessoryActive = async (value: any) => {
|
private onSetAccessoryActive = async (value: any) => {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 0:
|
case 0:
|
||||||
this._dataProvider.powerOff(this._accessory.displayName);
|
this._activityService.stopCurrentActivity(this._accessory.displayName);
|
||||||
break;
|
break;
|
||||||
//Turn on with first activity
|
//Turn on with first activity
|
||||||
case 1:
|
case 1:
|
||||||
this._dataProvider.powerOn(
|
this._activityService.startActivity(
|
||||||
this._accessory.displayName,
|
this._accessory.displayName,
|
||||||
this._activities[0]
|
this._activities[0]
|
||||||
);
|
);
|
||||||
@ -146,7 +150,7 @@ export class ControlUnit {
|
|||||||
*/
|
*/
|
||||||
private onGetAccessoryActive = async () => {
|
private onGetAccessoryActive = async () => {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
return this._dataProvider.getIsActive(this._accessory.displayName)
|
return this._activityService.getIsActive(this._accessory.displayName)
|
||||||
? this._platform.Characteristic.Active.ACTIVE
|
? this._platform.Characteristic.Active.ACTIVE
|
||||||
: this._platform.Characteristic.Active.INACTIVE;
|
: this._platform.Characteristic.Active.INACTIVE;
|
||||||
};
|
};
|
||||||
@ -155,14 +159,14 @@ export class ControlUnit {
|
|||||||
* Event handler for SET remote key
|
* Event handler for SET remote key
|
||||||
*/
|
*/
|
||||||
private onSetRemoteKey = async (key: any) => {
|
private onSetRemoteKey = async (key: any) => {
|
||||||
this._dataProvider.sendKeyPress(this._accessory.displayName, key);
|
this._commandService.sendKeyPress(this._accessory.displayName, key);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event handler for SET active identifier characteristic
|
* Event handler for SET active identifier characteristic
|
||||||
*/
|
*/
|
||||||
private onSetActiveIdentifier = async (identifier: any) => {
|
private onSetActiveIdentifier = async (identifier: any) => {
|
||||||
this._dataProvider.startActivity(
|
this._activityService.startActivity(
|
||||||
this._accessory.displayName,
|
this._accessory.displayName,
|
||||||
this._activities[identifier]
|
this._activities[identifier]
|
||||||
);
|
);
|
||||||
@ -172,7 +176,7 @@ export class ControlUnit {
|
|||||||
* Event handler for GET active identifier characteristic
|
* Event handler for GET active identifier characteristic
|
||||||
*/
|
*/
|
||||||
private onGetActiveIdentifier = async () => {
|
private onGetActiveIdentifier = async () => {
|
||||||
let currentActivity: IActivity = this._dataProvider.getIsActive(
|
let currentActivity: IActivity = this._activityService.getIsActive(
|
||||||
this._accessory.displayName
|
this._accessory.displayName
|
||||||
)!;
|
)!;
|
||||||
let identifier: number = 0;
|
let identifier: number = 0;
|
||||||
@ -230,10 +234,10 @@ export class ControlUnit {
|
|||||||
private onSetVolumeSelector = async (value: any) => {
|
private onSetVolumeSelector = async (value: any) => {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 0:
|
case 0:
|
||||||
this._dataProvider.volumeUp(this._accessory.displayName);
|
this._volumeService.volumeUp(this._accessory.displayName);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
this._dataProvider.volumeDown(this._accessory.displayName);
|
this._volumeService.volumeDown(this._accessory.displayName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -1,7 +1,7 @@
|
|||||||
import { PlatformAccessory, Service } from "homebridge";
|
import { PlatformAccessory, Service } from "homebridge";
|
||||||
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider";
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
||||||
import { IDeviceButton } from "../Models/Config";
|
import { IDeviceButton } from "../models/config";
|
||||||
import { HarmonyDevice } from "../Models/HarmonyDevice";
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
import { Platform } from "../platform";
|
import { Platform } from "../platform";
|
||||||
|
|
||||||
export class DeviceButton {
|
export class DeviceButton {
|
||||||
@ -69,29 +69,40 @@ export class DeviceButton {
|
|||||||
|
|
||||||
//Get device command if we don't have it
|
//Get device command if we don't have it
|
||||||
if (!this._device) {
|
if (!this._device) {
|
||||||
this._device = this._dataProvider.getDeviceFromName(
|
this._device = this._dataProvider.getDeviceByName(
|
||||||
this._deviceInfo.DeviceName
|
this._deviceInfo.DeviceName
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Execute command
|
//Execute command
|
||||||
if (this._device) {
|
if (!this._device) {
|
||||||
//change state if stateful
|
return callback();
|
||||||
if (this._deviceInfo.IsStateful && this._buttonState != newState) {
|
}
|
||||||
this._buttonState = newState;
|
|
||||||
await this._device.sendCommand(this._deviceInfo.ButtonName);
|
//change state if stateful
|
||||||
} else if (!this._deviceInfo.IsStateful) {
|
if (this._deviceInfo.IsStateful && this._buttonState != newState) {
|
||||||
//Send the number of configured key presses
|
this._buttonState = newState;
|
||||||
for (let i = 0; i < this._deviceInfo.NumberOfKeyPresses; i++) {
|
await this._dataProvider.sendCommand(
|
||||||
await this._device.sendCommand(this._deviceInfo.ButtonName);
|
this._deviceInfo.ButtonName,
|
||||||
}
|
this._device
|
||||||
this._switchService
|
);
|
||||||
.getCharacteristic(this._platform.Characteristic.On)
|
return callback();
|
||||||
.updateValue(false);
|
} else if (!this._deviceInfo.IsStateful) {
|
||||||
return callback(new Error("Normal Response"));
|
//Send the number of configured key presses
|
||||||
}
|
for (let i = 0; i < this._deviceInfo.NumberOfKeyPresses; i++) {
|
||||||
|
await this._dataProvider.sendCommand(
|
||||||
|
this._deviceInfo.ButtonName,
|
||||||
|
this._device
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._switchService
|
||||||
|
.getCharacteristic(this._platform.Characteristic.On)
|
||||||
|
.updateValue(false);
|
||||||
|
|
||||||
|
this._buttonState = false;
|
||||||
|
return callback();
|
||||||
}
|
}
|
||||||
return callback();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
2
src/accessories/index.ts
Normal file
2
src/accessories/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export { ControlUnit } from "./controlUnit";
|
||||||
|
export { DeviceButton } from "./deviceButton";
|
105
src/accessories/sequence.ts
Normal file
105
src/accessories/sequence.ts
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
import {
|
||||||
|
CharacteristicGetCallback,
|
||||||
|
CharacteristicSetCallback,
|
||||||
|
CharacteristicValue,
|
||||||
|
PlatformAccessory,
|
||||||
|
Service,
|
||||||
|
} from "homebridge";
|
||||||
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
||||||
|
import { ISequence } from "../models/config/sequence";
|
||||||
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
|
import { Platform } from "../platform";
|
||||||
|
import { sleep } from "../util";
|
||||||
|
|
||||||
|
export class Sequence {
|
||||||
|
private _devices: { [deviceName: string]: HarmonyDevice };
|
||||||
|
private _switchService: Service;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly _platform: Platform,
|
||||||
|
private readonly _accessory: PlatformAccessory,
|
||||||
|
private _dataProvider: HarmonyDataProvider,
|
||||||
|
private _sequence: ISequence
|
||||||
|
) {
|
||||||
|
this._accessory
|
||||||
|
.getService(this._platform.Service.AccessoryInformation)!
|
||||||
|
.setCharacteristic(
|
||||||
|
this._platform.Characteristic.Manufacturer,
|
||||||
|
"Brandon Watson"
|
||||||
|
)
|
||||||
|
.setCharacteristic(this._platform.Characteristic.Model, "Sequence Button")
|
||||||
|
.setCharacteristic(
|
||||||
|
this._platform.Characteristic.SerialNumber,
|
||||||
|
"123-456-789"
|
||||||
|
);
|
||||||
|
|
||||||
|
const switchUUID = this._platform.api.hap.uuid.generate(
|
||||||
|
`${this._accessory.displayName} Switch`
|
||||||
|
);
|
||||||
|
|
||||||
|
this._switchService =
|
||||||
|
this._accessory.getService(this._platform.Service.Switch) ||
|
||||||
|
this._accessory.addService(
|
||||||
|
this._platform.Service.Switch,
|
||||||
|
this._accessory.displayName,
|
||||||
|
switchUUID
|
||||||
|
);
|
||||||
|
|
||||||
|
this._switchService
|
||||||
|
.getCharacteristic(this._platform.Characteristic.On)
|
||||||
|
.on("set", this.onSwitchSet)
|
||||||
|
.updateValue(false)
|
||||||
|
.on("get", (callback: CharacteristicGetCallback): void => {
|
||||||
|
return callback(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
this._devices = {};
|
||||||
|
// Get devices in sequence
|
||||||
|
for (const deviceName of _sequence.Steps.map((e) => e.DeviceName)) {
|
||||||
|
if (!deviceName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const device = this._dataProvider.getDeviceByName(deviceName);
|
||||||
|
if (device) {
|
||||||
|
this._devices[deviceName] = device;
|
||||||
|
} else {
|
||||||
|
this._platform.log.warn(
|
||||||
|
`Device ${deviceName} was not found in harmony configuration`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for switchSet command
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
public onSwitchSet = async (
|
||||||
|
_value: CharacteristicValue,
|
||||||
|
callback: CharacteristicSetCallback
|
||||||
|
): Promise<void> => {
|
||||||
|
// Execute sequence
|
||||||
|
for (const step of this._sequence.Steps) {
|
||||||
|
await sleep(step.Delay);
|
||||||
|
const device: HarmonyDevice = this._devices[step.DeviceName ?? ""];
|
||||||
|
if (
|
||||||
|
device &&
|
||||||
|
step.DeviceCommand &&
|
||||||
|
device.supportsCommand(step.DeviceCommand)
|
||||||
|
) {
|
||||||
|
await this._dataProvider.sendCommand(step.DeviceCommand, device);
|
||||||
|
} else {
|
||||||
|
this._platform.log.warn(
|
||||||
|
`Attempted to execute command ${step.DeviceCommand} on device ${step.DeviceName} but the device or command was not found`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deactivate button
|
||||||
|
this._switchService
|
||||||
|
.getCharacteristic(this._platform.Characteristic.On)
|
||||||
|
.updateValue(false);
|
||||||
|
|
||||||
|
callback(null);
|
||||||
|
};
|
||||||
|
}
|
166
src/dataProviders/harmonyDataProvider.ts
Normal file
166
src/dataProviders/harmonyDataProvider.ts
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
import { Logging } from "homebridge";
|
||||||
|
import { inject, injectable } from "tsyringe";
|
||||||
|
import { ICommand } from "../models";
|
||||||
|
import { IConfig } from "../models/config";
|
||||||
|
import { IDeviceConfig } from "../models/config/deviceConfig";
|
||||||
|
import { IHub } from "../models/config/hub";
|
||||||
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
|
import { HarmonyHub } from "../models/harmonyHub";
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class HarmonyDataProvider {
|
||||||
|
private _hubs: { [hubName: string]: HarmonyHub } = {};
|
||||||
|
private _deviceConfigByName: { [deviceName: string]: IDeviceConfig } = {};
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
@inject("IConfig") private _config: IConfig,
|
||||||
|
@inject("log") private _log: Logging
|
||||||
|
) {
|
||||||
|
_config.Devices.forEach((deviceConfig: IDeviceConfig) => {
|
||||||
|
this._deviceConfigByName[deviceConfig.Name] = deviceConfig;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.connect(_config.Hubs);
|
||||||
|
this.emitInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async powerOnDevices(devices: Array<HarmonyDevice>): Promise<void> {
|
||||||
|
await Promise.all(
|
||||||
|
devices.map(async (device: HarmonyDevice) => {
|
||||||
|
if (device && device.name) {
|
||||||
|
if (!device.on) {
|
||||||
|
this._log.info(`Turning on device ${device.name}`);
|
||||||
|
await this.powerOnDevice(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async powerOffDevices(devices: Array<HarmonyDevice>) {
|
||||||
|
await Promise.all(
|
||||||
|
//Turn off devices
|
||||||
|
devices.map(async (device: HarmonyDevice) => {
|
||||||
|
if (device) {
|
||||||
|
if (device.on) {
|
||||||
|
this._log.info(`Turning off device ${device.name}`);
|
||||||
|
await this.powerOffDevice(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async sendCommand(
|
||||||
|
commandName: string,
|
||||||
|
harmonyDevice: HarmonyDevice
|
||||||
|
): Promise<void> {
|
||||||
|
let command!: ICommand;
|
||||||
|
|
||||||
|
commandName = this.getOverrideCommand(commandName, harmonyDevice);
|
||||||
|
|
||||||
|
if (harmonyDevice.supportsCommand(commandName)) {
|
||||||
|
command = harmonyDevice.getCommand(commandName);
|
||||||
|
}
|
||||||
|
const hub = this.getHubByDevice(harmonyDevice);
|
||||||
|
await hub.sendCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the IDevice by name.
|
||||||
|
* @param deviceName The device to retrieve.
|
||||||
|
*/
|
||||||
|
public getDeviceByName(deviceName: string): HarmonyDevice {
|
||||||
|
let device: HarmonyDevice | undefined;
|
||||||
|
try {
|
||||||
|
device =
|
||||||
|
this._hubs[this._deviceConfigByName[deviceName].Hub].getDeviceByName(
|
||||||
|
deviceName
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
this._log.info(`Error retrieving device from hub: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return device!;
|
||||||
|
}
|
||||||
|
|
||||||
|
private connect = async (hubs: Array<IHub>) => {
|
||||||
|
let readyCount = 0;
|
||||||
|
await Promise.all(
|
||||||
|
hubs.map(async (hub: IHub): Promise<void> => {
|
||||||
|
const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log.info);
|
||||||
|
this._hubs[hub.Name] = newHarmonyHub;
|
||||||
|
newHarmonyHub.on("Ready", () => {
|
||||||
|
readyCount++;
|
||||||
|
if (readyCount === Object.keys(this._hubs).length) {
|
||||||
|
// this.emit("Ready");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await newHarmonyHub.initialize();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
private emitInfo(): void {
|
||||||
|
//Emit devices if requested
|
||||||
|
this._log.info("All hubs connected");
|
||||||
|
if (this._config.EmitDevicesOnStartup) {
|
||||||
|
const hubs = this._hubs;
|
||||||
|
Object.values(hubs).forEach((hub: HarmonyHub) => {
|
||||||
|
const deviceDictionary = hub.devices;
|
||||||
|
this._log.info(`${hub.hubName}`);
|
||||||
|
Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
|
||||||
|
this._log.info(` ${device.name} : ${device.id}`);
|
||||||
|
Object.keys(device.commands).forEach((command: string) => {
|
||||||
|
this._log.info(` ${command}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHubByDevice(device: HarmonyDevice) {
|
||||||
|
return this._hubs[this._deviceConfigByName[device.name].Hub];
|
||||||
|
}
|
||||||
|
|
||||||
|
private async powerOnDevice(harmonyDevice: HarmonyDevice): Promise<void> {
|
||||||
|
let powerOnCommand: string = "Power On";
|
||||||
|
let powerToggleCommand: string = "Power Toggle";
|
||||||
|
if (harmonyDevice.supportsCommand(powerOnCommand)) {
|
||||||
|
await this.sendCommand(powerOnCommand, harmonyDevice);
|
||||||
|
harmonyDevice.on = true;
|
||||||
|
} else if (harmonyDevice.supportsCommand(powerToggleCommand)) {
|
||||||
|
await this.sendCommand(powerToggleCommand, harmonyDevice);
|
||||||
|
harmonyDevice.on = true;
|
||||||
|
} else {
|
||||||
|
await this.sendCommand(powerOnCommand, harmonyDevice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async powerOffDevice(harmonyDevice: HarmonyDevice): Promise<void> {
|
||||||
|
let powerOffCommand: string = "Power Off";
|
||||||
|
let powerToggleCommand: string = "Power Toggle";
|
||||||
|
if (harmonyDevice.supportsCommand(powerOffCommand)) {
|
||||||
|
await this.sendCommand(powerOffCommand, harmonyDevice);
|
||||||
|
harmonyDevice.on = false;
|
||||||
|
} else if (harmonyDevice.supportsCommand(powerToggleCommand)) {
|
||||||
|
await this.sendCommand(powerToggleCommand, harmonyDevice);
|
||||||
|
harmonyDevice.on = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getOverrideCommand(
|
||||||
|
commandName: string,
|
||||||
|
harmonyDevice: HarmonyDevice
|
||||||
|
) {
|
||||||
|
const deviceConfig: IDeviceConfig =
|
||||||
|
this._deviceConfigByName[harmonyDevice.name];
|
||||||
|
if (!deviceConfig.Overrides) {
|
||||||
|
return commandName;
|
||||||
|
}
|
||||||
|
const overrideCommand = deviceConfig.Overrides.find(
|
||||||
|
(e) => e.Command == commandName
|
||||||
|
);
|
||||||
|
return overrideCommand ? overrideCommand.Override : commandName;
|
||||||
|
}
|
||||||
|
}
|
31
src/dataProviders/stateDataProvider.ts
Normal file
31
src/dataProviders/stateDataProvider.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { injectable } from "tsyringe";
|
||||||
|
import { IActivityState } from "../models/activityState";
|
||||||
|
import { IActivity } from "../models/config";
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class StateDataProvider {
|
||||||
|
private _states: {
|
||||||
|
[controlUnitName: string]: IActivityState | undefined;
|
||||||
|
} = {};
|
||||||
|
|
||||||
|
public updateState(activity: IActivity, controlUnitName: string): void {
|
||||||
|
this._states[controlUnitName] = { currentActivity: activity };
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeState(controlUnitName: string): void {
|
||||||
|
this._states[controlUnitName] = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getState(controlUnitName: string) {
|
||||||
|
if (!this._states[controlUnitName]) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return this._states[controlUnitName]!.currentActivity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get states(): {
|
||||||
|
[controlUnitName: string]: IActivityState | undefined;
|
||||||
|
} {
|
||||||
|
return this._states;
|
||||||
|
}
|
||||||
|
}
|
5
src/models/activityState.ts
Normal file
5
src/models/activityState.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { IActivity } from "./config";
|
||||||
|
|
||||||
|
export interface IActivityState {
|
||||||
|
currentActivity: IActivity;
|
||||||
|
}
|
10
src/models/config/activity.ts
Normal file
10
src/models/config/activity.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { IDeviceSetupItem } from "./deviceSetupItem";
|
||||||
|
|
||||||
|
export interface IActivity {
|
||||||
|
OutputDevice: string;
|
||||||
|
VolumeDevice: string;
|
||||||
|
ControlDevice: string;
|
||||||
|
DisplayName: string;
|
||||||
|
DeviceSetupList: Array<IDeviceSetupItem>;
|
||||||
|
UseMatrix: boolean;
|
||||||
|
}
|
22
src/models/config/config.ts
Normal file
22
src/models/config/config.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { IMatrix } from "./matrix";
|
||||||
|
import { IActivity } from "./activity";
|
||||||
|
import { IDeviceButton } from "./deviceButton";
|
||||||
|
import { IDeviceConfig } from "./deviceConfig";
|
||||||
|
import { IHub } from "./hub";
|
||||||
|
import { ISequence } from "./sequence";
|
||||||
|
|
||||||
|
export interface IControlUnit {
|
||||||
|
DisplayName: string;
|
||||||
|
Activities: Array<IActivity>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IConfig {
|
||||||
|
hubIp: string;
|
||||||
|
EmitDevicesOnStartup: boolean;
|
||||||
|
Matrix: IMatrix;
|
||||||
|
ControlUnits: Array<IControlUnit>;
|
||||||
|
DeviceButtons: Array<IDeviceButton>;
|
||||||
|
Sequences: Array<ISequence>;
|
||||||
|
Devices: Array<IDeviceConfig>;
|
||||||
|
Hubs: Array<IHub>;
|
||||||
|
}
|
5
src/models/config/deviceConfig.ts
Normal file
5
src/models/config/deviceConfig.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface IDeviceConfig {
|
||||||
|
Name: string;
|
||||||
|
Hub: string;
|
||||||
|
Overrides: Array<{ Command: string; Override: string }>;
|
||||||
|
}
|
5
src/models/config/index.ts
Normal file
5
src/models/config/index.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export * from "./activity";
|
||||||
|
export * from "./config";
|
||||||
|
export * from "./deviceButton";
|
||||||
|
export * from "./deviceSetupItem";
|
||||||
|
export * from "./matrix";
|
10
src/models/config/sequence.ts
Normal file
10
src/models/config/sequence.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export interface ISequence {
|
||||||
|
DisplayName: string;
|
||||||
|
Steps: Array<IStep>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IStep {
|
||||||
|
DeviceName?: string;
|
||||||
|
DeviceCommand?: string;
|
||||||
|
Delay: number;
|
||||||
|
}
|
47
src/models/harmonyDevice.ts
Normal file
47
src/models/harmonyDevice.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { ICommand } from "./device";
|
||||||
|
import { sleep } from "../util/sleep";
|
||||||
|
|
||||||
|
export interface IHarmonyDeviceProps {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
harmony: any;
|
||||||
|
log: any;
|
||||||
|
commands: { [name: string]: ICommand };
|
||||||
|
}
|
||||||
|
|
||||||
|
export class HarmonyDevice {
|
||||||
|
private _commands: { [name: string]: ICommand } = {};
|
||||||
|
private _on: boolean;
|
||||||
|
|
||||||
|
constructor(props: IHarmonyDeviceProps) {
|
||||||
|
this.id = props.id;
|
||||||
|
this.name = props.name;
|
||||||
|
this._on = false;
|
||||||
|
this._commands = props.commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public id: string;
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
public get on(): boolean {
|
||||||
|
return this._on;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set on(value: boolean) {
|
||||||
|
this._on = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get commands(): { [name: string]: ICommand } {
|
||||||
|
return this._commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Define device methods
|
||||||
|
public supportsCommand(commandName: string): boolean {
|
||||||
|
let command = this._commands[commandName];
|
||||||
|
return command ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCommand(commandName: string): ICommand {
|
||||||
|
return this._commands[commandName];
|
||||||
|
}
|
||||||
|
}
|
94
src/models/harmonyHub.ts
Normal file
94
src/models/harmonyHub.ts
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import { HarmonyDevice } from "./harmonyDevice";
|
||||||
|
const Harmony = require("harmony-websocket");
|
||||||
|
import { ICommand } from "./device";
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { sleep } from "../util";
|
||||||
|
|
||||||
|
export class HarmonyHub extends EventEmitter {
|
||||||
|
private _devices: { [deviceName: string]: HarmonyDevice } = {};
|
||||||
|
private _ip: string;
|
||||||
|
private _harmony: any;
|
||||||
|
private _log: any;
|
||||||
|
private _name: string;
|
||||||
|
|
||||||
|
constructor(hubName: string, ipAddress: string, log: any) {
|
||||||
|
super();
|
||||||
|
this._ip = ipAddress;
|
||||||
|
this._log = log;
|
||||||
|
this._name = hubName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get devices(): { [deviceName: string]: HarmonyDevice } {
|
||||||
|
return this._devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get hubName(): string {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDeviceByName = (deviceName: string): HarmonyDevice => {
|
||||||
|
return this._devices[deviceName];
|
||||||
|
};
|
||||||
|
|
||||||
|
public initialize = async () => {
|
||||||
|
this._harmony = new Harmony();
|
||||||
|
await this._harmony.connect(this._ip);
|
||||||
|
this._harmony.on("stateDigest", (data: any) => {
|
||||||
|
console.log(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
this._harmony.on("automationState", (data: any) => {
|
||||||
|
console.log(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
//Gather devices
|
||||||
|
let devices: any = await this._harmony.getDevices();
|
||||||
|
try {
|
||||||
|
await Promise.all(
|
||||||
|
//Add each to dictionary
|
||||||
|
devices.map(async (dev: any) => {
|
||||||
|
//get commands
|
||||||
|
let commands: { [name: string]: ICommand } = {};
|
||||||
|
let deviceCommands: any = await this._harmony.getDeviceCommands(
|
||||||
|
dev.id
|
||||||
|
);
|
||||||
|
deviceCommands.forEach((command: any) => {
|
||||||
|
commands[command.label] = command.action;
|
||||||
|
});
|
||||||
|
this._devices[dev.label] = new HarmonyDevice({
|
||||||
|
id: dev.id,
|
||||||
|
name: dev.label,
|
||||||
|
commands: commands,
|
||||||
|
log: this._log,
|
||||||
|
harmony: this._harmony,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
this.emit("Ready");
|
||||||
|
} catch (err) {
|
||||||
|
this._log(`ERROR - error connecting to harmony: ${err}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private connect = async (): Promise<void> => {
|
||||||
|
await this._harmony.Connect(this._ip);
|
||||||
|
};
|
||||||
|
|
||||||
|
public async sendCommand(command: ICommand): Promise<void> {
|
||||||
|
try {
|
||||||
|
//Execute command
|
||||||
|
//HACK to fix Harmon Kardon receiver not turning off
|
||||||
|
if (command.command === "PowerOff") {
|
||||||
|
for (let i = 0; i < 2; i++) {
|
||||||
|
await this._harmony.sendCommand(JSON.stringify(command));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await this._harmony.sendCommand(JSON.stringify(command));
|
||||||
|
|
||||||
|
//Sleep
|
||||||
|
await sleep(800);
|
||||||
|
} catch (err) {
|
||||||
|
this._log(`ERROR - error sending command to harmony: ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/models/index.ts
Normal file
1
src/models/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from "./device";
|
194
src/platform.ts
194
src/platform.ts
@ -1,3 +1,4 @@
|
|||||||
|
import "reflect-metadata";
|
||||||
import {
|
import {
|
||||||
API,
|
API,
|
||||||
Characteristic,
|
Characteristic,
|
||||||
@ -7,12 +8,17 @@ import {
|
|||||||
PlatformConfig,
|
PlatformConfig,
|
||||||
Service,
|
Service,
|
||||||
} from "homebridge";
|
} from "homebridge";
|
||||||
import { ControlUnit, DeviceButton } from "./Accessories";
|
import { ControlUnit, DeviceButton } from "./accessories";
|
||||||
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
|
import { Sequence } from "./accessories/sequence";
|
||||||
import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config";
|
import { IConfig, IControlUnit, IDeviceButton } from "./models/config";
|
||||||
import { HarmonyDevice } from "./Models/HarmonyDevice";
|
import { ISequence } from "./models/config/sequence";
|
||||||
import { HarmonyHub } from "./Models/HarmonyHub";
|
|
||||||
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
|
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
|
||||||
|
import { container } from "tsyringe";
|
||||||
|
import { HarmonyDataProvider } from "./dataProviders/harmonyDataProvider";
|
||||||
|
import { StateDataProvider } from "./dataProviders/stateDataProvider";
|
||||||
|
import { CommandService } from "./services/commandService";
|
||||||
|
import { ActivityService } from "./services/activityService";
|
||||||
|
import { VolumeService } from "./services/volumeService";
|
||||||
|
|
||||||
export class Platform implements DynamicPlatformPlugin {
|
export class Platform implements DynamicPlatformPlugin {
|
||||||
constructor(
|
constructor(
|
||||||
@ -21,74 +27,51 @@ export class Platform implements DynamicPlatformPlugin {
|
|||||||
public readonly api: API
|
public readonly api: API
|
||||||
) {
|
) {
|
||||||
this.log.debug("Finished initializing platform:", config.name);
|
this.log.debug("Finished initializing platform:", config.name);
|
||||||
|
this.config = config as unknown as IConfig;
|
||||||
|
this.register();
|
||||||
|
|
||||||
this.config = (config as unknown) as IConfig;
|
let didFinishLaunching = false;
|
||||||
//construct data provider
|
|
||||||
const dataProvider = new HarmonyDataProvider({
|
|
||||||
hubs: this.config.Hubs,
|
|
||||||
deviceConfigs: this.config.Devices,
|
|
||||||
matrix: this.config.Matrix,
|
|
||||||
log: this.log,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.api.on("didFinishLaunching", async () => {
|
this.api.on("didFinishLaunching", async () => {
|
||||||
log.debug("Executed didFinishLaunching callback");
|
log.debug("Executed didFinishLaunching callback");
|
||||||
this.discoverDevices(dataProvider);
|
this.discoverControlUnitAccessories();
|
||||||
|
this.discoverDeviceButtonAccessories();
|
||||||
|
this.discoverSequenceAccessories();
|
||||||
|
this.pruneAccessories();
|
||||||
|
didFinishLaunching = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.dataProvider = null;
|
|
||||||
|
|
||||||
if (this.config) {
|
|
||||||
//construct data provider
|
|
||||||
this.dataProvider = new HarmonyDataProvider({
|
|
||||||
hubs: this.config.Hubs,
|
|
||||||
deviceConfigs: this.config.Devices,
|
|
||||||
matrix: this.config.Matrix,
|
|
||||||
log: this.log,
|
|
||||||
});
|
|
||||||
|
|
||||||
//Emit devices if requested
|
|
||||||
|
|
||||||
this.dataProvider.on("Ready", () => {
|
|
||||||
this.log.info("All hubs connected");
|
|
||||||
if (this.config.EmitDevicesOnStartup) {
|
|
||||||
const hubs = this.dataProvider!.hubs;
|
|
||||||
Object.values(hubs).forEach((hub: HarmonyHub) => {
|
|
||||||
const deviceDictionary = hub.devices;
|
|
||||||
this.log.info(`${hub.hubName}`);
|
|
||||||
|
|
||||||
Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
|
|
||||||
this.log.info(` ${device.name} : ${device.id}`);
|
|
||||||
Object.keys(device.commands).forEach((command: string) => {
|
|
||||||
this.log.info(` ${command}`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly Service: typeof Service = this.api.hap.Service;
|
public readonly Service: typeof Service = this.api.hap.Service;
|
||||||
public readonly Characteristic: typeof Characteristic = this.api.hap
|
public readonly Characteristic: typeof Characteristic =
|
||||||
.Characteristic;
|
this.api.hap.Characteristic;
|
||||||
|
|
||||||
// this is used to track restored cached accessories
|
// this is used to track restored cached accessories
|
||||||
public readonly accessories: PlatformAccessory[] = [];
|
public readonly accessories: PlatformAccessory[] = [];
|
||||||
public config: IConfig;
|
public config: IConfig;
|
||||||
public dataProvider: HarmonyDataProvider | null;
|
|
||||||
|
|
||||||
public discoverDevices(dataProvider: HarmonyDataProvider) {
|
/**
|
||||||
|
* Discover new control unit accessories
|
||||||
|
* @param dataProvider
|
||||||
|
*/
|
||||||
|
private discoverControlUnitAccessories(): void {
|
||||||
this.config.ControlUnits.forEach((unit: IControlUnit) => {
|
this.config.ControlUnits.forEach((unit: IControlUnit) => {
|
||||||
const uuid = this.api.hap.uuid.generate(unit.DisplayName);
|
const uuid = this.api.hap.uuid.generate(unit.DisplayName);
|
||||||
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
|
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
|
||||||
|
|
||||||
if (existingAccessory) {
|
if (existingAccessory) {
|
||||||
this.log.info(
|
this.log.info(
|
||||||
"Restoring existing accessory from cache: " +
|
"Restoring existing accessory from cache: " +
|
||||||
existingAccessory.displayName
|
existingAccessory.displayName
|
||||||
);
|
);
|
||||||
|
|
||||||
new ControlUnit(this, existingAccessory, dataProvider, unit.Activities);
|
new ControlUnit(
|
||||||
|
this,
|
||||||
|
existingAccessory,
|
||||||
|
container.resolve(ActivityService),
|
||||||
|
container.resolve(CommandService),
|
||||||
|
container.resolve(VolumeService),
|
||||||
|
unit.Activities
|
||||||
|
);
|
||||||
|
|
||||||
this.api.publishExternalAccessories(PLUGIN_NAME, [existingAccessory]);
|
this.api.publishExternalAccessories(PLUGIN_NAME, [existingAccessory]);
|
||||||
console.log("Publishing external accessory: " + uuid);
|
console.log("Publishing external accessory: " + uuid);
|
||||||
@ -99,14 +82,28 @@ export class Platform implements DynamicPlatformPlugin {
|
|||||||
uuid
|
uuid
|
||||||
);
|
);
|
||||||
accessory.context["DeviceName"] = unit.DisplayName;
|
accessory.context["DeviceName"] = unit.DisplayName;
|
||||||
|
accessory.context["Type"] = typeof ControlUnit.name;
|
||||||
|
|
||||||
new ControlUnit(this, accessory, dataProvider, unit.Activities);
|
new ControlUnit(
|
||||||
|
this,
|
||||||
|
accessory,
|
||||||
|
container.resolve(ActivityService),
|
||||||
|
container.resolve(CommandService),
|
||||||
|
container.resolve(VolumeService),
|
||||||
|
unit.Activities
|
||||||
|
);
|
||||||
|
|
||||||
this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
|
this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
|
||||||
console.log("Publishing external accessory: " + uuid);
|
console.log("Publishing external accessory: " + uuid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover new device button accessories
|
||||||
|
* @param dataProvider
|
||||||
|
*/
|
||||||
|
private discoverDeviceButtonAccessories(): void {
|
||||||
this.config.DeviceButtons.forEach((button: IDeviceButton) => {
|
this.config.DeviceButtons.forEach((button: IDeviceButton) => {
|
||||||
const uuid = this.api.hap.uuid.generate(button.DisplayName);
|
const uuid = this.api.hap.uuid.generate(button.DisplayName);
|
||||||
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
|
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
|
||||||
@ -116,7 +113,12 @@ export class Platform implements DynamicPlatformPlugin {
|
|||||||
existingAccessory.displayName
|
existingAccessory.displayName
|
||||||
);
|
);
|
||||||
|
|
||||||
new DeviceButton(this, existingAccessory, dataProvider, button);
|
new DeviceButton(
|
||||||
|
this,
|
||||||
|
existingAccessory,
|
||||||
|
container.resolve(HarmonyDataProvider),
|
||||||
|
button
|
||||||
|
);
|
||||||
this.api.updatePlatformAccessories([existingAccessory]);
|
this.api.updatePlatformAccessories([existingAccessory]);
|
||||||
} else {
|
} else {
|
||||||
this.log.info("Adding new accessory: " + button.DisplayName);
|
this.log.info("Adding new accessory: " + button.DisplayName);
|
||||||
@ -125,20 +127,102 @@ export class Platform implements DynamicPlatformPlugin {
|
|||||||
uuid
|
uuid
|
||||||
);
|
);
|
||||||
accessory.context["DeviceName"] = button.DisplayName;
|
accessory.context["DeviceName"] = button.DisplayName;
|
||||||
|
accessory.context["Type"] = typeof DeviceButton;
|
||||||
|
|
||||||
new DeviceButton(this, accessory, dataProvider, button);
|
new DeviceButton(
|
||||||
|
this,
|
||||||
|
accessory,
|
||||||
|
container.resolve(HarmonyDataProvider),
|
||||||
|
button
|
||||||
|
);
|
||||||
|
|
||||||
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
|
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
|
||||||
accessory,
|
accessory,
|
||||||
]);
|
]);
|
||||||
|
this.accessories.push(accessory);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover new sequence accessories
|
||||||
|
* @param dataProvider
|
||||||
|
*/
|
||||||
|
private discoverSequenceAccessories(): void {
|
||||||
|
this.config.Sequences.forEach((sequence: ISequence) => {
|
||||||
|
const uuid = this.api.hap.uuid.generate(sequence.DisplayName);
|
||||||
|
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
|
||||||
|
if (existingAccessory) {
|
||||||
|
this.log.info(
|
||||||
|
"Restoring existing accessory from cache: " +
|
||||||
|
existingAccessory.displayName
|
||||||
|
);
|
||||||
|
|
||||||
|
new Sequence(
|
||||||
|
this,
|
||||||
|
existingAccessory,
|
||||||
|
container.resolve(HarmonyDataProvider),
|
||||||
|
sequence
|
||||||
|
);
|
||||||
|
this.api.updatePlatformAccessories([existingAccessory]);
|
||||||
|
} else {
|
||||||
|
this.log.info("Adding new accessory: " + sequence.DisplayName);
|
||||||
|
const accessory = new this.api.platformAccessory(
|
||||||
|
sequence.DisplayName,
|
||||||
|
uuid
|
||||||
|
);
|
||||||
|
accessory.context["DeviceName"] = sequence.DisplayName;
|
||||||
|
accessory.context["Type"] = typeof "Sequence";
|
||||||
|
|
||||||
|
new Sequence(
|
||||||
|
this,
|
||||||
|
accessory,
|
||||||
|
container.resolve(HarmonyDataProvider),
|
||||||
|
sequence
|
||||||
|
);
|
||||||
|
|
||||||
|
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
|
||||||
|
accessory,
|
||||||
|
]);
|
||||||
|
this.accessories.push(accessory);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private pruneAccessories(): void {
|
||||||
|
// Remove devices not discovered
|
||||||
|
const listOfConfiguredAccessories = [
|
||||||
|
...this.config.DeviceButtons.map((e) => e.DisplayName),
|
||||||
|
...this.config.Sequences.map((e) => e.DisplayName),
|
||||||
|
...this.config.ControlUnits.map((e) => e.DisplayName),
|
||||||
|
];
|
||||||
|
for (const accessory of this.accessories) {
|
||||||
|
if (
|
||||||
|
listOfConfiguredAccessories.filter(
|
||||||
|
(displayName) => displayName === accessory.displayName
|
||||||
|
).length === 0
|
||||||
|
) {
|
||||||
|
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
|
||||||
|
accessory,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
configureAccessory(accessory: PlatformAccessory<Record<string, any>>): void {
|
configureAccessory(accessory: PlatformAccessory<Record<string, any>>): void {
|
||||||
this.log.info("Loading accessory from cache:", accessory.displayName);
|
this.log.info("Loading accessory from cache:", accessory.displayName);
|
||||||
|
|
||||||
// add the restored accessory to the accessories cache so we can track if it has already been registered
|
// add the restored accessory to the accessories cache so we can track if it has already been registered
|
||||||
this.accessories.push(accessory);
|
this.accessories.push(accessory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private register(): void {
|
||||||
|
container.register<Platform>(Platform, { useValue: this });
|
||||||
|
container.register<IConfig>("IConfig", { useValue: this.config });
|
||||||
|
container.register<Logger>("log", { useValue: this.log });
|
||||||
|
container.registerSingleton<HarmonyDataProvider>(HarmonyDataProvider);
|
||||||
|
container.registerSingleton<ActivityService>(ActivityService);
|
||||||
|
container.registerSingleton<StateDataProvider>(StateDataProvider);
|
||||||
|
container.registerSingleton<CommandService>(CommandService);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
226
src/services/activityService.ts
Normal file
226
src/services/activityService.ts
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
import { Logging } from "homebridge";
|
||||||
|
import { inject, injectable } from "tsyringe";
|
||||||
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
||||||
|
import { StateDataProvider } from "../dataProviders/stateDataProvider";
|
||||||
|
import { IActivityState } from "../models/activityState";
|
||||||
|
import {
|
||||||
|
IActivity,
|
||||||
|
IConfig,
|
||||||
|
IDeviceSetupItem,
|
||||||
|
IInput,
|
||||||
|
IOutput,
|
||||||
|
} from "../models/config";
|
||||||
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class ActivityService {
|
||||||
|
constructor(
|
||||||
|
@inject(HarmonyDataProvider)
|
||||||
|
private _harmonyDataProvider: HarmonyDataProvider,
|
||||||
|
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
|
||||||
|
@inject("IConfig") private _config: IConfig,
|
||||||
|
@inject("log") private _log: Logging
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public startActivity = async (
|
||||||
|
controlUnitName: string,
|
||||||
|
activity: IActivity
|
||||||
|
) => {
|
||||||
|
this._log.info(
|
||||||
|
`Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}`
|
||||||
|
);
|
||||||
|
let devicesToTurnOn: Array<HarmonyDevice> = this.getDevicesToTurnOn(
|
||||||
|
activity,
|
||||||
|
controlUnitName
|
||||||
|
);
|
||||||
|
|
||||||
|
await this._harmonyDataProvider.powerOnDevices(devicesToTurnOn);
|
||||||
|
|
||||||
|
await this.assignDeviceInput(activity);
|
||||||
|
|
||||||
|
await this.routeInputsToOutputs(activity);
|
||||||
|
|
||||||
|
let lastActivity = this.getCurrentActivity(controlUnitName);
|
||||||
|
|
||||||
|
let devicesToTurnOff: Array<HarmonyDevice> = this.getDevicesToTurnOff(
|
||||||
|
controlUnitName,
|
||||||
|
lastActivity,
|
||||||
|
activity
|
||||||
|
);
|
||||||
|
await this._harmonyDataProvider.powerOffDevices(devicesToTurnOff);
|
||||||
|
|
||||||
|
this._stateDataProvider.updateState(activity, controlUnitName);
|
||||||
|
};
|
||||||
|
|
||||||
|
public stopCurrentActivity = async (
|
||||||
|
controlUnitName: string
|
||||||
|
): Promise<void> => {
|
||||||
|
if (!this.getCurrentActivity(controlUnitName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastActivity: IActivity | undefined =
|
||||||
|
this.getCurrentActivity(controlUnitName);
|
||||||
|
|
||||||
|
let devicesToTurnOff: Array<HarmonyDevice> = this.getDevicesToTurnOff(
|
||||||
|
controlUnitName,
|
||||||
|
lastActivity
|
||||||
|
);
|
||||||
|
await this._harmonyDataProvider.powerOffDevices(devicesToTurnOff);
|
||||||
|
|
||||||
|
this._stateDataProvider.removeState(controlUnitName);
|
||||||
|
};
|
||||||
|
|
||||||
|
public getCurrentActivity(controlUnitName: string): IActivity | undefined {
|
||||||
|
return this._stateDataProvider.getState(controlUnitName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if a control unit is active
|
||||||
|
* @param controlUnitName
|
||||||
|
*/
|
||||||
|
public getIsActive(controlUnitName: string): IActivity | undefined {
|
||||||
|
return this._stateDataProvider.getState(controlUnitName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to make sure no control unit depends on device list.
|
||||||
|
* @param devicesToTurnOn The list of devices to modify.
|
||||||
|
* @param controlUnitName The name of the control unit in question.
|
||||||
|
*/
|
||||||
|
private sanitizeDeviceList(
|
||||||
|
devicesToTurnOn: Array<HarmonyDevice>,
|
||||||
|
controlUnitName: string
|
||||||
|
): Array<HarmonyDevice> {
|
||||||
|
for (let controlUnitKey in this._stateDataProvider.states) {
|
||||||
|
//Skip self
|
||||||
|
if (controlUnitKey === controlUnitName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let currentOtherState: IActivityState =
|
||||||
|
this._stateDataProvider.states[controlUnitKey]!;
|
||||||
|
|
||||||
|
if (!currentOtherState) {
|
||||||
|
return devicesToTurnOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentOtherState.currentActivity.DeviceSetupList.forEach(
|
||||||
|
(value: IDeviceSetupItem) => {
|
||||||
|
//there are devices to remove
|
||||||
|
if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) {
|
||||||
|
let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter(
|
||||||
|
(i) => i.name === value.DeviceName
|
||||||
|
)[0];
|
||||||
|
delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return devicesToTurnOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDevicesToTurnOn(
|
||||||
|
activity: IActivity,
|
||||||
|
controlUnitName: string
|
||||||
|
): Array<HarmonyDevice> {
|
||||||
|
let potentialDevices = this.buildPotentialDeviceList(activity);
|
||||||
|
return this.sanitizeDeviceList(potentialDevices, controlUnitName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDevicesToTurnOff(
|
||||||
|
controlUnitName: string,
|
||||||
|
lastActivity?: IActivity,
|
||||||
|
nextActivity?: IActivity
|
||||||
|
) {
|
||||||
|
let potentialDevices = lastActivity
|
||||||
|
? this.buildPotentialDeviceList(lastActivity)
|
||||||
|
: [];
|
||||||
|
|
||||||
|
//remove devices that will be used for next activity from list
|
||||||
|
//delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls
|
||||||
|
if (nextActivity) {
|
||||||
|
potentialDevices.forEach((device: HarmonyDevice, index: number) => {
|
||||||
|
if (
|
||||||
|
device &&
|
||||||
|
device.name &&
|
||||||
|
nextActivity.DeviceSetupList.some((e) => {
|
||||||
|
return e && e.DeviceName === device.name;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
delete potentialDevices[index];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.sanitizeDeviceList(potentialDevices, controlUnitName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildPotentialDeviceList(activity: IActivity): HarmonyDevice[] {
|
||||||
|
return activity.DeviceSetupList.map(
|
||||||
|
(value: IDeviceSetupItem): HarmonyDevice => {
|
||||||
|
return this._harmonyDataProvider.getDeviceByName(value.DeviceName);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async assignDeviceInput(activity: IActivity): Promise<void> {
|
||||||
|
await Promise.all(
|
||||||
|
activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => {
|
||||||
|
let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName(
|
||||||
|
value.DeviceName
|
||||||
|
);
|
||||||
|
|
||||||
|
if (device && device.supportsCommand(`Input${value.Input}`)) {
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
`Input${value.Input}`,
|
||||||
|
device
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async routeInputsToOutputs(activity: IActivity) {
|
||||||
|
if (activity.UseMatrix) {
|
||||||
|
//get input and output
|
||||||
|
let input: IInput = this._config.Matrix.Inputs.filter(
|
||||||
|
(e) => e.InputDevice === activity.ControlDevice
|
||||||
|
)[0];
|
||||||
|
let output: IOutput = this._config.Matrix.Outputs.filter(
|
||||||
|
(e) => e.OutputDevice === activity.OutputDevice
|
||||||
|
)[0];
|
||||||
|
|
||||||
|
let inputCommandName: string = `In ${input.InputNumber}`;
|
||||||
|
let outputCommandName: string = `Out ${output.OutputLetter}`;
|
||||||
|
|
||||||
|
let matrixDevice: HarmonyDevice =
|
||||||
|
this._harmonyDataProvider.getDeviceByName(
|
||||||
|
this._config.Matrix.DeviceName
|
||||||
|
);
|
||||||
|
|
||||||
|
//Route hdmi
|
||||||
|
if (
|
||||||
|
matrixDevice.supportsCommand(inputCommandName) &&
|
||||||
|
matrixDevice.supportsCommand(outputCommandName)
|
||||||
|
) {
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
outputCommandName,
|
||||||
|
matrixDevice
|
||||||
|
);
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
inputCommandName,
|
||||||
|
matrixDevice
|
||||||
|
);
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
outputCommandName,
|
||||||
|
matrixDevice
|
||||||
|
);
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
inputCommandName,
|
||||||
|
matrixDevice
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
71
src/services/commandService.ts
Normal file
71
src/services/commandService.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { inject, injectable } from "tsyringe";
|
||||||
|
import { RemoteKey } from "../accessories/controlUnit";
|
||||||
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
||||||
|
import { StateDataProvider } from "../dataProviders/stateDataProvider";
|
||||||
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class CommandService {
|
||||||
|
constructor(
|
||||||
|
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
|
||||||
|
@inject(HarmonyDataProvider)
|
||||||
|
private _harmonyDataProvider: HarmonyDataProvider
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send key press for current activity.
|
||||||
|
*
|
||||||
|
* @param controlUnitName The name of the control unit to act on.
|
||||||
|
* @param key The key to send.
|
||||||
|
*/
|
||||||
|
public sendKeyPress = async (controlUnitName: string, key: any) => {
|
||||||
|
let currentActivity = this._stateDataProvider.getState(controlUnitName);
|
||||||
|
if (currentActivity) {
|
||||||
|
let commandName: string = "";
|
||||||
|
|
||||||
|
let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName(
|
||||||
|
currentActivity.ControlDevice
|
||||||
|
);
|
||||||
|
switch (key) {
|
||||||
|
case RemoteKey.ARROW_UP: {
|
||||||
|
commandName = "Direction Up";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.ARROW_DOWN: {
|
||||||
|
commandName = "Direction Down";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.ARROW_LEFT: {
|
||||||
|
commandName = "Direction Left";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.ARROW_RIGHT: {
|
||||||
|
commandName = "Direction Right";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.SELECT: {
|
||||||
|
commandName = "Select";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.PLAY_PAUSE: {
|
||||||
|
commandName = "Pause";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.INFORMATION: {
|
||||||
|
commandName = "Menu";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.BACK: {
|
||||||
|
commandName = "Back";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RemoteKey.EXIT: {
|
||||||
|
commandName = "Back";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this._harmonyDataProvider.sendCommand(commandName, device);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
45
src/services/volumeService.ts
Normal file
45
src/services/volumeService.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { inject, injectable } from "tsyringe";
|
||||||
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
||||||
|
import { StateDataProvider } from "../dataProviders/stateDataProvider";
|
||||||
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class VolumeService {
|
||||||
|
constructor(
|
||||||
|
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
|
||||||
|
@inject(HarmonyDataProvider)
|
||||||
|
private _harmonyDataProvider: HarmonyDataProvider
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn the volume up for the current running activity.
|
||||||
|
*/
|
||||||
|
public volumeUp = async (controlUnitName: string) => {
|
||||||
|
let volumeUpCommand: string = "Volume Up";
|
||||||
|
let currentActivity = this._stateDataProvider.getState(controlUnitName);
|
||||||
|
if (currentActivity) {
|
||||||
|
let volumeDevice: HarmonyDevice =
|
||||||
|
this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice);
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
volumeUpCommand,
|
||||||
|
volumeDevice
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Volume down for current running activity.
|
||||||
|
*/
|
||||||
|
public volumeDown = async (controlUnitName: string) => {
|
||||||
|
let volumeDownCommand: string = "Volume Down";
|
||||||
|
let currentActivity = this._stateDataProvider.getState(controlUnitName);
|
||||||
|
if (currentActivity) {
|
||||||
|
let volumeDevice: HarmonyDevice =
|
||||||
|
this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice);
|
||||||
|
await this._harmonyDataProvider.sendCommand(
|
||||||
|
volumeDownCommand,
|
||||||
|
volumeDevice
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -6,4 +6,4 @@ export const PLATFORM_NAME = "HarmonyHubMatrix";
|
|||||||
/**
|
/**
|
||||||
* This must match the name of your plugin as defined the package.json
|
* This must match the name of your plugin as defined the package.json
|
||||||
*/
|
*/
|
||||||
export const PLUGIN_NAME = "homebridge-harmony-control";
|
export const PLUGIN_NAME = "@watsonb8/homebridge-harmony-control";
|
||||||
|
2
src/util/index.ts
Normal file
2
src/util/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./callbackify";
|
||||||
|
export * from "./sleep";
|
@ -6,7 +6,9 @@
|
|||||||
"outDir": "./bin",
|
"outDir": "./bin",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"skipLibCheck": true
|
"skipLibCheck": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"emitDecoratorMetadata": true
|
||||||
},
|
},
|
||||||
"include": ["src"],
|
"include": ["src"],
|
||||||
"exclude": ["**/node_modules", "**/__tests__/*"]
|
"exclude": ["**/node_modules", "**/__tests__/*"]
|
||||||
|
Reference in New Issue
Block a user