homebridge-ecowitt-weather-sensors
Version:
Homebridge support for your Fine Offset weather station (Ecowitt, Ambient, Froggit, GoGen, and more)
96 lines • 4.06 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.TemperatureSensor = void 0;
const Sensor_1 = require("./Sensor");
const utils = __importStar(require("./../Utils"));
//------------------------------------------------------------------------------
class TemperatureSensor 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.TemperatureSensor, name, platform.serviceUuid(id)));
this.platform = platform;
this.accessory = accessory;
this.id = id;
this.name = name;
this.setName(name);
this.setStatusActive(false);
}
//---------------------------------------------------------------------------
update(tempf, time) {
if (!Number.isFinite(tempf)) {
this.platform.log.warn(`Cannot update ${this.name}, temperature ${tempf} is NaN`);
this.updateStatusActive(false);
return;
}
let tempStr;
let tempC = utils.toCelcius(tempf);
if (tempC < -270) {
this.platform.log.warn(`Temperature below the minimum of -270°C for ${this.id}`);
tempC = -270;
}
else if (tempC > 100) {
this.platform.log.warn(`Temperature above the maximum of 100°C for ${this.id}`);
tempC = 100;
}
// homekit will round °C to the nearest 0.5 degrees, and then convert to °F
// we mirror this behavior so that temperature in the service name is the same as
// reported by homekit
switch (this.platform.config?.units?.temp) {
case 'ce':
tempStr = `${(Math.round(tempC * 2) / 2).toFixed(2)}°C`;
break;
default:
case 'fa':
tempStr = `${(utils.toFahrenheit(Math.round(tempC * 2) / 2)).toFixed(2)}°F`;
break;
}
const staticNames = utils.truthy(this.platform.config?.additional?.staticNames);
this.updateTemperature(Math.round(tempC * 2) / 2);
this.updateName(staticNames ? this.name : `${this.name} ${tempStr}`);
this.updateStatusActive(true);
this.updateTime(time);
}
//---------------------------------------------------------------------------
updateTemperature(tempc) {
this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, tempc);
}
}
exports.TemperatureSensor = TemperatureSensor;
//# sourceMappingURL=TemperatureSensor.js.map