Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Add sensor support #52

Merged
merged 6 commits into from
Oct 26, 2016
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Here's a list of the devices that are currently exposed:
* **Rollershutter** - exposed as a garage door
* **Fan** - on/off/speed
* **Input boolean** - on/off
* **Sensors** - temperature, light and humidity sensors

### Scene Support

Expand Down Expand Up @@ -68,7 +69,7 @@ adding it to your `config.json`.
"name": "HomeAssistant",
"host": "http://192.168.1.16:8123",
"password": "yourapipassword",
"supported_types": ["fan", "garage_door", "input_boolean", "light", "lock", "media_player", "rollershutter", "scene", "switch"]
"supported_types": ["fan", "garage_door", "input_boolean", "light", "lock", "media_player", "rollershutter", "scene", "sensor", "switch"]
}
]
```
Expand Down
108 changes: 108 additions & 0 deletions accessories/sensor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var Service, Characteristic, communicationError;

module.exports = function (oService, oCharacteristic, oCommunicationError) {
Service = oService;
Characteristic = oCharacteristic;
communicationError = oCommunicationError;

return HomeAssistantSensorFactory;
};

function HomeAssistantSensorFactory(log, data, client) {
if (!data.attributes) {
return null;
}
var service, characteristic, transformData;
if (data.attributes.unit_of_measurement === '°C' || data.attributes.unit_of_measurement === '°F') {
service = Service.TemperatureSensor;
characteristic = Characteristic.CurrentTemperature;
transformData = function(data) {
var value = parseFloat(data.state);
// HomeKit only works with Celsius internally
if (data.attributes.unit_of_measurement === '°F') {
value = (value - 32) / 1.8;
}
return value;
};
} else if (data.attributes.unit_of_measurement === "%" && data.entity_id.includes("humidity")) {
service = Service.HumiditySensor;
characteristic = Characteristic.CurrentRelativeHumidity;
} else if (data.attributes.unit_of_measurement === "lux") {
service = Service.LightSensor;
characteristic = Characteristic.CurrentAmbientLightLevel;
transformData = function(data) {
return Math.max(0.0001, parseFloat(data.state));
};
} else {
return null;
}

return new HomeAssistantSensor(log, data, client, service, characteristic, transformData);
}

class HomeAssistantSensor {
constructor(log, data, client, service, characteristic, transformData) {
// device info
this.data = data;
this.entity_id = data.entity_id;
if (data.attributes && data.attributes.friendly_name) {
this.name = data.attributes.friendly_name;
}else{
this.name = data.entity_id.split('.').pop().replace(/_/g, ' ');
}

this.entity_type = data.entity_id.split('.')[0];

this.client = client;
this.log = log;

this.service = service;
this.characteristic = characteristic;
if (transformData) {
this.transformData = transformData;
}
}

transformData(data) {
return parseFloat(data.state);
}

onEvent(old_state, new_state) {
this.sensorService.getCharacteristic(this.characteristic)
.setValue(this.transformData(new_state), null, 'internal');
}

identify(callback){
this.log("identifying: " + this.name);
callback();
}

getState(callback){
this.log("fetching state for: " + this.name);
this.client.fetchState(this.entity_id, function(data) {
if (data) {
callback(null, this.transformData(data));
}else{
callback(communicationError);
}
}.bind(this));
}

getServices() {
this.sensorService = new this.service();
var informationService = new Service.AccessoryInformation();

informationService
.setCharacteristic(Characteristic.Manufacturer, "Home Assistant")
.setCharacteristic(Characteristic.Model, "Sensor")
.setCharacteristic(Characteristic.SerialNumber, "xxx");

this.sensorService
.getCharacteristic(this.characteristic)
.on('get', this.getState.bind(this));

return [informationService, this.sensorService];
}
}

module.exports.HomeAssistantSensorFactory = HomeAssistantSensorFactory;
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var HomeAssistantGarageDoor;
var HomeAssistantMediaPlayer;
var HomeAssistantRollershutter;
var HomeAssistantFan;
var HomeAssistantSensorFactory;

module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
Expand All @@ -27,6 +28,7 @@ module.exports = function(homebridge) {
HomeAssistantRollershutter = require('./accessories/rollershutter')(Service, Characteristic, communicationError);
HomeAssistantMediaPlayer = require('./accessories/media_player')(Service, Characteristic, communicationError);
HomeAssistantFan = require('./accessories/fan')(Service, Characteristic, communicationError);
HomeAssistantSensorFactory = require('./accessories/sensor')(Service, Characteristic, communicationError);

homebridge.registerPlatform("homebridge-homeassistant", "HomeAssistant", HomeAssistantPlatform, false);
}
Expand Down Expand Up @@ -139,8 +141,6 @@ HomeAssistantPlatform.prototype = {
setTimeout(function() { that.accessories(callback); }, 5000);
return;
}
// that.log(response)
// that.log(data)

for (var i = 0; i < data.length; i++) {
entity = data[i]
Expand Down Expand Up @@ -186,6 +186,8 @@ HomeAssistantPlatform.prototype = {
accessory = new HomeAssistantSwitch(that.log, entity, that, 'input_boolean')
}else if (entity_type == 'fan'){
accessory = new HomeAssistantFan(that.log, entity, that)
}else if (entity_type == 'sensor'){
accessory = HomeAssistantSensorFactory(that.log, entity, that)
}

if (accessory) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"url": "http://github.com/home-assistant/homebridge-homeassistant/issues"
},
"engines": {
"node": ">=0.12.0",
"node": ">=4.3.2",
"homebridge": ">=0.3.0"
},
"dependencies": {
Expand Down