homebridge-platform-orbit
Version:
Orbit Irrigation System platform plugin for [HomeBridge](https://github.com/nfarina/homebridge).
338 lines • 21.4 kB
JavaScript
;
var PluginName = 'homebridge-platform-orbit';
var PlatformName = 'orbit';
var hap;
var orbitapi_1 = require("./orbitapi");
var PlatformOrbit = /** @class */ (function () {
function PlatformOrbit(log, config, api) {
var _this = this;
this.log = log;
this.config = config;
this.api = api;
this.email = "";
this.password = "";
this.accessories = {};
if (!config || !config["email"] || !config["password"]) {
this.log.error("Platform config incorrect or missing. Check the config.json file.");
}
else {
this.email = config["email"];
this.password = config["password"];
this.log.info('Starting PlatformOrbit using homebridge API', api.version);
if (api) {
// save the api for use later
this.api = api;
// if finished loading cache accessories
this.api.on("didFinishLaunching", function () {
// Load the orbit devices
_this.loadDevices();
});
}
}
}
PlatformOrbit.prototype.loadDevices = function () {
var _this = this;
this.log.debug("Loading the devices");
// login to the API and get the token
var orbitAPI = new orbitapi_1.OrbitAPI(this.log, this.email, this.password);
orbitAPI.login()
.then(function () {
// get an array of the devices
orbitAPI.getDevices()
.then(function (devices) {
// loop through each device
devices.forEach(function (device) {
// Generate irrigation service uuid
var uuid = hap.uuid.generate(device.id);
// Check if device is already loaded from cache
if (_this.accessories[uuid]) {
_this.log.info('Configuring cached device', device.name);
// Setup Irrigation Accessory and Irrigation Service
var irrigationAccessory_1 = _this.accessories[uuid];
irrigationAccessory_1.context.timeEnding = [];
_this.api.updatePlatformAccessories([irrigationAccessory_1]);
_this.configureIrrigationService(irrigationAccessory_1.getService(hap.Service.IrrigationSystem));
// Find the valve Services associated with the accessory
irrigationAccessory_1.services.forEach(function (service) {
if (hap.Service.Valve.UUID === service.UUID) {
// Configure Valve Service
_this.configureValveService(irrigationAccessory_1, service, device);
}
});
}
else {
_this.log.info('Creating and configuring new device', device.name);
// Create Irrigation Accessory and Irrigation Service
var irrigationAccessory_2 = new _this.api.platformAccessory(device.name, uuid);
irrigationAccessory_2.context.timeEnding = [];
var irrigationSystemService_1 = _this.createIrrigationService(irrigationAccessory_2, device);
_this.configureIrrigationService(irrigationSystemService_1);
// Create and configure Values services and link to Irrigation Service
device.zones.forEach(function (zone) {
var valveService = _this.createValveService(irrigationAccessory_2, zone['station'], zone['name']);
irrigationSystemService_1.addLinkedService(valveService);
_this.configureValveService(irrigationAccessory_2, valveService, device);
});
// Register platform accessory
_this.log.debug('Registering platform accessory');
_this.api.registerPlatformAccessories(PluginName, PlatformName, [irrigationAccessory_2]);
_this.accessories[uuid] = irrigationAccessory_2;
}
device.openConnection();
device.onMessage(_this.processMessage.bind(_this));
device.sync();
});
})["catch"](function (error) {
_this.log.error('Unable to get devices', error);
});
})["catch"](function (error) {
_this.log.error('Unable to get token', error);
});
};
PlatformOrbit.prototype.configureAccessory = function (accessory) {
// Add cached devices to the accessories arrary
this.log.info('Loading accessory from cache', accessory.displayName);
this.accessories[accessory.UUID] = accessory;
};
PlatformOrbit.prototype.createIrrigationService = function (irrigationAccessory, device) {
this.log.debug('Create Irrigation service', device.id);
// Update AccessoryInformation Service
irrigationAccessory.getService(hap.Service.AccessoryInformation)
.setCharacteristic(hap.Characteristic.Name, device.name)
.setCharacteristic(hap.Characteristic.Manufacturer, "Orbit")
.setCharacteristic(hap.Characteristic.SerialNumber, device.id)
.setCharacteristic(hap.Characteristic.Model, device.hardware_version)
.setCharacteristic(hap.Characteristic.FirmwareRevision, device.firmware_version);
// Add Irrigation System Service
var irrigationSystemService = irrigationAccessory.addService(hap.Service.IrrigationSystem, device.name)
.setCharacteristic(hap.Characteristic.Name, device.name)
.setCharacteristic(hap.Characteristic.Active, hap.Characteristic.Active.ACTIVE)
.setCharacteristic(hap.Characteristic.InUse, hap.Characteristic.InUse.NOT_IN_USE)
.setCharacteristic(hap.Characteristic.ProgramMode, hap.Characteristic.ProgramMode.NO_PROGRAM_SCHEDULED)
.setCharacteristic(hap.Characteristic.RemainingDuration, 0)
.setCharacteristic(hap.Characteristic.StatusFault, device.is_connected ? hap.Characteristic.StatusFault.NO_FAULT : hap.Characteristic.StatusFault.GENERAL_FAULT);
return irrigationSystemService;
};
PlatformOrbit.prototype.configureIrrigationService = function (irrigationSystemService) {
var _this = this;
this.log.debug('Configure Irrigation service', irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value);
// Configure Irrigation System Service
irrigationSystemService
.getCharacteristic(hap.Characteristic.Active)
.onGet(function () {
_this.log.debug("IrrigationSystem", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "Active = ", irrigationSystemService.getCharacteristic(hap.Characteristic.Active).value ? "ACTIVE" : "INACTIVE");
return irrigationSystemService.getCharacteristic(hap.Characteristic.Active).value;
})
.onSet(function (value) {
_this.log.info("Set irrigation system ", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, " to ", value ? "ACTIVE" : "INACTIVE");
irrigationSystemService.getCharacteristic(hap.Characteristic.Active).updateValue(value);
});
irrigationSystemService
.getCharacteristic(hap.Characteristic.ProgramMode)
.onGet(function () {
_this.log.debug("IrrigationSystem", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "ProgramMode = ", irrigationSystemService.getCharacteristic(hap.Characteristic.ProgramMode).value);
return irrigationSystemService.getCharacteristic(hap.Characteristic.ProgramMode).value;
});
irrigationSystemService
.getCharacteristic(hap.Characteristic.InUse)
.onGet(function () {
_this.log.debug("IrrigationSystem", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "InUse = ", irrigationSystemService.getCharacteristic(hap.Characteristic.InUse).value ? "IN_USE" : "NOT_IN_USE");
return irrigationSystemService.getCharacteristic(hap.Characteristic.InUse).value;
});
irrigationSystemService
.getCharacteristic(hap.Characteristic.StatusFault)
.onGet(function () {
_this.log.debug("IrrigationSystem", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "StatusFault = ", irrigationSystemService.getCharacteristic(hap.Characteristic.StatusFault).value ? "GENERAL_FAULT" : "NO_FAULT");
return irrigationSystemService.getCharacteristic(hap.Characteristic.StatusFault).value;
});
irrigationSystemService
.getCharacteristic(hap.Characteristic.RemainingDuration)
.onGet(function () {
_this.log.debug("IrrigationSystem", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "RemainingDuration = ", irrigationSystemService.getCharacteristic(hap.Characteristic.RemainingDuration).value);
return irrigationSystemService.getCharacteristic(hap.Characteristic.RemainingDuration).value;
});
};
PlatformOrbit.prototype.createValveService = function (irrigationAccessory, station, name) {
this.log.debug("Create Valve service " + name + " with station " + station);
// Add Valve Service
var valve = irrigationAccessory.addService(hap.Service.Valve, name, station);
valve
.setCharacteristic(hap.Characteristic.Active, hap.Characteristic.Active.INACTIVE)
.setCharacteristic(hap.Characteristic.InUse, hap.Characteristic.InUse.NOT_IN_USE)
.setCharacteristic(hap.Characteristic.ValveType, hap.Characteristic.ValveType.IRRIGATION)
.setCharacteristic(hap.Characteristic.SetDuration, 300)
.setCharacteristic(hap.Characteristic.RemainingDuration, 0)
.setCharacteristic(hap.Characteristic.IsConfigured, hap.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(hap.Characteristic.ServiceLabelIndex, station)
.setCharacteristic(hap.Characteristic.StatusFault, hap.Characteristic.StatusFault.NO_FAULT)
.setCharacteristic(hap.Characteristic.Name, name);
return valve;
};
PlatformOrbit.prototype.configureValveService = function (irrigationAccessory, valveService, device) {
var _this = this;
this.log.debug("Configure Valve service", valveService.getCharacteristic(hap.Characteristic.Name).value);
// Configure Valve Service
valveService
.getCharacteristic(hap.Characteristic.Active)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "Active = ", valveService.getCharacteristic(hap.Characteristic.Active).value ? "ACTIVE" : "INACTIVE");
return valveService.getCharacteristic(hap.Characteristic.Active).value;
})
.onSet(function (value) {
// Prepare message for API
var station = valveService.getCharacteristic(hap.Characteristic.ServiceLabelIndex).value;
var run_time = valveService.getCharacteristic(hap.Characteristic.SetDuration).value / 60;
if (value == hap.Characteristic.Active.ACTIVE) {
// Turn on the valve
_this.log.info("Start zone", valveService.getCharacteristic(hap.Characteristic.Name).value, "for", run_time, "mins");
device.startZone(station, run_time);
}
else {
// Turn off the valve
_this.log.info("Stop zone", valveService.getCharacteristic(hap.Characteristic.Name).value);
device.stopZone();
}
});
valveService
.getCharacteristic(hap.Characteristic.InUse)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "InUse = ", valveService.getCharacteristic(hap.Characteristic.InUse).value ? "IN_USE" : "NOT_IN_USE");
return valveService.getCharacteristic(hap.Characteristic.InUse).value;
});
valveService
.getCharacteristic(hap.Characteristic.IsConfigured)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "IsConfigured = ", valveService.getCharacteristic(hap.Characteristic.IsConfigured).value ? "CONFIGURED" : "NOT_CONFIGURED");
return valveService.getCharacteristic(hap.Characteristic.IsConfigured).value;
})
.onSet(function (value) {
_this.log.info("Set valve ", valveService.getCharacteristic(hap.Characteristic.Name).value, " isConfigured to ", value);
valveService.getCharacteristic(hap.Characteristic.IsConfigured).updateValue(value);
});
valveService
.getCharacteristic(hap.Characteristic.StatusFault)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "StatusFault = ", valveService.getCharacteristic(hap.Characteristic.StatusFault).value ? "GENERAL_FAULT" : "NO_FAULT");
return valveService.getCharacteristic(hap.Characteristic.StatusFault).value;
});
valveService
.getCharacteristic(hap.Characteristic.ValveType)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "ValveType = ", valveService.getCharacteristic(hap.Characteristic.ValveType).value);
return valveService.getCharacteristic(hap.Characteristic.ValveType).value;
});
valveService
.getCharacteristic(hap.Characteristic.SetDuration)
.onGet(function () {
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "SetDuration = ", valveService.getCharacteristic(hap.Characteristic.SetDuration).value);
return valveService.getCharacteristic(hap.Characteristic.SetDuration).value;
})
.onSet(function (value) {
// Update the value run duration
_this.log.info("Set valve ", valveService.getCharacteristic(hap.Characteristic.Name).value, " SetDuration to ", value);
valveService.getCharacteristic(hap.Characteristic.SetDuration).updateValue(value);
});
valveService
.getCharacteristic(hap.Characteristic.RemainingDuration)
.onGet(function () {
// Calc remain duration
var station = valveService.getCharacteristic(hap.Characteristic.ServiceLabelIndex).value;
var timeRemaining = Math.max(Math.round((irrigationAccessory.context.timeEnding[station] - Date.now()) / 1000), 0);
if (isNaN(timeRemaining)) {
timeRemaining = 0;
}
_this.log.debug("Valve", valveService.getCharacteristic(hap.Characteristic.Name).value, "RemainingDuration =", timeRemaining);
return timeRemaining;
});
irrigationAccessory.context.timeEnding[valveService.getCharacteristic(hap.Characteristic.ServiceLabelIndex).value] = 0;
};
PlatformOrbit.prototype.processMessage = function (message) {
// Incoming data
var jsonData = JSON.parse(message);
// Find the irrigation system and process message
var irrigationAccessory = this.accessories[hap.uuid.generate(jsonData['device_id'])];
var irrigationSystemService = irrigationAccessory.getService(hap.Service.IrrigationSystem);
switch (jsonData['event']) {
case "watering_in_progress_notification":
this.log.debug("Watering_in_progress_notification Station", "device =", irrigationSystemService.getCharacteristic(hap.Characteristic.Name).value, "station =", jsonData['current_station'], "Runtime =", jsonData['run_time']);
// Update Irrigation System Service
irrigationSystemService.getCharacteristic(hap.Characteristic.InUse).updateValue(hap.Characteristic.InUse.IN_USE);
// Find the valve Services
irrigationAccessory.services.forEach(function (service) {
if (hap.Service.Valve.UUID === service.UUID) {
// Update Valve Services
var station = service.getCharacteristic(hap.Characteristic.ServiceLabelIndex).value;
if (station == jsonData['current_station']) {
service.getCharacteristic(hap.Characteristic.Active).updateValue(hap.Characteristic.Active.ACTIVE);
service.getCharacteristic(hap.Characteristic.InUse).updateValue(hap.Characteristic.InUse.IN_USE);
service.getCharacteristic(hap.Characteristic.RemainingDuration).updateValue(jsonData['run_time'] * 60);
irrigationAccessory.context.timeEnding[station] = Date.now() + parseInt(jsonData['run_time']) * 60 * 1000;
}
else {
service.getCharacteristic(hap.Characteristic.Active).updateValue(hap.Characteristic.Active.INACTIVE);
service.getCharacteristic(hap.Characteristic.InUse).updateValue(hap.Characteristic.InUse.NOT_IN_USE);
service.getCharacteristic(hap.Characteristic.RemainingDuration).updateValue(0);
}
}
;
});
break;
case "watering_complete":
case "device_idle":
this.log.debug("Watering_complete or device_idle");
// Update Irrigation System Service
irrigationSystemService.getCharacteristic(hap.Characteristic.InUse).updateValue(hap.Characteristic.InUse.NOT_IN_USE);
// Find the valve Services
irrigationAccessory.services.forEach(function (service) {
// Update Valve hap.Service
if (hap.Service.Valve.UUID === service.UUID) {
service.getCharacteristic(hap.Characteristic.Active).updateValue(hap.Characteristic.Active.INACTIVE);
service.getCharacteristic(hap.Characteristic.InUse).updateValue(hap.Characteristic.InUse.NOT_IN_USE);
service.getCharacteristic(hap.Characteristic.RemainingDuration).updateValue(0);
}
});
break;
case "change_mode":
this.log.debug("change_mode", jsonData['mode']);
// Update the ProgramMode
switch (jsonData['mode']) {
case "off":
irrigationSystemService.getCharacteristic(hap.Characteristic.ProgramMode).updateValue(hap.Characteristic.ProgramMode.NO_PROGRAM_SCHEDULED);
break;
case "auto":
irrigationSystemService.getCharacteristic(hap.Characteristic.ProgramMode).updateValue(hap.Characteristic.ProgramMode.PROGRAM_SCHEDULED);
break;
case "manual":
irrigationSystemService.getCharacteristic(hap.Characteristic.ProgramMode).updateValue(hap.Characteristic.ProgramMode.PROGRAM_SCHEDULED_MANUAL_MODE_);
break;
}
break;
case "program_changed":
this.log.debug("program_change - do nothing");
break;
case "rain_delay":
this.log.debug("rain_delay - do nothing");
break;
case "device_connected":
this.log.debug("device_connected");
irrigationSystemService.getCharacteristic(hap.Characteristic.StatusFault).updateValue(hap.Characteristic.StatusFault.NO_FAULT);
break;
case "device_disconnected":
this.log.debug("device_disconnected");
irrigationSystemService.getCharacteristic(hap.Characteristic.StatusFault).updateValue(hap.Characteristic.StatusFault.GENERAL_FAULT);
break;
case "clear_low_battery":
this.log.debug("clear_low_battery - do nothing");
break;
default:
this.log.warn("Unhandled message received: " + jsonData['event']);
break;
}
};
return PlatformOrbit;
}());
module.exports = function (api) {
hap = api.hap;
api.registerPlatform(PluginName, PlatformName, PlatformOrbit);
};
//# sourceMappingURL=platform.js.map