UNPKG

homebridge-shelly-consumption

Version:

Provides the consumption values of Shelly EM and Shelly 3EM to HomeKit.

159 lines 7.52 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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.ShellyConsumptionPlatformAccessory = void 0; const http = __importStar(require("http")); /** * Platform Accessory * An instance of this class is created for each accessory your platform registers * Each accessory may expose multiple services of different service types. */ class ShellyConsumptionPlatformAccessory { constructor(platform, accessory, device) { this.platform = platform; this.accessory = accessory; this.device = device; http.get('http://' + platform.config.user + ':' + platform.config.password + '@' + device.ipAddress + '/shelly', (res) => { res.setEncoding('utf-8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(rawData); // set accessory information this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Shelly') .setCharacteristic(this.platform.Characteristic.Model, parsedData.type) .setCharacteristic(this.platform.Characteristic.SerialNumber, parsedData.mac) .setCharacteristic(this.platform.Characteristic.FirmwareRevision, parsedData.fw); } catch (e) { //console.log('Got error' + e.message); } }); }) /*.on('error', (e) => { console.error(`Got error: ${e.message}`); })*/; /* add light sensor for power value */ // get the LightSensor service if it exists, otherwise create a new LightSensor service // you can create multiple services for each accessory this.service = this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor); // set the service name, this is what is displayed as the default name on the Home app // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method. if (accessory.context.device.name !== '') { this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name); } else { this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.ipAddress); } // register handlers for the On/Off Characteristic this.service.getCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel) .on('get', this.handleCurrentAmbientLightLevelGet.bind(this)); // GET - bind to the `handleCurrentAmbientLightLevelGet` method below this.service.getCharacteristic(this.platform.Characteristic.StatusActive) .on('get', this.handleCurrentStatusActive.bind(this)); // GET - bind to the `handleCurrentAmbientLightLevelGet` method below } /** * Handle requests to get the current value of the "Current Ambient Light Level" characteristic */ handleCurrentAmbientLightLevelGet(callback) { this.platform.log.debug('Triggered GET CurrentAmbientLightLevel'); http.get('http://' + this.platform.config.user + ':' + this.platform.config.password + '@' + this.device.ipAddress + '/status', (res) => { res.setEncoding('utf-8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(rawData); let power = 0; if (this.device.emeterChannel1) { power += parsedData.emeters[0].power; } if (this.device.emeterChannel2) { power += parsedData.emeters[1].power; } if (this.device.emeterChannel3) { power += parsedData.emeters[2].power; } if (power === 0) { power = 0.0001; } // set this to a valid value for CurrentAmbientLightLevel callback(null, Math.abs(power)); } catch (e) { //console.log('Got error' + e.message); } }); }) /*.on('error', (e) => { //console.error(`Got error: ${e.message}`); })*/; } handleCurrentStatusActive(callback) { this.platform.log.debug('Triggered GET PowerPolarity'); http.get('http://' + this.platform.config.user + ':' + this.platform.config.password + '@' + this.device.ipAddress + '/status', (res) => { res.setEncoding('utf-8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(rawData); let power = 0; if (this.device.emeterChannel1) { power += parsedData.emeters[0].power; } if (this.device.emeterChannel2) { power += parsedData.emeters[1].power; } if (this.device.emeterChannel3) { power += parsedData.emeters[2].power; } let polarity; if (power >= 0) { polarity = true; } else if (power < 0) { polarity = false; } // set this to a valid value for CurrentAmbientLightLevel callback(null, polarity); } catch (e) { //console.log('Got error' + e.message); } }); }) /*.on('error', (e) => { //console.error(`Got error: ${e.message}`); })*/; } } exports.ShellyConsumptionPlatformAccessory = ShellyConsumptionPlatformAccessory; //# sourceMappingURL=platformAccessory.js.map