├── .gitignore ├── LICENSE ├── README.md ├── config.example.json ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Evan Coleman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # homebridge-harmony-api 2 | 3 | > A [homebridge](https://github.com/nfarina/homebridge) plugin for controlling individual Harmony Hub devices via [harmony-api](https://github.com/maddox/harmony-api). 4 | 5 | [![npm version](https://badge.fury.io/js/homebridge-harmony-api.svg)](https://badge.fury.io/js/homebridge-harmony-api) 6 | [![License][license-image]][license-url] 7 | 8 | `homebridge-harmony-api` currently supports `Switch`, `Fan`, and `Speaker` accessory types. 9 | 10 | ## Installation 11 | 12 | This guide assumes that you already have a running [`harmony-api`](https://github.com/maddox/harmony-api) server. 13 | 14 | ``` 15 | # Install homebridge 16 | $ npm install -g homebridge 17 | 18 | # Install plugin 19 | $ npm install -g homebridge-harmony-api 20 | ``` 21 | 22 | or add `homebridge-harmony-api` to your `install.sh` file. 23 | 24 | ## Configuration 25 | 26 | Configuration is as simple as adding a new `accessories` object for each device you'd like to control. Below is an example for an IR controlled air conditioner that I've taught my Harmony Hub to control. For more examples, see [config.example.json](config.example.json). 27 | 28 | ```json 29 | { 30 | "accessory": "HarmonyDevice", 31 | "name": "Living Room Air Conditioner", 32 | "service": "Fan", 33 | "host": "localhost", 34 | "port": 8282, 35 | "hub_slug": "living-room", 36 | "device_slug": "air-conditioner", 37 | "commands": { 38 | "on": "power-toggle", 39 | "off": "power-toggle", 40 | "rotation_speed": { 41 | // the keys here correspond to the rotation speed percentage, 42 | // the values are the corresponding command to send 43 | "33": "low", 44 | "67": "med", 45 | "100": "high" 46 | } 47 | } 48 | } 49 | ``` 50 | 51 | ## Caveats 52 | 53 | `harmony-api` currently conflicts with the [`homebridge-harmonyhub`](https://github.com/KraigM/homebridge-harmonyhub) plugin. You won't be able to run them on the same host because they bind to the same port to discover harmony hubs on your network. My personal workaround for this is to link my Harmony Activities to HomeKit via my SmartThings hub and then using the [`homebridge-smartthings`](https://github.com/pdlove/homebridge-smartthings) plugin. In the future I'd like to add support for harmony activities to this plugin. 54 | 55 | ## TODO 56 | 57 | * Add support for harmony hub activities 58 | * Investigate adding support for more HomeKit services and characteristics 59 | 60 | ## Meta 61 | 62 | You can find me on Twitter [@edc1591](https://twitter.com/edc1591) 63 | 64 | [Hire me!](http://edc.me) 65 | 66 | Distributed under the MIT license. See ``LICENSE`` for more information. 67 | 68 | [license-image]: https://img.shields.io/badge/License-MIT-blue.svg 69 | [license-url]: LICENSE -------------------------------------------------------------------------------- /config.example.json: -------------------------------------------------------------------------------- 1 | { 2 | "bridge": { 3 | "name": "Homebridge", 4 | "username": "CC:22:3D:E3:CE:30", 5 | "port": 51826, 6 | "pin": "031-45-154" 7 | }, 8 | "accessories": [ 9 | { 10 | "accessory": "HarmonyDevice", 11 | "name": "Living Room Air Conditioner", 12 | "service": "Fan", 13 | "host": "localhost", 14 | "port": 8282, 15 | "hub_slug": "living-room", 16 | "device_slug": "air-conditioner", 17 | "commands": { 18 | "on": "power-toggle", 19 | "off": "power-toggle", 20 | "rotation_speed": { 21 | // the keys here correspond to the rotation speed percentage, 22 | // the values are the corresponding command to send 23 | "33": "low", 24 | "67": "med", 25 | "100": "high" 26 | } 27 | } 28 | }, 29 | { 30 | "accessory": "HarmonyDevice", 31 | "name": "Living Room Speakers", 32 | "service": "Speaker", 33 | "host": "localhost", 34 | "port": 8282, 35 | "hub_slug": "living-room", 36 | "device_slug": "air-conditioner", 37 | "commands": { 38 | "mute": "mute" 39 | } 40 | }, 41 | { 42 | "accessory": "HarmonyDevice", 43 | "name": "Bedroom TV", 44 | "service": "Switch", 45 | "host": "localhost", 46 | "port": 8282, 47 | "hub_slug": "bedroom", 48 | "device_slug": "tv", 49 | "commands": { 50 | "on": "power-on", 51 | "off": "power-off" 52 | } 53 | } 54 | ] 55 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Service, Characteristic; 2 | var request = require("request"); 3 | 4 | module.exports = function (homebridge) { 5 | Service = homebridge.hap.Service; 6 | Characteristic = homebridge.hap.Characteristic; 7 | homebridge.registerAccessory("homebridge-harmony-api", "HarmonyDevice", HarmonyDevice); 8 | }; 9 | 10 | 11 | function HarmonyDevice(log, config) { 12 | this.log = log; 13 | 14 | // url info 15 | this.name = config["name"]; 16 | this.serviceType = config["service"] || "Switch"; 17 | this.host = config["host"]; 18 | this.port = config["port"]; 19 | this.hubSlug = config["hub_slug"]; 20 | this.deviceSlug = config["device_slug"]; 21 | this.commands = config["commands"]; 22 | } 23 | 24 | HarmonyDevice.prototype = { 25 | httpRequest: function (method, url, callback) { 26 | request({ 27 | url: url, 28 | method: method 29 | }, 30 | function (error, response, body) { 31 | callback(error, response, body) 32 | }) 33 | }, 34 | 35 | setPowerState: function (powerState, callback) { 36 | console.log("[HarmonyDevice] Power On", powerState); 37 | 38 | var commandSlug = this.state ? this.commands["off"] : this.commands["on"]; 39 | var url = "http://" + this.host + ":" + this.port + "/hubs/" + this.hubSlug + "/devices/" + this.deviceSlug + "/commands/" + commandSlug; 40 | 41 | var that = this; 42 | this.httpRequest("POST", url, function (error, response, responseBody) { 43 | if (error) { 44 | console.log("[HarmonyDevice] set power state failed: %s", error.message); 45 | callback(error); 46 | } else { 47 | console.log("[HarmonyDevice] set power state succeeded!"); 48 | that.state = powerState; 49 | callback(); 50 | } 51 | }.bind(this)); 52 | }, 53 | 54 | getPowerState: function (callback) { 55 | callback(null, this.state); 56 | }, 57 | 58 | setRotationSpeed: function (rotationSpeed, callback) { 59 | console.log("[HarmonyDevice] Rotation Speed", rotationSpeed); 60 | 61 | var commands = this.commands["rotation_speed"]; 62 | var levels = Object.keys(commands) 63 | .map(function(val) { return parseInt(val); }) 64 | .sort(function(a, b) { return a - b }); 65 | var command = null; 66 | for (var i = 0; i < levels.length; i++) { 67 | var level = levels[i]; 68 | if (level >= rotationSpeed) { 69 | command = commands[level.toString()]; 70 | break; 71 | } 72 | } 73 | if (command == null) { 74 | callback(new Error("Missing command")); 75 | return; 76 | } 77 | var url = "http://" + this.host + ":" + this.port + "/hubs/" + this.hubSlug + "/devices/" + this.deviceSlug + "/commands/" + command; 78 | 79 | this.httpRequest("POST", url, function (error, response, responseBody) { 80 | if (error) { 81 | console.log("[HarmonyDevice] set rotation speed failed: %s", error.message); 82 | callback(error); 83 | } else { 84 | console.log("[HarmonyDevice] set rotation speed succeeded!"); 85 | this.rotationSpeed = rotationSpeed; 86 | callback(); 87 | } 88 | }.bind(this)); 89 | }, 90 | 91 | getRotationSpeed: function (callback) { 92 | callback(null, this.rotationSpeed); 93 | }, 94 | 95 | setMuteState: function (muteState, callback) { 96 | console.log("Mute", muteState); 97 | 98 | var url = "http://" + this.host + ":" + this.port + "/hubs/" + this.hubSlug + "/devices/" + this.deviceSlug + "/commands/" + this.muteSlug; 99 | 100 | this.httpRequest("POST", url, function (error, response, responseBody) { 101 | if (error) { 102 | console.log("[HarmonyDevice] set mute state failed: %s", error.message); 103 | callback(error); 104 | } else { 105 | console.log("[HarmonyDevice] set mute state succeeded!"); 106 | this.muted = muteState; 107 | callback(); 108 | } 109 | }.bind(this)); 110 | }, 111 | 112 | getMuteState: function (callback) { 113 | callback(null, this.muted); 114 | }, 115 | 116 | identify: function (callback) { 117 | this.log("[HarmonyDevice] Identify requested!"); 118 | callback(); // success 119 | }, 120 | 121 | getServices: function () { 122 | var that = this; 123 | 124 | // you can OPTIONALLY create an information service if you wish to override 125 | // the default values for things like serial number, model, etc. 126 | var informationService = new Service.AccessoryInformation(); 127 | 128 | informationService 129 | .setCharacteristic(Characteristic.Manufacturer, "HarmonyDevice Manufacturer") 130 | .setCharacteristic(Characteristic.Model, "HarmonyDevice Model") 131 | .setCharacteristic(Characteristic.SerialNumber, "HarmonyDevice Serial Number"); 132 | 133 | switch (this.serviceType) { 134 | case "Switch": 135 | this.service = new Service.Switch(this.name); 136 | this.service 137 | .getCharacteristic(Characteristic.On) 138 | .on("get", this.getPowerState.bind(this)) 139 | .on("set", this.setPowerState.bind(this)); 140 | 141 | return [this.service]; 142 | case "Fan": 143 | this.service = new Service.Fan(this.name); 144 | this.service 145 | .getCharacteristic(Characteristic.On) 146 | .on("get", this.getPowerState.bind(this)) 147 | .on("set", this.setPowerState.bind(this)); 148 | 149 | if (this.commands["rotation_speed"]) { 150 | this.service 151 | .getCharacteristic(Characteristic.RotationSpeed) 152 | .on("get", this.getRotationSpeed.bind(this)) 153 | .on("set", this.setRotationSpeed.bind(this)); 154 | } 155 | 156 | return [this.service]; 157 | case "Speaker": 158 | this.service = new Service.Speaker(this.name); 159 | this.service 160 | .getCharacteristic(Characteristic.Mute) 161 | .on("get", this.getMuteState.bind(this)) 162 | .on("set", this.setMuteState.bind(this)); 163 | 164 | return [this.service]; 165 | } 166 | } 167 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "homebridge-harmony-api", 3 | "version": "0.1.0", 4 | "description": "A homebridge plugin for controlling individual Harmony Hub connected devices via: https://github.com/maddox/harmony-api", 5 | "license": "MIT", 6 | "keywords": [ 7 | "homebridge-plugin", 8 | "harmony-api", 9 | "logitech harmony", 10 | "harmony hub" 11 | ], 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/edc1591/homebridge-harmony-api.git" 15 | }, 16 | "bugs": { 17 | "url": "http://github.com/edc1591/homebridge-harmony-api/issues" 18 | }, 19 | "engines": { 20 | "node": ">=0.12.0", 21 | "homebridge": ">=0.2.0" 22 | }, 23 | "dependencies": { 24 | "request": "^2.82.0" 25 | } 26 | } 27 | --------------------------------------------------------------------------------