homebridge-dummy 0.8.0 → 0.9.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.
package/README.md CHANGED
@@ -37,6 +37,21 @@ The default behavior of a dummy switch is to turn itself off one second after be
37
37
 
38
38
  ```
39
39
 
40
+ ## Dimmer Switches
41
+
42
+ By default, switches are toggle switches (on/off). You can configure your switch to be a dimmer switch, instead, storing a brightness value between 0 and 100. This can be done by passing the 'dimmer' argument in your config.json:
43
+
44
+ ```
45
+ "accessories": [
46
+ {
47
+ "accessory": "DummySwitch",
48
+ "name": "My Stateful Switch 1",
49
+ "dimmer": true
50
+ }
51
+ ]
52
+
53
+ ```
54
+
40
55
  ## Reverse Switches
41
56
 
42
57
  You may also want to create a dummy switch that turns itself on one second after being turned off. This can be done by passing the 'reverse' argument in your config.json:
@@ -16,6 +16,12 @@
16
16
  "default": false,
17
17
  "description": "The switch remains on instead of being automatically turned off."
18
18
  },
19
+ "dimmer": {
20
+ "title": "Dimmer",
21
+ "type": "boolean",
22
+ "default": false,
23
+ "description": "Make the switch a dimmer instead of a toggle (on/off) switch"
24
+ },
19
25
  "reverse": {
20
26
  "title": "Reverse",
21
27
  "type": "boolean",
@@ -40,6 +46,14 @@
40
46
  "default": false,
41
47
  "description": "The timer will reset each time the switch is turned on. "
42
48
  },
49
+ "brightness": {
50
+ "title": "Brightness",
51
+ "type": "integer",
52
+ "default": 0,
53
+ "placeholder": 0,
54
+ "maximum": 100,
55
+ "description": "Starting brightness (dimmer only)"
56
+ },
43
57
  "disableLogging": {
44
58
  "title": "DisableLogging",
45
59
  "type": "boolean",
package/index.js CHANGED
@@ -16,18 +16,28 @@ function DummySwitch(log, config) {
16
16
  this.log = log;
17
17
  this.name = config.name;
18
18
  this.stateful = config.stateful;
19
+ this.dimmer = config.dimmer;
20
+ this.brightness = config.brightness;
21
+ this.brightnessStorageKey = this.name + "Brightness";
19
22
  this.reverse = config.reverse;
20
23
  this.time = config.time ? config.time : 1000;
21
24
  this.resettable = config.resettable;
22
25
  this.timer = null;
23
26
  this.random = config.random;
24
27
  this.disableLogging = config.disableLogging;
25
- this._service = new Service.Switch(this.name);
28
+
29
+ if (this.dimmer) {
30
+ this._service = new Service.Lightbulb(this.name);
31
+ this.modelString = "Dummy Dimmer";
32
+ } else {
33
+ this._service = new Service.Switch(this.name);
34
+ this.modelString = "Dummy Switch";
35
+ }
26
36
 
27
37
  this.informationService = new Service.AccessoryInformation();
28
38
  this.informationService
29
39
  .setCharacteristic(Characteristic.Manufacturer, 'Homebridge')
30
- .setCharacteristic(Characteristic.Model, 'Dummy Switch')
40
+ .setCharacteristic(Characteristic.Model, this.modelString)
31
41
  .setCharacteristic(Characteristic.FirmwareRevision, HomebridgeDummyVersion)
32
42
  .setCharacteristic(Characteristic.SerialNumber, 'Dummy-' + this.name.replace(/\s/g, '-'));
33
43
 
@@ -37,6 +47,11 @@ function DummySwitch(log, config) {
37
47
 
38
48
  this._service.getCharacteristic(Characteristic.On)
39
49
  .on('set', this._setOn.bind(this));
50
+ if (this.dimmer) {
51
+ this._service.getCharacteristic(Characteristic.Brightness)
52
+ .on('get', this._getBrightness.bind(this))
53
+ .on('set', this._setBrightness.bind(this));
54
+ }
40
55
 
41
56
  if (this.reverse) this._service.setCharacteristic(Characteristic.On, true);
42
57
 
@@ -48,6 +63,17 @@ function DummySwitch(log, config) {
48
63
  this._service.setCharacteristic(Characteristic.On, true);
49
64
  }
50
65
  }
66
+
67
+ if (this.dimmer) {
68
+ var cachedBrightness = this.storage.getItemSync(this.brightnessStorageKey);
69
+ if ((cachedBrightness == undefined) || cachedBrightness == 0) {
70
+ this._service.setCharacteristic(Characteristic.On, false);
71
+ this._service.setCharacteristic(Characteristic.Brightness, 0);
72
+ } else {
73
+ this._service.setCharacteristic(Characteristic.On, true);
74
+ this._service.setCharacteristic(Characteristic.Brightness, cachedBrightness);
75
+ }
76
+ }
51
77
  }
52
78
 
53
79
  DummySwitch.prototype.getServices = function() {
@@ -58,6 +84,28 @@ function randomize(time) {
58
84
  return Math.floor(Math.random() * (time + 1));
59
85
  }
60
86
 
87
+ DummySwitch.prototype._getBrightness = function(callback) {
88
+
89
+ if ( ! this.disableLogging ) {
90
+ this.log("Getting " + "brightness: " + this.brightness);
91
+ }
92
+
93
+ callback(null, this.brightness);
94
+ }
95
+
96
+ DummySwitch.prototype._setBrightness = function(brightness, callback) {
97
+
98
+ if ( ! this.disableLogging ) {
99
+ var msg = "Setting brightness: " + brightness
100
+ this.log(msg);
101
+ }
102
+
103
+ this.brightness = brightness;
104
+ this.storage.setItemSync(this.brightnessStorageKey, brightness);
105
+
106
+ callback();
107
+ }
108
+
61
109
  DummySwitch.prototype._setOn = function(on, callback) {
62
110
 
63
111
  var delay = this.random ? randomize(this.time) : this.time;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-dummy",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge",
5
5
  "license": "ISC",
6
6
  "keywords": [