homebridge-sensor-cmd-polling 2.50.7

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Alex 'apexad' Martin
4
+ Copyright (c) 2025 Quentin Ulmer (Polling Support Fork)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # homebridge-sensor-cmd-polling
2
+
3
+ [![MIT license](https://badgen.net/badge/license/MIT/red)](https://github.com/quebulm/homebridge-sensor-cmd-polling/blob/master/LICENSE)
4
+
5
+ ✅ **This is a fork of [apexad/homebridge-sensor-cmd](https://github.com/apexad/homebridge-sensor-cmd)**
6
+ ➡️ **Extended with configurable automatic polling support**
7
+
8
+ ---
9
+
10
+ ## What is this?
11
+
12
+ Originally, [homebridge-sensor-cmd](https://github.com/apexad/homebridge-sensor-cmd) was a **minimalistic Homebridge plugin** (about 50 lines of code) to create a Contact/Motion/Occupancy Sensor based on the output of a shell command (returning `1` or `0`).
13
+
14
+ This fork keeps all original functionality but **adds automatic polling**.
15
+ Now, you can define a `pollingInterval` in the config so the plugin updates the sensor state automatically without waiting for HomeKit GET requests.
16
+
17
+ ---
18
+
19
+ ## New Feature: Automatic Polling
20
+
21
+ With this fork you can now add:
22
+
23
+ ```json
24
+ "pollingInterval": 30
25
+ ```
26
+
27
+ This will poll the command every **30 seconds** and update the sensor state automatically in HomeKit.
28
+
29
+ If you set `pollingInterval` to `0` or omit it, the behavior is identical to the original plugin (only updates on GET requests).
30
+
31
+ ---
32
+
33
+ ## Configuration
34
+
35
+ The easiest way to use this plugin is via [homebridge-config-ui-x](https://www.npmjs.com/package/homebridge-config-ui-x).
36
+
37
+ Add this to the `accessories` section of Homebridge's `config.json` after installing the plugin:
38
+
39
+ ```json
40
+ {
41
+ "accessory": "SensorCmd",
42
+ "name": "<name of the sensor>",
43
+ "type": "<sensor type: contact/motion/occupancy - default is contact>",
44
+ "command": "<command-line/shell command to execute>",
45
+ "pollingInterval": 30
46
+ }
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Original Author
52
+
53
+ - Original minimal version: [Alex 'apexad' Martin](https://github.com/apexad/homebridge-sensor-cmd)
54
+
55
+ ## Fork Maintainer
56
+
57
+ - Fork with polling support: [Quentin Ulmer](https://github.com/quebulm/homebridge-sensor-cmd-polling)
58
+
59
+ ---
60
+
61
+ ## Installation
62
+
63
+ ```bash
64
+ npm install -g homebridge-sensor-cmd-polling
65
+ ```
66
+
67
+ ---
68
+
69
+ ## License
70
+
71
+ MIT same as the original.
@@ -0,0 +1,33 @@
1
+ {
2
+ "pluginAlias": "SensorCmd",
3
+ "pluginType": "accessory",
4
+ "singular": false,
5
+ "schema": {
6
+ "type": "object",
7
+ "properties": {
8
+ "name": {
9
+ "title": "Name",
10
+ "type": "string",
11
+ "required": true
12
+ },
13
+ "type": {
14
+ "title": "Sensor Type",
15
+ "type": "string",
16
+ "default": "Contact",
17
+ "oneOf": [
18
+ { "title": "Contact", "enum": ["contact"] },
19
+ { "title": "Motion", "enum": ["motion"] },
20
+ { "title": "Occupancy", "enum": ["occupancy"] }
21
+ ],
22
+ "required": true
23
+ },
24
+ "command": {
25
+ "title": "Command",
26
+ "type": "string",
27
+ "required": true
28
+ }
29
+ }
30
+ },
31
+ "form": null,
32
+ "display": null
33
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ const child_process_1 = require("child_process");
3
+ let hap;
4
+ class SensorCmd {
5
+ constructor(log, config) {
6
+ this.log = log;
7
+ this.config = config;
8
+ // Determine which sensor type to create
9
+ switch (this.config.type) {
10
+ case "motion":
11
+ this.sensorService = new hap.Service.MotionSensor(this.config.name);
12
+ break;
13
+ case "occupancy":
14
+ this.sensorService = new hap.Service.OccupancySensor(this.config.name);
15
+ break;
16
+ case "contact":
17
+ default:
18
+ this.sensorService = new hap.Service.ContactSensor(this.config.name);
19
+ }
20
+ // Register the on-demand GET handler
21
+ const characteristic = this.getRelevantCharacteristic();
22
+ characteristic.on("get" /* GET */, (callback) => {
23
+ this.runCommand((err, value) => callback(null, err ? 0 : value));
24
+ });
25
+ // Accessory info
26
+ this.infoService = new hap.Service.AccessoryInformation()
27
+ .setCharacteristic(hap.Characteristic.Manufacturer, 'apexad')
28
+ .setCharacteristic(hap.Characteristic.Model, 'sensor-cmd');
29
+ // Polling interval (default 60s)
30
+ this.pollingInterval = this.config.pollingInterval ? this.config.pollingInterval : 60;
31
+ if (this.pollingInterval > 0) {
32
+ this.log(`Starting automatic polling every ${this.pollingInterval}s`);
33
+ this.startPolling();
34
+ }
35
+ }
36
+ /** Get the correct characteristic based on sensor type */
37
+ getRelevantCharacteristic() {
38
+ switch (this.config.type) {
39
+ case "motion":
40
+ return this.sensorService.getCharacteristic(hap.Characteristic.MotionDetected);
41
+ case "occupancy":
42
+ return this.sensorService.getCharacteristic(hap.Characteristic.OccupancyDetected);
43
+ case "contact":
44
+ default:
45
+ return this.sensorService.getCharacteristic(hap.Characteristic.ContactSensorState);
46
+ }
47
+ }
48
+ /** Run the configured command and return parsed result */
49
+ runCommand(callback) {
50
+ child_process_1.exec(this.config.command, (err, stdout) => {
51
+ if (err) {
52
+ this.log(`Command failed: ${err.message}`);
53
+ callback(true, 0);
54
+ return;
55
+ }
56
+ let parsed = parseInt(stdout.trim(), 10);
57
+ if (isNaN(parsed))
58
+ parsed = 0;
59
+ callback(false, parsed);
60
+ });
61
+ }
62
+ /** Start polling periodically */
63
+ startPolling() {
64
+ const characteristic = this.getRelevantCharacteristic();
65
+ const poll = () => {
66
+ this.runCommand((err, value) => {
67
+ if (!err) {
68
+ this.log(`Polled value: ${value}`);
69
+ characteristic.updateValue(value);
70
+ }
71
+ else {
72
+ this.log(`Polling error, keeping previous value`);
73
+ }
74
+ });
75
+ };
76
+ // Initial poll
77
+ poll();
78
+ // Set recurring interval
79
+ this.pollingTimer = setInterval(poll, this.pollingInterval * 1000);
80
+ }
81
+ /** Clean up when accessory is unloaded */
82
+ shutdown() {
83
+ if (this.pollingTimer) {
84
+ clearInterval(this.pollingTimer);
85
+ this.pollingTimer = undefined;
86
+ }
87
+ }
88
+ getServices() {
89
+ return [this.infoService, this.sensorService];
90
+ }
91
+ }
92
+ module.exports = (api) => {
93
+ hap = api.hap;
94
+ api.registerAccessory('SensorCmd', SensorCmd);
95
+ };
96
+ //# sourceMappingURL=accessory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accessory.js","sourceRoot":"","sources":["../src/accessory.ts"],"names":[],"mappings":";AAUA,iDAAqC;AAErC,IAAI,GAAQ,CAAC;AAOb,MAAM,SAAS;IAMX,YAA6B,GAAY,EAAmB,MAAuB;QAAtD,QAAG,GAAH,GAAG,CAAS;QAAmB,WAAM,GAAN,MAAM,CAAiB;QAC/E,wCAAwC;QACxC,QAAQ,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;YACtB,KAAK,QAAQ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AK,EAAE,EAAE;gBAC3B,IAAI,CAAC,GAAG,EAAE;oBACN,IAAI,CAAC,GAAG,CAAC,iBAAiB,KAAK,EAAE,CAAC,CAAC;oBACnC,cAAc,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACrC;qBAAM;oBACH,IAAI,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;iBACrD;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;QAEF,eAAe;QACf,IAAI,EAAE,CAAC;QAEP,yBAAyB;QACzB,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;IACvE,CAAC;IAED,0CAA0C;IACnC,QAAQ;QACX,IAAI,IAAI,CAAC,YAAY,EAAE;YACnB,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;YACjC,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;SACjC;IACL,CAAC;IAED,WAAW;QACP,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IAClD,CAAC;CACJ;AA1GD,iBAAS,CAAC,GAAQ,EAAE,EAAE;IAClB,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;IACd,GAAG,CAAC,iBAAiB,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;AAClD,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "homebridge-sensor-cmd-polling",
3
+ "version": "2.50.7",
4
+ "description": "Fork of apexad/homebridge-sensor-cmd with automatic polling support (configurable interval) – original minimal contact sensor via shell command",
5
+ "main": "dist/accessory.js",
6
+ "scripts": {
7
+ "clean": "rimraf ./dist",
8
+ "build": "rimraf ./dist && tsc",
9
+ "prepublishOnly": "npm run build",
10
+ "postpublish": "npm run clean",
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "author": {
14
+ "name": "Quentin Ulmer (forked from Alex 'apexad' Martin)"
15
+ },
16
+ "contributors": [
17
+ {
18
+ "name": "Alex 'apexad' Martin",
19
+ "url": "https://github.com/apexad/homebridge-sensor-cmd"
20
+ },
21
+ {
22
+ "name": "Quentin Ulmer",
23
+ "url": "https://github.com/quebulm/homebridge-sensor-cmd-polling"
24
+ }
25
+ ],
26
+ "engines": {
27
+ "node": ">=12.0.0",
28
+ "homebridge": ">=1.0.0"
29
+ },
30
+ "keywords": [
31
+ "homebridge-plugin",
32
+ "sensor",
33
+ "polling",
34
+ "contact-sensor",
35
+ "motion-sensor",
36
+ "occupancy-sensor"
37
+ ],
38
+ "license": "MIT",
39
+ "files": [
40
+ "LICENSE",
41
+ "dist",
42
+ "config.schema.json"
43
+ ],
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "git://github.com/quebulm/homebridge-sensor-cmd-polling.git"
47
+ },
48
+ "bugs": {
49
+ "url": "https://github.com/quebulm/homebridge-sensor-cmd-polling/issues"
50
+ },
51
+ "homepage": "https://github.com/quebulm/homebridge-sensor-cmd-polling#readme",
52
+ "devDependencies": {
53
+ "@types/node": "^20.0.0",
54
+ "homebridge": "^1.6.0",
55
+ "rimraf": "^3.0.2",
56
+ "typescript": "^5.4.0"
57
+ }
58
+ }