@homebridge-plugins/homebridge-rainbird
Version:
The Rainbird plugin allows you to access your Rainbird device(s) from HomeKit.
175 lines • 7.66 kB
JavaScript
export class DeviceBase {
platform;
accessory;
device;
rainbird;
api;
log;
config;
hap;
// Config
deviceLogging;
deviceRefreshRate;
deviceUpdateRate;
devicePushRate;
deviceFirmwareVersion;
constructor(platform, accessory, device, rainbird) {
this.platform = platform;
this.accessory = accessory;
this.device = device;
this.rainbird = rainbird;
this.api = this.platform.api;
this.log = this.platform.log;
this.config = this.platform.config;
this.hap = this.api.hap;
this.getDeviceLogSettings(device);
this.getDeviceRateSettings(device);
this.getDeviceConfigSettings(device);
this.getDeviceContext(accessory, device);
// Set accessory information
accessory
.getService(this.hap.Service.AccessoryInformation)
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'RainBird')
.setCharacteristic(this.hap.Characteristic.AppMatchingIdentifier, 'id1060727082')
.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
.setCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName)
.setCharacteristic(this.hap.Characteristic.Model, accessory.context.model ?? rainbird.model)
.setCharacteristic(this.hap.Characteristic.SerialNumber, accessory.context.deviceID ?? rainbird.serialNumber)
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.deviceFirmwareVersion)
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
.updateValue(this.deviceFirmwareVersion);
}
async getDeviceLogSettings(device) {
this.deviceLogging = this.platform.debugMode ? 'debugMode' : device.logging ?? this.platform.platformLogging ?? 'standard';
const logging = this.platform.debugMode ? 'Debug Mode' : device.logging ? 'Device Config' : this.platform.platformLogging ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${logging} Logging: ${this.deviceLogging}`);
}
async getDeviceRateSettings(device) {
// refreshRate
this.deviceRefreshRate = device.refreshRate ?? this.platform.platformRefreshRate ?? 1800;
const refreshRate = device.refreshRate ? 'Device Config' : this.platform.platformRefreshRate ? 'Platform Config' : 'Default';
// updateRate
this.deviceUpdateRate = device.updateRate ?? this.platform.platformUpdateRate ?? 5;
const updateRate = device.updateRate ? 'Device Config' : this.platform.platformUpdateRate ? 'Platform Config' : 'Default';
// pushRate
this.devicePushRate = device.pushRate ?? this.platform.platformPushRate ?? 1;
const pushRate = device.pushRate ? 'Device Config' : this.platform.platformPushRate ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${refreshRate} refreshRate: ${this.deviceRefreshRate}, ${updateRate} updateRate: ${this.deviceUpdateRate}, ${pushRate} pushRate: ${this.devicePushRate}`);
}
async getDeviceConfigSettings(device) {
const deviceConfig = {};
const properties = [
'logging',
'refreshRate',
'updateRate',
'pushRate',
'external',
'showRainSensor',
'showValveSensor',
'showProgramASwitch',
'showProgramBSwitch',
'showProgramCSwitch',
'showProgramDSwitch',
'showDelayIrrigationSwitch',
'showStopIrrigationSwitch',
'minValueRemainingDuration',
'maxValueRemainingDuration',
'syncTime',
'showRequestResponse',
'showZoneValve',
'includeZones',
'irrigationDelay',
];
properties.forEach((prop) => {
if (device[prop] !== undefined) {
deviceConfig[prop] = device[prop];
}
});
if (Object.keys(deviceConfig).length !== 0) {
this.debugSuccessLog(`Config: ${JSON.stringify(deviceConfig)}`);
}
}
async getDeviceContext(accessory, device) {
const deviceFirmwareVersion = device.firmware ?? this.rainbird.version ?? this.platform.version ?? '0.0.0';
const version = deviceFirmwareVersion.toString();
this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
if (version?.includes('.') === false) {
const replace = version?.replace(/^V|-.*$/g, '');
const match = replace?.match(/./g);
const validVersion = match?.join('.');
this.deviceFirmwareVersion = validVersion ?? '0.0.0';
}
else {
this.deviceFirmwareVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
}
accessory
.getService(this.hap.Service.AccessoryInformation)
.setCharacteristic(this.hap.Characteristic.HardwareRevision, this.deviceFirmwareVersion)
.setCharacteristic(this.hap.Characteristic.SoftwareRevision, this.deviceFirmwareVersion)
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.deviceFirmwareVersion)
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
.updateValue(this.deviceFirmwareVersion);
this.debugSuccessLog(`deviceFirmwareVersion: ${this.deviceFirmwareVersion}`);
}
/**
* Logging for Device
*/
async infoLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.info(`${this.accessory.displayName}`, String(...log));
}
}
async successLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.success(`${this.accessory.displayName}`, String(...log));
}
}
async debugSuccessLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.success(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async warnLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.warn(`${this.accessory.displayName}`, String(...log));
}
}
async debugWarnLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.warn(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async errorLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.error(`${this.accessory.displayName}`, String(...log));
}
}
async debugErrorLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.error(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async debugLog(...log) {
if (await this.enablingDeviceLogging()) {
if (this.deviceLogging === 'debug') {
this.log.info(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
else if (this.deviceLogging === 'debugMode') {
this.log.debug(`${this.accessory.displayName}`, String(...log));
}
}
}
async loggingIsDebug() {
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug';
}
async enablingDeviceLogging() {
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
}
}
//# sourceMappingURL=DeviceBase.js.map