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 2 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
* **Temperature** - temperature from sensors and climate devices is exposed

### 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": ["climate", "fan", "garage_door", "input_boolean", "light", "lock", "media_player", "rollershutter", "scene", "sensor", "switch"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are both climate and sensor added?

}
]
```
Expand Down
75 changes: 75 additions & 0 deletions accessories/temperature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var Service, Characteristic, communicationError;

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

return HomeAssistantTemperature;
};
module.exports.HomeAssistantTemperature = HomeAssistantTemperature;

function HomeAssistantTemperature(log, data, client) {
// 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;
}

HomeAssistantTemperature.prototype = {
temperatureFromData: function(data) {
if (this.entity_type == 'sensor'){
value = parseFloat(data.state)
}else{
value = parseFloat(data.attributes.temperature)
}
// HomeKit only works with Celsius internally
if (data.attributes.unit_of_measurement == '\u00B0F') {
value = (value - 32) / 1.8
}
return value
},
onEvent: function(old_state, new_state) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to trigger this to test it.

this.temperatureService.getCharacteristic(Characteristic.CurrentTemperature)
.setValue(this.temperatureFromData(new_state), null, 'internal')
},
identify: function(callback){
this.log("identifying: " + this.name);
callback();
},
getTemperature: function(callback){
this.log("fetching temperature for: " + this.name);
this.client.fetchState(this.entity_id, function(data){
if (data) {
callback(null, this.temperatureFromData(data))
}else{
callback(communicationError)
}
}.bind(this))
},
getServices: function() {
this.temperatureService = new Service.TemperatureSensor();
var informationService = new Service.AccessoryInformation();

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

this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this))

return [informationService, this.temperatureService];
}

}
8 changes: 8 additions & 0 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 HomeAssistantTemperature;

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);
HomeAssistantTemperature = require('./accessories/temperature')(Service, Characteristic, communicationError);

homebridge.registerPlatform("homebridge-homeassistant", "HomeAssistant", HomeAssistantPlatform, false);
}
Expand Down Expand Up @@ -186,6 +188,12 @@ 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'){
if (entity.attributes && (entity.attributes.unit_of_measurement == '\u00B0C' || entity.attributes.unit_of_measurement == '\u00B0F') && entity.entity_id != 'sensor.vision_zp3111_multisensor_4in1_dew_point_5'){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove && entity.entity_id != 'sensor.vision_zp3111_multisensor_4in1_dew_point_5'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not put the actual degrees symbol in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can. I wasn't sure what the source encoding for JS/node was, but it seems like UTF-8 works.

accessory = new HomeAssistantTemperature(that.log, entity, that);
}
}else if (entity_type == 'climate'){
accessory = new HomeAssistantTemperature(that.log, entity, that);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather leave the climate stuff out so that it can be implemented as a real thermostat service instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I didn't realise there was a thermostat service.

}

if (accessory) {
Expand Down