Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
ptz0n committed Jan 23, 2017
0 parents commit 14c2019
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export PATH := $(shell npm bin):$(PATH)
SHELL := /bin/bash

.PHONY: release

release:
npm version minor || exit 1
npm publish || exit 1
git push origin master --tags
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# homebridge-verisure

This is a plugin for [homebridge](https://github.com/nfarina/homebridge). It's a
working implementation for several Verisure devices:

- [x] __VOICEBOX1__ - Temperature
- [x] __SMOKE2__ - Temperature
- [x] __SMARTPLUG__ - State, on, off

## Installation

```bash
npm install -g homebridge-verisure
```

Now you can update your configuration file to enable the plugin, see sample
snippet below.

## Configuration

As part of your configuration, add an object with your Verisure credentials to
your array (list) of enabled platform plugins.

```json
"platforms": [
{
"platform" : "verisure",
"name" : "Verisure",
"email": "[email protected]",
"password": "yourT0p5ecre7Passw0rd"
}
]
```
185 changes: 185 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
const verisure = require('verisure')


var Accessory, Service, Characteristic, UUIDGen;

const PLUGIN_NAME = 'homebridge-verisure';
const PLATFORM_NAME = 'verisure';
const MANUFACTURER = 'Verisure';

const DEVICE_TYPES = {
'VOICEBOX1': 'Directenhet',
'SMOKE2': 'Rökdetektor',
'SMARTPLUG': 'Smart plug'
}

let VERISURE_TOKEN = null;
let VERISURE_INSTALLATION = null;
let VERISURE_CALLS = {};


const getVerisureInstallation = function(config, callback) {
verisure.auth(config.email, config.password, function(err, token) {
if(err) return callback(err);
VERISURE_TOKEN = token;

verisure.installations(token, config.email, function(err, installations) {
if(err) return callback(err);
VERISURE_INSTALLATION = installations[0];
callback();
});
});
}

const getOverview = function(callback) {
if(typeof VERISURE_CALLS.overview == 'undefined') {
VERISURE_CALLS.overview = [];
}
VERISURE_CALLS.overview.push(callback);
if(VERISURE_CALLS.overview.length > 1) {
return;
}
verisure.overview(VERISURE_TOKEN, VERISURE_INSTALLATION, function(err, overview) {
VERISURE_CALLS.overview.map(function(callback) {
callback(err, overview);
});
VERISURE_CALLS.overview = [];
});
}


module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;

Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;

homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, VerisurePlatform, true);
}


const VerisurePlatform = function(log, config, api) {
var platform = this;
this.log = log;
this.config = config;

this.accessories = function(callback) {
getVerisureInstallation(config, function(err) {
if(err) return log(err);

verisure.overview(VERISURE_TOKEN, VERISURE_INSTALLATION, function(err, overview) {
if(err) return log(err);
var devices = overview.climateValues.map(function(device) {
return new VerisureAccessory(log, {
name: `${DEVICE_TYPES[device.deviceType]} (${device.deviceArea})`,
model: device.deviceType,
serialNumber: device.deviceLabel,
value: 0
});
});

devices = devices.concat(overview.smartPlugs.map(function(device) {
return new VerisureAccessory(log, {
name: `${DEVICE_TYPES.SMARTPLUG} (${device.area})`,
model: 'SMARTPLUG',
serialNumber: device.deviceLabel,
value: device.currentState == 'ON' ? 1 : 0
});
}));

callback(devices);
});
})
}
}


const VerisureAccessory = function(log, config) {
this.log = log;
this.config = config;

this.name = config.name;
this.model = config.model;
this.serialNumber = config.serialNumber;
this.value = config.value;
}


VerisureAccessory.prototype = {
_getCurrentTemperature: function(callback) {
this.log(`${this.name} (${this.serialNumber}): Getting current temperature...`);
var that = this;

getOverview(function(err, overview) {
if(err) return callback(err);
overview.climateValues.map(function(device) {
if(device.deviceLabel != that.serialNumber) return;
that.value = device.temperature;
callback(err, that.value);
});
});
},

_getSwitchValue: function(callback) {
this.log(`${this.name} (${this.serialNumber}): Getting current value...`);
var that = this;

getOverview(function(err, overview) {
if(err) return callback(err);
overview.smartPlugs.map(function(device) {
if(device.deviceLabel != that.serialNumber) return;
that.value = device.currentState == 'ON' ? 1 : 0;
callback(err, that.value);
});
});
},

_setSwitchValue: function(value, callback) {
this.log(`${this.name} (${this.serialNumber}): Setting current value to "${value}"...`);
this.value = value;

verisure._apiClient({
method: 'POST',
uri: `/installation/${VERISURE_INSTALLATION.giid}/smartplug/state`,
headers: {
'Cookie': `vid=${VERISURE_TOKEN}`,
'Accept': 'application/json, text/javascript, */*; q=0.01'
},
json: [
{
deviceLabel: this.serialNumber,
state: value == 1 ? true : false
}
]
}, callback);
},

getServices: function() {
var accessoryInformation = new Service.AccessoryInformation();
accessoryInformation
.setCharacteristic(Characteristic.Manufacturer, MANUFACTURER)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber)

var service = null;

if(this.model == 'SMARTPLUG') {
service = new Service.Switch(this.name);
service
.getCharacteristic(Characteristic.On)
.on('get', this._getSwitchValue.bind(this))
.on('set', this._setSwitchValue.bind(this))
.value = this.value;
}

if(['VOICEBOX1', 'SMOKE2'].includes(this.model)) {
service = new Service.TemperatureSensor(this.name);
service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this._getCurrentTemperature.bind(this));
}

return [accessoryInformation, service]
}
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "homebridge-verisure",
"version": "1.0.0",
"description": "Verisure plugin for homebridge: https://github.com/nfarina/homebridge",
"license": "MIT",
"keywords": [
"homebridge-plugin"
],
"repository": {
"type": "git",
"url": "git://github.com/ptz0n/homebridge-verisure.git"
},
"bugs": {
"url": "http://github.com/ptz0n/homebridge-verisure/issues"
},
"engines": {
"node": ">=0.12.0",
"homebridge": ">=0.2.0"
},
"dependencies": {
"verisure": "^1.1.0"
}
}

0 comments on commit 14c2019

Please sign in to comment.