├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | homebridge-script2 2 | ================== 3 | 4 | Execute custom scripts via homekit apps. 5 | 6 | Core of the code written by [@xxcombat](https://github.com/xxcombat/). Great plugin that has served me well. 7 | Original plugin [homebridge-script](https://github.com/xxcombat/homebridge-script). 8 | 9 | Because it appears that the original [homebridge-script](https://github.com/xxcombat/homebridge-script) plugin has stopped being maintained and supported and PR's are also not being accepted. I've updated it to allow for executing a state script or work by checking for the existance of a file. Thanks to [@ybizeul](https://github.com/ybizeul/) for the code snipet that allows for state.sh to execute. This plugin also works with the latest file-exists that broke the original plugin. 10 | While this fork depends on file-exists there is no need to install it seperately for this fork, as I've included it as a dependency. 11 | 12 | 13 | ## Installation 14 | (Requires node >=6.0.0) 15 | 16 | 1. Install homebridge using: `npm install -g homebridge` 17 | 2. Install this plugin using: `npm install -g homebridge-script2` 18 | 3. Update your configuration file. See examples below that show the plugin working by using filestate for current state check as well as an example using state.sh script for current state check. 19 | 4. Make sure scripts have been made executable (chmod +x scriptname.sh) and also accessible by the homebridge user. 20 | 21 | 22 | Homebridge-script configuration parameters 23 | 24 | Name | Value | Required | Notes 25 | --------------- | ------------- | ------------------------------------------- | ------------- 26 | `accessory` | "Script2" | yes | Must be set to "Script2" and is required 27 | `name` | _(custom)_ | yes | Name of accessory that will appear in homekit app and is required 28 | `on` | _(custom)_ | yes | Location of script to execute the on action and is required 29 | `off` | _(custom)_ | yes | Location of script to execute the off action and is required 30 | `fileState` | _(custom)_ | fileState or state is required (see note) | Location of file that flags on or off current state. If this is configured the plugin will use the existence of this file to determine the current on or off state. If file exists, accessory is determined to be on. If file does not exist, accessory is determined to be off. This is not required. But if set, it will override using the state script. fileState or state must be configured. Use full path when setting this it's value. Do not use "~/". 31 | `state` | _(custom)_ | fileState or state is required (see note) | Location of script to execute the current state check. It must output to stdout the current state. It is not required if fileState is being used instead. fileState or state must be configured. 32 | `on_value` | _(custom)_ | no* (see note, default set to "true") | Used in conjunction with the state script. If using the state script this is the value that will be used to match against the state script output. If this value matches the output, then the accessory will be determined to be on. Required if using state script. 33 | `unique_serial` | _(custom)_ | no (default set to "Script2 Serial number") | If you have more than one "accessory" configured, please set unique values for each accessory. Unique values per accessory required for the Eve app. 34 | 35 | ## Configuration 36 | 37 | ### Configuration example 1, using filestate for current state check: 38 | 39 | ``` 40 | "accessories": 41 | [ 42 | { 43 | "accessory": "Script2", 44 | "name": "RPC3 Socket 1", 45 | "on": "/var/homebridge/rpc3control/on.sh 1", 46 | "off": "/var/homebridge/rpc3control/off.sh 1", 47 | "state": "/var/homebridge/rpc3control/state.sh 1", 48 | "fileState": "/var/homebridge/rpc3control/script1.flag", 49 | "on_value": "true", 50 | "unique_serial": "1234567" 51 | } 52 | ] 53 | ``` 54 | 55 | #### Notes 56 | ##### Using the above configuration as an example: 57 | - The on.sh script executes when you turn on the accessory via a homekit app. (In this case we are the using existence of a file to determine on or off current state, so you must insure the on.sh script creates the configured fileState file. 58 | - The off.sh script executes when you turn off the accessory via a homekit app. ( In this case we are using existence of a file to determine on or off current state, insure the off.sh script deletes the configured fileState file.) 59 | - The state.sh script in this case would not execute as fileState parameter overrides its use. 60 | - The configured fileState file is used as a flag. When the homekit app checks for current state it checks for the existence of this file. If it exists, current state is on. If it does not exist, current state is off. 61 | - The on_value in this case is not being used as it is only used when the state script is used to check for current state. 62 | 63 | ### Configuration example 2, executing state.sh script for current state check: 64 | 65 | ``` 66 | "accessories": 67 | [ 68 | { 69 | "accessory": "Script2", 70 | "name": "Alarm of bike", 71 | "on": "~/on.sh", 72 | "off": "~/off.sh", 73 | "state": "~/state.sh", 74 | "on_value": "true", 75 | "unique_serial": "1234567" 76 | } 77 | ] 78 | ``` 79 | 80 | #### Notes 81 | ##### Using the above configuration as an example: 82 | - The on.sh script executes when you turn on the accessory via a homekit app. (In this case we are executing a state script to determine on or off current state.) 83 | - The off.sh script executes when you turn off the accessory via a homekit app. ( In this case we are executing a state script to determine on or off current state.) 84 | - The state.sh script in this case would be executed to check current state. Insure that this script outputs to stdout the matching on value as configured by the on_value config parameter. If the on_value matches the on value output of this script then the accessory will be determined to be on. 85 | - The configured fileState file is not used in this example. Because it was not configured, the state script is being used. 86 | - The on_value in this case is used to match against the state script output. If the value matches the output of the state script, the accessory is determined to be on. 87 | 88 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | let Service; 2 | let Characteristic; 3 | 4 | const exec = require("child_process").exec; 5 | const fileExists = require("file-exists"); 6 | const chokidar = require("chokidar"); 7 | 8 | module.exports = function (homebridge) { 9 | Service = homebridge.hap.Service; 10 | Characteristic = homebridge.hap.Characteristic; 11 | homebridge.registerAccessory( 12 | "homebridge-script2", 13 | "Script2", 14 | script2Accessory 15 | ); 16 | }; 17 | 18 | function script2Accessory(log, config) { 19 | this.log = log; 20 | this.service = "Switch"; 21 | 22 | this.name = config["name"]; 23 | this.onCommand = config["on"]; 24 | this.offCommand = config["off"]; 25 | this.stateCommand = config["state"] || false; 26 | this.onValue = config["on_value"] || "true"; 27 | this.fileState = config["fileState"] || false; 28 | this.uniqueSerial = config["unique_serial"] || "script2 Serial Number"; 29 | this.onValue = this.onValue.trim().toLowerCase(); 30 | try { 31 | this.currentState = this.fileState 32 | ? fileExists.sync(this.fileState) 33 | : false; 34 | } catch (err) { 35 | this.log.error(`Error checking initial file state: ${err.message}`); 36 | this.currentState = false; 37 | } 38 | 39 | this.setStateHandler = function (powerOn, callback) { 40 | function setStateHandlerExecCallback(error, stdout, stderr) { 41 | if (error || stderr) { 42 | const errMessage = stderr 43 | ? `${stderr} (${error?.message ?? 'unknown error'})` 44 | : `${error?.message ?? 'unknown error'}`; 45 | this.log.error(`Set State returned an error: ${errMessage}`); 46 | callback(new Error(errMessage), null); 47 | return; 48 | } 49 | 50 | const commandOutput = stdout.trim().toLowerCase(); 51 | this.log.debug(`Set State Command returned ${commandOutput}`); 52 | 53 | this.currentState = powerOn; 54 | this.log.info(`Set ${this.name} to ${powerOn ? "ON" : "OFF"}`); 55 | 56 | callback(null, powerOn); 57 | } 58 | 59 | const command = powerOn ? this.onCommand : this.offCommand; 60 | this.log.debug(`Executing command: ${command}`); 61 | exec(command, setStateHandlerExecCallback.bind(this)); 62 | }; 63 | 64 | this.getStateHandler = function (callback) { 65 | function getStateHandlerExecCallback(error, stdout, stderr) { 66 | if (error || stderr) { 67 | const errMessage = stderr 68 | ? `${stderr} (${error.message})` 69 | : error.message; 70 | this.log.error(`Get State returned an error: ${errMessage}`); 71 | callback(new Error(errMessage), null); 72 | return; 73 | } 74 | 75 | const cleanCommandOutput = stdout.trim().toLowerCase(); 76 | this.log.debug(`Get State Command returned ${cleanCommandOutput}`); 77 | 78 | const poweredOn = cleanCommandOutput == this.onValue; 79 | this.log.info(`State of ${this.name} is: ${poweredOn ? "ON" : "OFF"}`); 80 | callback(null, poweredOn); 81 | } 82 | 83 | const command = this.stateCommand; 84 | this.log.debug(`Executing command: ${command}`); 85 | exec(command, getStateHandlerExecCallback.bind(this)); 86 | }; 87 | 88 | this.getFileStateHandler = function (callback) { 89 | try { 90 | const poweredOn = fileExists.sync(this.fileState); 91 | this.log.info(`State of ${this.name} is: ${poweredOn ? "ON" : "OFF"}`); 92 | callback(null, poweredOn); 93 | } catch (err) { 94 | this.log.error(`Error checking file state: ${err.message}`); 95 | callback(err, null); 96 | } 97 | }; 98 | } 99 | 100 | script2Accessory.prototype.setState = function (powerOn, callback) { 101 | this.log.info(`Setting ${this.name} to ${powerOn ? "ON" : "OFF"}...`); 102 | this.setStateHandler(powerOn, callback); 103 | }; 104 | 105 | script2Accessory.prototype.getState = function (callback) { 106 | this.log.info(`Getting ${this.name} state...`); 107 | if (this.fileState) { 108 | this.getFileStateHandler(callback); 109 | } else if (this.stateCommand) { 110 | this.getStateHandler(callback); 111 | } else { 112 | this.log.error("Must set config value for fileState or state."); 113 | } 114 | }; 115 | 116 | script2Accessory.prototype.getServices = function () { 117 | const informationService = new Service.AccessoryInformation(); 118 | const switchService = new Service.Switch(this.name); 119 | const theSerial = this.uniqueSerial.toString(); 120 | 121 | informationService 122 | .setCharacteristic(Characteristic.Manufacturer, "script2 Manufacturer") 123 | .setCharacteristic(Characteristic.Model, "script2 Model") 124 | .setCharacteristic(Characteristic.SerialNumber, theSerial); 125 | 126 | const characteristic = switchService 127 | .getCharacteristic(Characteristic.On) 128 | .on("set", this.setState.bind(this)); 129 | 130 | if (this.stateCommand || this.fileState) { 131 | characteristic.on("get", this.getState.bind(this)); 132 | } 133 | 134 | if (this.fileState) { 135 | const fileCreatedHandler = function (path, stats) { 136 | if (!this.currentState) { 137 | this.log.info(`File "${path}" was created`); 138 | this.currentState = true; 139 | switchService.setCharacteristic(Characteristic.On, true); 140 | } 141 | }.bind(this); 142 | 143 | const fileRemovedHandler = function (path, stats) { 144 | if (this.currentState) { 145 | this.log.info(`File "${path}" was deleted`); 146 | this.currentState = false; 147 | switchService.setCharacteristic(Characteristic.On, false); 148 | } 149 | }.bind(this); 150 | 151 | const watcher = chokidar.watch(this.fileState, { alwaysStat: true }); 152 | watcher.on("add", fileCreatedHandler); 153 | watcher.on("unlink", fileRemovedHandler); 154 | } 155 | return [informationService, switchService]; 156 | }; 157 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "homebridge-script2", 3 | "version": "0.3.2", 4 | "license": "MIT", 5 | "description": "script plugin for homebridge: https://github.com/nfarina/homebridge", 6 | "keywords": [ 7 | "homebridge-plugin", 8 | "homebridge-script2" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/pponce/homebridge-script2.git" 13 | }, 14 | "engines": { 15 | "homebridge": ">=0.2.0", 16 | "node": ">=6.0.0" 17 | }, 18 | "dependencies": { 19 | "object-assign": "^4.1.1", 20 | "file-exists": "^5.0.1", 21 | "chokidar": "^3.3.1" 22 | } 23 | } 24 | --------------------------------------------------------------------------------