homebridge-ecowitt-weather-sensors
Version:
Homebridge support for your Fine Offset weather station (Ecowitt, Ambient, Froggit, GoGen, and more)
88 lines • 4.01 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatterySensor = void 0;
const Sensor_1 = require("./Sensor");
const utils = __importStar(require("./../Utils"));
//------------------------------------------------------------------------------
class BatterySensor extends Sensor_1.Sensor {
platform;
accessory;
id;
name;
constructor(platform, accessory, id, name) {
super(platform, accessory, accessory.services.filter(s => s.subtype === platform.serviceUuid(id))[0]
|| accessory.addService(platform.Service.Battery, name, platform.serviceUuid(id)));
this.platform = platform;
this.accessory = accessory;
this.id = id;
this.name = name;
// remove legacy battery service that was created for each accessory prior to v2.7.0
const legacyBatteryService = accessory.services
.filter(s => (s instanceof platform.Service.Battery && s.subtype === undefined));
if (legacyBatteryService.length > 0) {
accessory.removeService(legacyBatteryService[0]);
}
this.setName(name);
}
//---------------------------------------------------------------------------
updateLevel(level, time) {
if (!Number.isFinite(level)) {
this.platform.log.warn(`Cannot update ${this.name}, battery level ${level} is NaN`);
return;
}
const levelStr = `${level.toFixed(0)} %`;
const staticNames = utils.truthy(this.platform.config?.additional?.staticNames);
this.updateName(staticNames ? this.name : `${this.name} ${levelStr}`);
this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, level);
this.updateTime(time);
}
//---------------------------------------------------------------------------
updateStatusLow(low, time) {
this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, low
? this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
: this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL);
this.updateTime(time);
}
//---------------------------------------------------------------------------
updateChargingState(charging, time) {
this.service.updateCharacteristic(this.platform.Characteristic.ChargingState, charging
? this.platform.Characteristic.ChargingState.CHARGING
: this.platform.Characteristic.ChargingState.NOT_CHARGING);
this.updateTime(time);
}
}
exports.BatterySensor = BatterySensor;
//# sourceMappingURL=BatterySensor.js.map