-
Notifications
You must be signed in to change notification settings - Fork 144
Add sensor support #52
Changes from 2 commits
50936ee
665abcb
872d40f
968015f
449ec07
851a12b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
@@ -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'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Derp. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not put the actual degrees symbol in? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are both
climate
andsensor
added?