-
Notifications
You must be signed in to change notification settings - Fork 2
/
home-assistant.js
81 lines (66 loc) · 2.63 KB
/
home-assistant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/** Module for communicating with Home Assistant API, see https://home-assistant.io/developers/rest_api.html */
// https://github.com/najaxjs/najax
var najax = require('najax'); // TODO Replace with http
var COMPONENT_PREFIX = "landroid.";
function Entity(entityId, friendlyName, unitOfMeasurement) {
this.entityId = COMPONENT_PREFIX + entityId;
this.friendlyName = friendlyName;
this.unitOfMeasurement = unitOfMeasurement;
}
var BATTERY_PERCENT_ENTITY_ID = new Entity("battery_percent", "Battery percent", "%");
var TOTAL_MOWING_HOURS_ENTITY_ID = new Entity("total_mowing_hours", "Total mowing hours", "h");
var NO_OF_ALARMS_ENTITY_ID = new Entity("no_of_alarms", "No of alarms", "#");
// var ALERT_ENTITY_ID = new Entity("Alert";
var STATE_ENTITY_ID = new Entity("state", "State");
function HomeAssistant(config) {
this.homeAssistantUrl = config.homeAssistantUrl;
this.homeAssistantPassword = config.homeAssistantPassword;
}
HomeAssistant.prototype.ajax = function (type, uri, data, callback) {
var self = this;
var url = self.homeAssistantUrl + "/api/" + (uri ? uri : "");
// console.log("About to '" + type + "' to '" + url + "'" + (data ? ": " + JSON.stringify(data) : ""));
najax({
url: url,
type: type,
data: data, // ? JSON.stringify(data) : null,
contentType: "application/json",
dataType: "json",
headers: {
"X-HA-Access": self.homeAssistantPassword
},
success: function(response) {
// console.log("Response from HomeAssistant: " + JSON.stringify(response));
if(callback)
callback(response);
},
error: function (response) {
console.error("Error calling Home Assistant: " + JSON.stringify(response));
}
})
};
HomeAssistant.prototype.postState = function (entity, state, callback) {
this.ajax("POST", "states/" + entity.entityId, {
state: state,
attributes: {
"friendly_name": entity.friendlyName,
"unit_of_measurement": entity.unitOfMeasurement
}
}, callback);
};
HomeAssistant.prototype.setBatteryPercentage = function(batteryPercentage) {
if(typeof batteryPercentage != "undefined")
this.postState(BATTERY_PERCENT_ENTITY_ID, batteryPercentage);
};
HomeAssistant.prototype.setTotalMowingHours = function(totalMowingHours) {
if(typeof totalMowingHours != "undefined")
this.postState(TOTAL_MOWING_HOURS_ENTITY_ID, totalMowingHours);
};
HomeAssistant.prototype.setNoOfAlarms = function(noOfAlarms) {
if(typeof noOfAlarms != "undefined")
this.postState(NO_OF_ALARMS_ENTITY_ID, noOfAlarms);
};
HomeAssistant.prototype.setState = function (state) {
this.postState(STATE_ENTITY_ID, state);
};
module.exports = HomeAssistant;