homebridge-ecowitt-weather-sensors
Version:
Homebridge support for your Fine Offset weather station (Ecowitt, Ambient, Froggit, GoGen, and more)
100 lines • 6.69 kB
JavaScript
"use strict";
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WH57 = void 0;
const EcowittAccessory_1 = require("./../EcowittAccessory");
const LightningSensor_1 = require("./../sensors/LightningSensor");
const BatterySensor_1 = require("./../sensors/BatterySensor");
const utils = __importStar(require("./../Utils"));
class WH57 extends EcowittAccessory_1.EcowittAccessory {
constructor(platform, accessory) {
var _a, _b, _c, _d, _e, _f;
super(platform, accessory, 'WH57', 'WH57 Lightning Sensor');
this.platform = platform;
this.accessory = accessory;
this.requiredData = ['wh57batt', 'lightning', 'lightning_num', 'lightning_time'];
const hideConfig = ((_a = this.platform.config) === null || _a === void 0 ? void 0 : _a.hidden) || {};
const hideConfigCustom = ((_b = this.platform.config) === null || _b === void 0 ? void 0 : _b.customHidden) || [];
const hidden = Object.keys(hideConfig).filter(k => !!hideConfig[k]).concat(hideConfigCustom);
let nameOverride;
if (!utils.includesAny(hidden, ['battery', `${this.shortServiceId}:battery`])) {
nameOverride = utils.lookup((_c = this.platform.config) === null || _c === void 0 ? void 0 : _c.nameOverrides, `${this.shortServiceId}:battery`);
this.battery = new BatterySensor_1.BatterySensor(platform, accessory, `${this.accessoryId}:battery`, nameOverride || 'Battery');
}
else {
this.battery = new BatterySensor_1.BatterySensor(platform, accessory, `${this.accessoryId}:battery`, 'Battery');
this.battery.removeService();
this.battery = undefined;
}
if (!utils.includesAny(hidden, ['lightningEvents', `${this.shortServiceId}:lightningEvents`])) {
nameOverride = utils.lookup((_d = this.platform.config) === null || _d === void 0 ? void 0 : _d.nameOverrides, `${this.shortServiceId}:lightningEvents`);
this.lightningEvents = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningEvents`, nameOverride || 'Strike Events');
}
else {
this.lightningEvents = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningEvents`, 'Strike Events');
this.lightningEvents.removeService();
this.lightningEvents = undefined;
}
if (!utils.includesAny(hidden, ['lightningDistance', `${this.shortServiceId}:lightningDistance`])) {
nameOverride = utils.lookup((_e = this.platform.config) === null || _e === void 0 ? void 0 : _e.nameOverrides, `${this.shortServiceId}:lightningDistance`);
this.lightningDistance = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningDistance`, nameOverride || 'Last Strike Distance');
}
else {
this.lightningDistance = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningDistance`, 'Last Strike Distance');
this.lightningDistance.removeService();
this.lightningDistance = undefined;
}
if (!utils.includesAny(hidden, ['lightningTime', `${this.shortServiceId}:lightningTime`])) {
nameOverride = utils.lookup((_f = this.platform.config) === null || _f === void 0 ? void 0 : _f.nameOverrides, `${this.shortServiceId}:lightningTime`);
this.lightningTime = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningTime`, nameOverride || 'Last Strike Time');
}
else {
this.lightningTime = new LightningSensor_1.LightningSensor(platform, accessory, `${this.accessoryId}:lightningTime`, 'Last Strike Time');
this.lightningTime.removeService();
this.lightningTime = undefined;
}
}
//----------------------------------------------------------------------------
update(dataReport) {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (!utils.includesAll(Object.keys(dataReport), this.requiredData)) {
throw new Error(`Update on ${this.accessoryId} requires data ${this.requiredData}`);
}
else {
this.platform.log.debug(`Updating accessory ${this.accessoryId}`);
}
const batt = parseFloat(dataReport['wh57batt']);
const batteryLevel = batt / 5.0;
const lowBattery = batt <= 1.1;
(_a = this.battery) === null || _a === void 0 ? void 0 : _a.updateLevel(utils.boundRange(batteryLevel * 100), dataReport.dateutc);
(_b = this.battery) === null || _b === void 0 ? void 0 : _b.updateStatusLow(lowBattery, dataReport.dateutc);
(_c = this.lightningEvents) === null || _c === void 0 ? void 0 : _c.updateLightningEvent(parseFloat(dataReport['lightning_num']), utils.lookup((_d = this.platform.config) === null || _d === void 0 ? void 0 : _d.thresholds, 'lightningEvents'), dataReport.dateutc);
(_e = this.lightningDistance) === null || _e === void 0 ? void 0 : _e.updateLightningDistance(parseFloat(dataReport['lightning']), utils.lookup((_f = this.platform.config) === null || _f === void 0 ? void 0 : _f.thresholds, 'lightningDistance'), dataReport.dateutc);
(_g = this.lightningTime) === null || _g === void 0 ? void 0 : _g.updateLightningTime(parseFloat(dataReport['lightning_time']), utils.lookup((_h = this.platform.config) === null || _h === void 0 ? void 0 : _h.thresholds, 'lightningTime'), dataReport.dateutc);
}
}
exports.WH57 = WH57;
WH57.properties = ['lightningEvents', 'lightningDistance', 'lightningTime'];
//# sourceMappingURL=WH57.js.map