From f95fecfe5f11f97f183cae1a82a4c1a1dc8ff9c9 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Sun, 11 Mar 2018 13:55:47 +0100 Subject: [PATCH] (bluefox) add getPanels --- lib/hass.js | 23 +++++++++++++++++++ main.js | 63 ----------------------------------------------------- 2 files changed, 23 insertions(+), 63 deletions(-) diff --git a/lib/hass.js b/lib/hass.js index 94fd25c..1b1ebfb 100644 --- a/lib/hass.js +++ b/lib/hass.js @@ -63,6 +63,19 @@ function HASS(options, log) { } } + function getPanels(socket, callback) { + if (socket && typeof socket.send === 'function') { + var id = currentId++; + requests[id] = {type: 'get_panels', cb: callback, ts: Date.now()}; + socket.send(JSON.stringify({ + id: id, + type: 'get_panels' + })); + } else { + callback && callback('not connected'); + } + } + function getServices(socket, callback) { if (socket && typeof socket.send === 'function') { var id = currentId++; @@ -215,6 +228,16 @@ function HASS(options, log) { } }; + this.getPanels = function (callback) { + if (!connected) { + if (typeof callback === 'function') { + callback('not connected'); + } + } else { + getPanels(this.socket, callback); + } + }; + this.callService = function (service, domain, serviceData, callback) { if (!connected) { if (typeof callback === 'function') { diff --git a/main.js b/main.js index deb53b6..f9e5943 100644 --- a/main.js +++ b/main.js @@ -695,7 +695,6 @@ function main() { adapter.log.debug('Connected'); connected = true; adapter.setState('info.connection', true, true); - hass.getConfig(function (err, config) { if (err) { adapter.log.error('Cannot read config: ' + err); @@ -735,66 +734,4 @@ function main() { }); hass.connect(); - - /*const ssocket = new WebSocket('ws://' + adapter.config.host + ':' + adapter.config.port + '/api/websocket'); - ssocket.addEventListener('message', function (msg) { - console.log(JSON.stringify(msg.a)); - }); - ssocket.addEventListener('close', function () { - console.log('Closed'); - }); - ssocket.addEventListener('error', function (err) { - console.error(err); - }); - - hass.createConnection('ws://' + adapter.config.host + ':' + adapter.config.port + '/api/websocket').then(function (conn) { - if (!connected) { - adapter.log.debug('Connected'); - connected = true; - adapter.setState('info.connection', true, true); - } - socket = conn; - hass.subscribeEntities(conn, function (entities) { - console.log('New entities!', entities); - var attrs = Object.keys(entities); - for (var a = 0; a < attrs.length; a++) { - console.log(attrs[a] + ': ' + entities[attrs[a]].state); - } - }); - hass.subscribeConfig(conn, function (config) { - console.log('New config!', config) - }); - conn.addEventListener('ready', function () { - if (!connected) { - adapter.log.debug('Connected'); - connected = true; - adapter.setState('info.connection', true, true); - } - }); - conn.addEventListener('disconnected', function () { - if (connected) { - adapter.log.debug('Disconnected'); - connected = false; - adapter.setState('info.connection', false, true); - } - }); - - conn.getStates(function (states) { - console.log('getStates: ', states) - }); - - }).catch(function (err) { - if (err === 2) { - adapter.log.error('Invalid password!.'); - } else { - adapter.log.error('Connection failed with code', ERRORS[err] || err); - } - socket = null; - - if (connected) { - adapter.log.debug('Disconnected'); - connected = false; - adapter.setState('info.connection', false, true); - } - });*/ }