UNPKG

homebridge-obis-powermeter

Version:

Plugin for OBIS smart meter devices (SML/D0) to read power, energy, and voltages.

76 lines (75 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class VoltageSensor { constructor(_config, log, api, accessory, device, opts) { this.log = log; this.api = api; this.accessory = accessory; this.device = device; this.Service = this.api.hap.Service; this.Characteristic = this.api.hap.Characteristic; this.obisKey = opts.obisKey; try { this.accessory.category = 10 /* this.api.hap.Categories.SENSOR */; } catch (_e) { // noop: category not supported } const info = this.accessory.getService(this.Service.AccessoryInformation); if (!info) { log.error('No service accessory provided'); return; } info .setCharacteristic(this.Characteristic.Manufacturer, 'HomebridgeObis') .setCharacteristic(this.Characteristic.Model, `${this.device.product_name} Voltage`) .setCharacteristic(this.Characteristic.SerialNumber, `${this.device.serial}-${opts.serialSuffix}`); // Use LightSensor to display numeric value in most clients this.voltageService = this.accessory.getService(this.Service.LightSensor) || this.accessory.addService(this.Service.LightSensor, opts.name); } floatOf(m) { if (!m) { return NaN; } try { if (typeof m.valueToString === 'function') { const s = String(m.valueToString()); const match = s.match(/-?\d+(?:[.,]\d+)?/); if (match) { let v = Number(match[0].replace(',', '.')); const unit = s.toLowerCase(); if (unit.includes('kv')) { v = v * 1000; } return v; } } const maybe = m; const vals = typeof maybe.getValues === 'function' ? maybe.getValues() : maybe.values; if (Array.isArray(vals) && vals.length > 0) { const first = vals[0]; if (Number.isFinite(first === null || first === void 0 ? void 0 : first.value)) { const u = String(first.unit || '').toLowerCase(); if (u.includes('kv')) { return first.value * 1000; } return first.value; } } } catch (_e) { // noop: parse failure handled by returning NaN } return NaN; } beatWithData(data) { const v = this.floatOf(data[this.obisKey]); const value = Number.isFinite(v) && v > 0 ? v : 0.0001; // HomeKit min this.voltageService.setCharacteristic(this.Characteristic.CurrentAmbientLightLevel, value); } } exports.default = VoltageSensor;