Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions common/v2/identifyRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const fetch = require('node-fetch');
const https = require('https');

/**
* Async function to perform an 'identify' request to the HomeWizard Energy device.
*
* @async
* @param {string} url - The URL of the HomeWizard Energy device.
* @param {string} token - The token for authentication.
* @throws {Error} Throws an error if the URL or token is not defined.
* @throws {Error} Throws an error if the response is not ok.
*
* @returns {Promise<void>} A promise that resolves when the request is successful.
*/
async function onIdentify(url, token) {
if (!url) throw new Error('URL is not defined');
if (!token) throw new Error('Token is not defined');

const res = await fetch(`${url}/api/system/identify`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
agent: new (https.Agent)({ rejectUnauthorized: false }), // Ignore SSL errors
}).catch(((err) => {
throw new Error(`Network error: ${err.message}`);
}));

// Check if the response is ok (status code 200-299)
if (!res.ok) { throw new Error(res.statusText); }
}

module.exports = { onIdentify };
28 changes: 6 additions & 22 deletions drivers/energy_v2/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const Homey = require('homey');
const fetch = require('node-fetch');
const https = require('https');

const { onIdentify } = require('../../common/v2/identifyRequest');

const POLL_INTERVAL = 1000 * 10; // 10 seconds

module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {
Expand All @@ -15,8 +17,10 @@ module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {
this._flowTriggerImport = this.homey.flow.getDeviceTriggerCard('import_changed');
this._flowTriggerExport = this.homey.flow.getDeviceTriggerCard('export_changed');

this.token = this.getStoreValue('token');

this.registerCapabilityListener('identify', async (value) => {
await this.onIdentify();
await onIdentify(this.url, this.token);
});
}

Expand Down Expand Up @@ -58,24 +62,6 @@ module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {
this.onPoll();
}

async onIdentify() {
if (!this.url) return;

const token = this.getStoreValue('token');

const res = await fetch(`${this.url}/api/system/identify`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
agent: new (require('https').Agent)({ rejectUnauthorized: false }), // Ignore SSL errors
}).catch(this.error);

if (!res.ok)
{ throw new Error(res.statusText); }
}

onPoll() {

const httpsAgent = new https.Agent({
Expand All @@ -86,11 +72,9 @@ module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {

Promise.resolve().then(async () => {

const token = this.getStoreValue('token');

const res = await fetch(`${this.url}/api/measurement`, {
headers: {
Authorization: `Bearer ${token}`,
Authorization: `Bearer ${this.token}`,
},
agent: new (require('https').Agent)({ rejectUnauthorized: false }), // Ignore SSL errors
});
Expand Down
36 changes: 6 additions & 30 deletions drivers/plugin_battery/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ const Homey = require('homey');
const fetch = require('node-fetch');
const https = require('https');

const { onIdentify } = require('../../common/v2/identifyRequest');

const POLL_INTERVAL = 1000 * 10; // 10 seconds

module.exports = class HomeWizardPluginBattery extends Homey.Device {

async onInit() {
this.onPollInterval = setInterval(this.onPoll.bind(this), POLL_INTERVAL);

this.token = this.getStoreValue('token');

this.registerCapabilityListener('identify', async (value) => {
await this.onIdentify();
await onIdentify(this.url, this.token);
});
}

Expand Down Expand Up @@ -42,24 +46,6 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
this.onPoll();
}

async onIdentify() {
if (!this.url) return;

const token = this.getStoreValue('token');

const res = await fetch(`${this.url}/api/system/identify`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
agent: new (require('https').Agent)({ rejectUnauthorized: false }), // Ignore SSL errors
}).catch(this.error);

if (!res.ok)
{ throw new Error(res.statusText); }
}

onPoll() {

const httpsAgent = new https.Agent({
Expand All @@ -68,21 +54,11 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {

if (!this.url) return;

const token2 = this.getData().token;
const token = this.getStoreValue('token');

if (!token) {
token = token2;
}

// console.log('Token: ', token);
// console.log('Token2: ', token2);

Promise.resolve().then(async () => {

const res = await fetch(`${this.url}/api/measurement`, {
headers: {
Authorization: `Bearer ${token}`,
Authorization: `Bearer ${this.token}`,
},
agent: new (require('https').Agent)({ rejectUnauthorized: false }), // Ignore SSL errors
});
Expand Down