From 20d37348c0141d8bf86535764c8121fb1e09020c Mon Sep 17 00:00:00 2001 From: pedroserano Date: Sat, 2 Dec 2017 16:59:26 -0500 Subject: [PATCH 1/3] Added Motion Sensor --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1e26766..4e7870f 100644 --- a/index.js +++ b/index.js @@ -96,6 +96,15 @@ function ParticleAccessory(log, url, access_token, device) { .getCharacteristic(Characteristic.CurrentRelativeHumidity) .on('get', this.getDefaultValue.bind(this)); } + else if(this.sensorType.toLowerCase() === "motion"){ + console.log("Motion Sensor"); + + service = new Service.MotionSensor(this.name); + + service + .getCharacteristic(Characteristic.MotionDetected) + .on('get', this.getDefaultValue.bind(this)); + } else if(this.sensorType.toLowerCase() === "light"){ console.log("Light Sensor"); @@ -124,7 +133,7 @@ function ParticleAccessory(log, url, access_token, device) { this.services.push(service); } - console.log("Servie Count: " + this.services.length); + console.log("Service Count: " + this.services.length); } } @@ -188,6 +197,20 @@ ParticleAccessory.prototype.processEventData = function(e){ .getCharacteristic(Characteristic.CurrentAmbientLightLevel) .setValue(parseFloat(tokens[1])); } + else if (tokens[0].toLowerCase() === "motion") { + this.value = parseFloat(tokens[1]); + this.log('Received ' + this.value); + if (this.value === '1.00' || this.value === 1.00 || this.value === 'true' || this.value === 'TRUE') this.value = true; + else if (this.value === '0.00' || this.value === 0.00 || this.value === 'false' || this.value === 'FALSE') this.value = false; + if (this.value !== true && this.value !== false) { + this.log('Received value is not valid.'); + } else { + this.services[1] + .getCharacteristic(Characteristic.MotionDetected) + .setValue(this.value); + } + + } } } From 6db2f4c26a4fa815295269b0898d54bbda88b2f3 Mon Sep 17 00:00:00 2001 From: pedroserano Date: Sat, 2 Dec 2017 17:01:40 -0500 Subject: [PATCH 2/3] added motion sensor config example --- sample-config.json | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/sample-config.json b/sample-config.json index 5fe7712..2aad964 100644 --- a/sample-config.json +++ b/sample-config.json @@ -1,7 +1,7 @@ { "bridge": { "name": "Homebridge", - "username": "CC:22:3D:E3:CE:39", + "username": "<>", "port": 51826, "pin": "031-45-154" }, @@ -23,14 +23,6 @@ "function_name": "onoff", "args": "0={STATE}" }, - { - "accessory": "ParticleLight", - "name": "Kitchen Light", - "deviceid": "<>", - "type": "LIGHT", - "function_name": "onoff", - "args": "1={STATE}" - }, { "accessory": "ParticleTemperature", "name": "Kitchen Temperature", @@ -38,9 +30,36 @@ "type": "SENSOR", "sensorType": "temperature", "key": "temperature", - "event_name": "tvalue" + "event_name": "HKSValues" + }, + { + "accessory": "Motion Sensor", + "name": "Motion", + "deviceid": "<>", + "type": "SENSOR", + "sensorType": "motion", + "key": "motion", + "event_name": "HKSValues" + }, + { + "accessory": “Humidity Sensor”, + "name": “Humidity", + "deviceid": "<>", + "type": “SENSOR”, + "sensorType": “humidity”, + "key": "humidity", + "event_name": “HKSValues" + }, + { + "accessory": “Light Sensor”, + "name": “Light", + "deviceid": "<>", + "type": “SENSOR”, + "sensorType": “light”, + "key": "light", + "event_name": “HKSValues" } ] } ] -} \ No newline at end of file +} From 58fed93892cf4160e47acababc97a894e1857634 Mon Sep 17 00:00:00 2001 From: pedroserano Date: Sat, 2 Dec 2017 17:03:48 -0500 Subject: [PATCH 3/3] Added config examples to readme --- README.md | 77 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b9de2c7..17e0e72 100644 --- a/README.md +++ b/README.md @@ -28,45 +28,64 @@ In this version, I have made some changes from the older version. Mainly the plu "access_token": "<>", "cloudurl": "https://api.spark.io/v1/devices/", "devices": [ - { - "accessory": "BedroomLight", - "name": "Bedroom Light", - "deviceid": "<>", - "type": "LIGHT", - "function_name": "onoff", - "args": "0={STATE}" - }, - { - "accessory": "KitchenLight", - "name": "Kitchen Light", - "deviceid": "<>", - "type": "LIGHT", - "function_name": "onoff", - "args": "1={STATE}" - }, - { - "accessory": "KitchenTemperature", - "name": "Kitchen Temperature", - "deviceid": "<>", - "type": "SENSOR", - "sensorType": "temperature", - "key": "temperature", - "event_name": "tvalue" - } + { + "accessory": "ParticleLight", + "name": "Bedroom Light", + "deviceid": "<>", + "type": "LIGHT", + "function_name": "onoff", + "args": "0={STATE}" + }, + { + "accessory": "ParticleTemperature", + "name": "Kitchen Temperature", + "deviceid": "<>", + "type": "SENSOR", + "sensorType": "temperature", + "key": "temperature", + "event_name": "HKSValues" + }, + { + "accessory": "Motion Sensor", + "name": "Motion", + "deviceid": "<>", + "type": "SENSOR", + "sensorType": "motion", + "key": "motion", + "event_name": "HKSValues" + }, + { + "accessory": “Humidity Sensor”, + "name": “Humidity", + "deviceid": "<>", + "type": “SENSOR”, + "sensorType": “humidity”, + "key": "humidity", + "event_name": “HKSValues" + }, + { + "accessory": “Light Sensor”, + "name": “Light", + "deviceid": "<>", + "type": “SENSOR”, + "sensorType": “light”, + "key": "light", + "event_name": “HKSValues" + } ] } ] } -As you can see from the above example this `config.json` file defines 3 accessories. 2 Lights and one Temperature Sensor. The **access_token** defines the Particle Access Token and **cloudurl** defines the base Particle API url. If you are using the Particle Cloud, then the value of *cloudurl* should be https://api.spark.io/v1/devices/. If you are using local cloud, then replace with your sensor address. +As you can see from the above example this `config.json` file defines 5 accessories. A Light, and 4 sensors: temperature, humidity, light, and motion. The **access_token** defines the Particle Access Token and **cloudurl** defines the base Particle API url. If you are using the Particle Cloud, then the value of *cloudurl* should be https://api.spark.io/v1/devices/. If you are using local cloud, then replace with your sensor address. The `devices` array contains all the accessories. You can see the accessory object defines following string objects: - ***accessory*** - Accessory name, this is the name of the accessory. - ***name*** - Display name, this is the name to be displayed on the HomeKit app. - ***deviceid*** - Device ID of the Particle Device (Core, Photon or Electron). It is defined in accessory so that you can use different Particle Devices for different accessory. - - ***type*** - Type of the accessoy. As of now, the plugin supports 2 type, LIGHT and SENSOR. Type LIGHT represents a light, such as bedroom light, kitchen light, living room light, etc... Type SENSOR represents sensor accessory such as Temperature sensor, Humidity sensor, Light sensor, etc... - - ***sensorType*** - Optional Sensor Type, this string object is optional. This is only valid when the accessory type is SENSOR. As of now the plugin supports 3 types of sensors, Temperature Sensor, Humidity Sensor and Light Sensor. More sensor will be supports in future versions. - - ***event_name*** - The name of the event to listen for sensor value update. This is only valid if the accessory type is SENSOR. If the accessory is a type of SENSOR, then the plugin listens for events published from Particle Device (using `Particle.publish`). The device firmware should publish the sensor values in the format `key=value`. The key identifies the sensor value. For a temperature sensor the key should be ***temperature***. For a humidity sensor the key should be ***humidity***. For light sensor it should be ***light***. + - ***type*** - Type of the accessoy. As of now, the plugin supports 2 type, LIGHT and SENSOR. Type LIGHT represents a light, such as bedroom light, kitchen light, living room light, etc... Type SENSOR represents sensor accessory such as Temperature sensor, Humidity sensor, Light sensor, Motion Sensor, etc... + - ***sensorType*** - Optional Sensor Type, this string object is optional. This is only valid when the accessory type is SENSOR. As of now the plugin supports 4 types of sensors, Temperature Sensor, Humidity Sensor, Motion Sensor, and Light Sensor. More sensor will be supports in future versions. + - ***event_name*** - The name of the event to listen for sensor value update. This is only valid if the accessory type is SENSOR. If the accessory is a type of SENSOR, then the plugin listens for events published from Particle Device (using `Particle.publish`). The device firmware should publish the sensor values in the format `key=value`. The key identifies the sensor value. For a temperature sensor the key should be ***temperature***. For a humidity sensor the key should be ***humidity***. For light sensor it should be ***light***. For motion sensor it should be ***motion***. - ***key*** - Name of the key, this is not used in this version of the plugin. This is included for future purpose.