Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motion Sensor #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,64 @@ In this version, I have made some changes from the older version. Mainly the plu
"access_token": "<<access token>>",
"cloudurl": "https://api.spark.io/v1/devices/",
"devices": [
{
"accessory": "BedroomLight",
"name": "Bedroom Light",
"deviceid": "<<device id>>",
"type": "LIGHT",
"function_name": "onoff",
"args": "0={STATE}"
},
{
"accessory": "KitchenLight",
"name": "Kitchen Light",
"deviceid": "<<device id>>",
"type": "LIGHT",
"function_name": "onoff",
"args": "1={STATE}"
},
{
"accessory": "KitchenTemperature",
"name": "Kitchen Temperature",
"deviceid": "<<device id>>",
"type": "SENSOR",
"sensorType": "temperature",
"key": "temperature",
"event_name": "tvalue"
}
{
"accessory": "ParticleLight",
"name": "Bedroom Light",
"deviceid": "<<device id>>",
"type": "LIGHT",
"function_name": "onoff",
"args": "0={STATE}"
},
{
"accessory": "ParticleTemperature",
"name": "Kitchen Temperature",
"deviceid": "<<device id>>",
"type": "SENSOR",
"sensorType": "temperature",
"key": "temperature",
"event_name": "HKSValues"
},
{
"accessory": "Motion Sensor",
"name": "Motion",
"deviceid": "<<device id>>",
"type": "SENSOR",
"sensorType": "motion",
"key": "motion",
"event_name": "HKSValues"
},
{
"accessory": “Humidity Sensor”,
"name": “Humidity",
"deviceid": "<<device id>>",
"type": “SENSOR”,
"sensorType": “humidity”,
"key": "humidity",
"event_name": “HKSValues"
},
{
"accessory": “Light Sensor”,
"name": “Light",
"deviceid": "<<device id>>",
"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.

25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

}
}
}

Expand Down
41 changes: 30 additions & 11 deletions sample-config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:39",
"username": "<<MAC ADDRESS>>",
"port": 51826,
"pin": "031-45-154"
},
Expand All @@ -23,24 +23,43 @@
"function_name": "onoff",
"args": "0={STATE}"
},
{
"accessory": "ParticleLight",
"name": "Kitchen Light",
"deviceid": "<<device id>>",
"type": "LIGHT",
"function_name": "onoff",
"args": "1={STATE}"
},
{
"accessory": "ParticleTemperature",
"name": "Kitchen Temperature",
"deviceid": "<<device id>>",
"type": "SENSOR",
"sensorType": "temperature",
"key": "temperature",
"event_name": "tvalue"
"event_name": "HKSValues"
},
{
"accessory": "Motion Sensor",
"name": "Motion",
"deviceid": "<<device id>>",
"type": "SENSOR",
"sensorType": "motion",
"key": "motion",
"event_name": "HKSValues"
},
{
"accessory": “Humidity Sensor”,
"name": “Humidity",
"deviceid": "<<device id>>",
"type": “SENSOR”,
"sensorType": “humidity”,
"key": "humidity",
"event_name": “HKSValues"
},
{
"accessory": “Light Sensor”,
"name": “Light",
"deviceid": "<<device id>>",
"type": “SENSOR”,
"sensorType": “light”,
"key": "light",
"event_name": “HKSValues"
}
]
}
]
}
}