@vectronic/homebridge-script-switch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
package/.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager">
4
+ <output url="file://$PROJECT_DIR$/out" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/homebridge-script-switch.iml" filepath="$PROJECT_DIR$/.idea/homebridge-script-switch.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 vectronic
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.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # homebridge-script-switch
2
+
3
+ > A switch plugin for [Homebridge](https://homebridge.io) which integrates with shell scripts
4
+
5
+ ## Installation
6
+
7
+ 1. Install this plugin using the Homebridge Config UI X or via commandline `npm install -g @vectronic/homebridge-script-switch`
8
+ 1. Setup the plugin's configuration
9
+
10
+ ## Configuration
11
+
12
+ | Property | Description |
13
+ |---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
14
+ | name | The name for the accessory instance. |
15
+ | set_state_on_script | Script to set state ON, optional. If not provided, setting switch On from Home App is not possible. |
16
+ | set_state_on_script | Script to set state OFF, optional. If not provided, setting switch Off from Home App is not possible. |
17
+ | get_state_script | Script to get the current switch state. Should output on stdout text signifying "On" which matches `on_state_value` and anything else ot signify "Off". |
18
+ | on_state_value | Return value from get_state_script which corresponds to ON state. |
19
+
20
+ Example `config.json` entry:
21
+
22
+ ```json
23
+ "accessories": [
24
+ {
25
+ "accessory": "ScriptSwitch",
26
+ "name": "mySwitch",
27
+ "set_state_on_script": "/usr/local/bin/setMySwitchOn.sh",
28
+ "set_state_off_script": "/usr/local/bin/setMySwitchOff.sh",
29
+ "get_state_script": "/usr/local/bin/getMySwitchState.sh",
30
+ "on_state_value": "ON"
31
+ }
32
+ ]
33
+ ```
34
+
35
+ ## Help
36
+
37
+ If you have a query or problem, raise an issue in GitHub, or better yet submit a PR!
@@ -0,0 +1,36 @@
1
+ {
2
+ "pluginAlias": "ScriptSwitch",
3
+ "pluginType": "accessory",
4
+ "singular": false,
5
+ "schema": {
6
+ "type": "object",
7
+ "required": [
8
+ "name",
9
+ "get_state_script",
10
+ "on_state_value"
11
+ ],
12
+ "additionalProperties": true,
13
+ "properties": {
14
+ "name": {
15
+ "type": "string",
16
+ "description": "Name of the accessory instance."
17
+ },
18
+ "set_state_on_script": {
19
+ "type": "string",
20
+ "description": "Script to set state ON, optional."
21
+ },
22
+ "set_state_off_script": {
23
+ "type": "string",
24
+ "description": "Script to set state OFF, optional."
25
+ },
26
+ "get_state_script": {
27
+ "type": "string",
28
+ "description": "Script to get current state."
29
+ },
30
+ "on_state_value": {
31
+ "type": "string",
32
+ "description": "Return value from get_state_script which corresponds to ON state."
33
+ }
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,4 @@
1
+ import { API } from 'homebridge';
2
+ declare const _default: (api: API) => void;
3
+ export = _default;
4
+ //# sourceMappingURL=accessory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accessory.d.ts","sourceRoot":"","sources":["../src/accessory.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,GAAG,EAQN,MAAM,YAAY,CAAC;8BAgIL,GAAG;AAAlB,kBAGE"}
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ const child_process_1 = require("child_process");
3
+ let hap;
4
+ class ScriptSwitch {
5
+ constructor(log, config) {
6
+ this.log = log;
7
+ this.name = config.name;
8
+ this.setOnStateScript = config.set_state_on_script;
9
+ this.setOffStateScript = config.set_state_off_script;
10
+ this.getStateScript = config.get_state_script || '/usr/bin/python';
11
+ this.onStateValue = config.on_state_value;
12
+ if (this.setOnStateScript !== undefined) {
13
+ this.log(`setOnStateScript: ${this.setOnStateScript}`);
14
+ }
15
+ if (this.setOffStateScript !== undefined) {
16
+ this.log(`setOffStateScript: ${this.setOffStateScript}`);
17
+ }
18
+ this.log(`getStateScript: ${this.getStateScript}`);
19
+ this.log(`onStateValue: ${this.onStateValue}`);
20
+ this.switchService = new hap.Service.Switch(this.name);
21
+ this.switchService.getCharacteristic(hap.Characteristic.On)
22
+ .on("get" /* CharacteristicEventTypes.GET */, async (callback) => {
23
+ let on;
24
+ try {
25
+ const stdout = await new Promise((resolve, reject) => {
26
+ (0, child_process_1.exec)(this.getStateScript, {}, (error, stdout) => {
27
+ if (error) {
28
+ reject(error);
29
+ return;
30
+ }
31
+ resolve(stdout);
32
+ });
33
+ });
34
+ log.debug('set state script returned: ' + stdout);
35
+ on = (stdout === this.onStateValue);
36
+ }
37
+ catch (err) {
38
+ log.error(`get state exec error: ${err}`);
39
+ callback(err);
40
+ return;
41
+ }
42
+ log.debug('Returning switch state: ' + (on ? 'ON' : 'OFF'));
43
+ callback(undefined, on);
44
+ })
45
+ .on("set" /* CharacteristicEventTypes.SET */, async (value, callback) => {
46
+ const newOn = value;
47
+ if (newOn && (this.setOnStateScript === undefined)) {
48
+ log.debug('Ignoring switch state ON as set_state_on_script is not configured');
49
+ callback();
50
+ return;
51
+ }
52
+ if (!newOn && (this.setOffStateScript === undefined)) {
53
+ log.debug('Ignoring switch state OFF as set_state_off_script is not configured');
54
+ callback();
55
+ return;
56
+ }
57
+ log.debug('Setting switch state: ' + (newOn ? 'ON' : 'OFF'));
58
+ const setStateScript = (newOn ? this.setOnStateScript : this.setOffStateScript);
59
+ try {
60
+ const stdout = await new Promise((resolve, reject) => {
61
+ (0, child_process_1.exec)(setStateScript, {}, (error, stdout) => {
62
+ if (error) {
63
+ reject(error);
64
+ return;
65
+ }
66
+ resolve(stdout);
67
+ });
68
+ });
69
+ log.debug('set state script returned: ' + stdout);
70
+ }
71
+ catch (err) {
72
+ log.error(`set state exec error: ${err}`);
73
+ callback(err);
74
+ return;
75
+ }
76
+ log.debug('Switch state set to: ' + (newOn ? 'ON' : 'OFF'));
77
+ callback();
78
+ });
79
+ this.informationService = new hap.Service.AccessoryInformation()
80
+ .setCharacteristic(hap.Characteristic.Manufacturer, 'Vectronic');
81
+ log.info('Script switch finished initializing!');
82
+ }
83
+ identify() {
84
+ this.log('Identify!');
85
+ }
86
+ getServices() {
87
+ return [
88
+ this.informationService,
89
+ this.switchService
90
+ ];
91
+ }
92
+ }
93
+ module.exports = (api) => {
94
+ hap = api.hap;
95
+ api.registerAccessory('ScriptSwitch', ScriptSwitch);
96
+ };
97
+ //# sourceMappingURL=accessory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accessory.js","sourceRoot":"","sources":["../src/accessory.ts"],"names":[],"mappings":";AAYA,iDAAqC;AAErC,IAAI,GAAQ,CAAC;AAGb,MAAM,YAAY;IAad,YAAY,GAAY,EAAE,MAAuB;QAC7C,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QAExB,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,mBAAmB,CAAC;QACnD,IAAI,CAAC,iBAAiB,GAAG,MAAM,CAAC,oBAAoB,CAAC;QACrD,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,gBAAgB,IAAI,iBAAiB,CAAC;QACnE,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC;QAE1C,IAAI,IAAI,CAAC,gBAAgB,KAAK,SAAS,EAAE;YACrC,IAAI,CAAC,GAAG,CAAC,qBAAqB,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC;SAC1D;QACD,IAAI,IAAI,CAAC,iBAAiB,KAAK,SAAS,EAAE;YACtC,IAAI,CAAC,GAAG,CAAC,sBAAsB,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;SAC5D;QACD,IAAI,CAAC,GAAG,CAAC,mBAAmB,IAAI,CAAC,cAAc,EAAE,CAAC,CAAC;QACnD,IAAI,CAAC,GAAG,CAAC,iBAAiB,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC;QAE/C,IAAI,CAAC,a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EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEP,IAAI,CAAC,kBAAkB,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,oBAAoB,EAAE;aAC3D,iBAAiB,CAAC,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;QAErE,GAAG,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAC;IACrD,CAAC;IAED,QAAQ;QACJ,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAC1B,CAAC;IAED,WAAW;QACP,OAAO;YACH,IAAI,CAAC,kBAAkB;YACvB,IAAI,CAAC,aAAa;SACrB,CAAC;IACN,CAAC;CACJ;AAKD,iBAAS,CAAC,GAAQ,EAAE,EAAE;IAClB,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;IACd,GAAG,CAAC,iBAAiB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;AACxD,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "displayName": "Homebridge Script Switch",
3
+ "name": "@vectronic/homebridge-script-switch",
4
+ "version": "0.1.0",
5
+ "description": "A switch plugin for Homebridge which integrates with shell scripts",
6
+ "main": "dist/accessory.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git://github.com/vectronic/homebridge-script-switch.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/vectronic/homebridge-script-switch/issues"
13
+ },
14
+ "scripts": {
15
+ "lint": "eslint src/**.ts",
16
+ "build": "rimraf ./dist && tsc",
17
+ "prepublishOnly": "npm run lint && npm run build"
18
+ },
19
+ "keywords": [
20
+ "homebridge-plugin"
21
+ ],
22
+ "author": "vectronic",
23
+ "license": "MIT",
24
+ "engines": {
25
+ "node": ">=14",
26
+ "homebridge": ">=1.1.0"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "20.1.1",
30
+ "@typescript-eslint/eslint-plugin": "5.59.5",
31
+ "@typescript-eslint/parser": "5.59.5",
32
+ "eslint": "8.40.0",
33
+ "homebridge": "1.6.1",
34
+ "rimraf": "5.0.0",
35
+ "ts-node": "10.9.1",
36
+ "typescript": "5.0.4"
37
+ }
38
+ }