forked from fredan75/worx-landroid-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
landroid2home-assistant.js
51 lines (46 loc) · 1.64 KB
/
landroid2home-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
var config = require('./config'); // Read configuration
// Import project modules
var Landroid = require('./landroid');
var LandroidState = Landroid.LandroidState;
var HomeAssistant = require('./home-assistant');
var homeAssistant = new HomeAssistant(config);
var landroid = new Landroid(config);
console.log("Scheduling polling");
landroid.pollEvery(60, function(status) { // Poll every 60 seconds
if(status) { // We got some data back from the Landroid
// Send data to Home Assistant
homeAssistant.setNoOfAlarms(status.noOfAlarms);
homeAssistant.setBatteryPercentage(status.batteryPercentage);
homeAssistant.setTotalMowingHours(status.totalMowingHours);
homeAssistant.setState(status.state.toString());
switch (status.state) {
case LandroidState.ALARM:
homeAssistant.setState(status.errorMessage ? status.errorMessage : "[Alarm]");
break;
case LandroidState.CHARGING:
homeAssistant.setState("Charging");
break;
case LandroidState.CHARGING_COMPLETE:
homeAssistant.setState("Charging complete");
break;
case LandroidState.MOWING:
homeAssistant.setState("Mowing");
break;
case LandroidState.GOING_HOME:
homeAssistant.setState("Going home");
break;
case LandroidState.MANUAL_STOP:
homeAssistant.setState("Manual stop");
break;
case LandroidState.ERROR:
homeAssistant.setState("ERROR!");
break;
default:
console.error("Unknown state: " + status.state);
}
}
else {
homeAssistant.setState("Error getting update!");
console.error("Error getting update!");
}
});