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
75 changes: 75 additions & 0 deletions common/v2/Api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

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

module.exports = (function() {

const api = {};

const http_agent = new https.Agent({
rejectUnauthorized: false,
});

/**
* 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.
*/
api.identify = async function(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: http_agent, // 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); }
};

/**
* 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<data>} A promise that resolves with the response data when the request is successful.
*/
api.getMeasurement = async function(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/measurement`, {
headers: {
Authorization: `Bearer ${token}`,
},
agent: http_agent, // 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); }

return res.json();
};

return api;
}());
File renamed without changes.
36 changes: 0 additions & 36 deletions common/v2/identifyRequest.js

This file was deleted.

29 changes: 6 additions & 23 deletions drivers/energy_v2/device.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

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

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

const POLL_INTERVAL = 1000 * 10; // 10 seconds

Expand All @@ -20,7 +17,7 @@ module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {
this.token = this.getStoreValue('token');

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

Expand Down Expand Up @@ -64,29 +61,15 @@ module.exports = class HomeWizardEnergyDeviceV2 extends Homey.Device {

onPoll() {

const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});

// URL may be undefined if the device is not available
if (!this.url) return;

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

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

if (!res || !res.ok)
{ throw new Error(res ? res.statusText : 'Unknown error during fetch'); }

const data = await res.json();

// console.log('Data: ', data);
const data = await api.getMeasurement(this.url, this.token);

const promises = []; // Capture all await promises
// Capture all await promises
const promises = [];

// identify
if (!this.hasCapability('identify')) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/energy_v2/driver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Homey = require('homey');
const driver = require('../../common/v2/driver');
const driver = require('../../common/v2/Driver');

module.exports = driver;
45 changes: 11 additions & 34 deletions drivers/plugin_battery/device.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

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

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

const POLL_INTERVAL = 1000 * 10; // 10 seconds

Expand All @@ -16,7 +13,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
this.token = this.getStoreValue('token');

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

Expand Down Expand Up @@ -48,25 +45,12 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {

onPoll() {

const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});

// URL may be undefined if the device is not available
if (!this.url) return;

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

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

if (!res || !res.ok)
{ throw new Error(res ? res.statusText : 'Unknown error during fetch'); }

const data = await res.json();
const data = await api.getMeasurement(this.url, this.token);

// energy_import_kwh Number The energy usage meter reading in kWh.
// energy_export_kwh Number The energy feed-in meter reading in kWh.
Expand All @@ -89,8 +73,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('meter_power.import')) {
await this.addCapability('meter_power.import').catch(this.error);
}
if (this.getCapabilityValue('meter_power.import') != data.energy_import_kwh)
{ await this.setCapabilityValue('meter_power.import', data.energy_import_kwh).catch(this.error); }
if (this.getCapabilityValue('meter_power.import') != data.energy_import_kwh) { await this.setCapabilityValue('meter_power.import', data.energy_import_kwh).catch(this.error); }
}
else if ((data.energy_import_kwh == undefined) && (this.hasCapability('meter_power.import'))) {
await this.removeCapability('meter_power.import').catch(this.error);
Expand All @@ -101,8 +84,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('meter_power.export')) {
await this.addCapability('meter_power.export').catch(this.error);
}
if (this.getCapabilityValue('meter_power.export') != data.energy_export_kwh)
{ await this.setCapabilityValue('meter_power.export', data.energy_export_kwh).catch(this.error); }
if (this.getCapabilityValue('meter_power.export') != data.energy_export_kwh) { await this.setCapabilityValue('meter_power.export', data.energy_export_kwh).catch(this.error); }
}
else if ((data.energy_export_kwh == undefined) && (this.hasCapability('meter_power.export'))) {
await this.removeCapability('meter_power.export').catch(this.error);
Expand All @@ -113,8 +95,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('measure_power')) {
await this.addCapability('measure_power').catch(this.error);
}
if (this.getCapabilityValue('measure_power') != data.power_w)
{ await this.setCapabilityValue('measure_power', data.power_w).catch(this.error); }
if (this.getCapabilityValue('measure_power') != data.power_w) { await this.setCapabilityValue('measure_power', data.power_w).catch(this.error); }
}
else if ((data.power_w == undefined) && (this.hasCapability('measure_power'))) {
await this.removeCapability('measure_power').catch(this.error);
Expand All @@ -125,8 +106,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('measure_voltage')) {
await this.addCapability('measure_voltage').catch(this.error);
}
if (this.getCapabilityValue('measure_voltage') != data.voltage_l1_v)
{ await this.setCapabilityValue('measure_voltage', data.voltage_l1_v).catch(this.error); }
if (this.getCapabilityValue('measure_voltage') != data.voltage_l1_v) { await this.setCapabilityValue('measure_voltage', data.voltage_l1_v).catch(this.error); }
}
else if ((data.voltage_l1_v == undefined) && (this.hasCapability('measure_voltage'))) {
await this.removeCapability('measure_voltage').catch(this.error);
Expand All @@ -137,8 +117,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('measure_current')) {
await this.addCapability('measure_current').catch(this.error);
}
if (this.getCapabilityValue('measure_current') != data.current_a)
{ await this.setCapabilityValue('measure_current', data.current_a).catch(this.error); }
if (this.getCapabilityValue('measure_current') != data.current_a) { await this.setCapabilityValue('measure_current', data.current_a).catch(this.error); }
}
else if ((data.current_a == undefined) && (this.hasCapability('measure_current'))) {
await this.removeCapability('measure_current').catch(this.error);
Expand All @@ -149,8 +128,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('measure_battery')) {
await this.addCapability('measure_battery').catch(this.error);
}
if (this.getCapabilityValue('measure_battery') != data.state_of_charge_pct)
{ await this.setCapabilityValue('measure_battery', data.state_of_charge_pct).catch(this.error); }
if (this.getCapabilityValue('measure_battery') != data.state_of_charge_pct) { await this.setCapabilityValue('measure_battery', data.state_of_charge_pct).catch(this.error); }
}
else if ((data.state_of_charge_pct == undefined) && (this.hasCapability('measure_battery'))) {
await this.removeCapability('measure_battery').catch(this.error);
Expand All @@ -175,8 +153,7 @@ module.exports = class HomeWizardPluginBattery extends Homey.Device {
if (!this.hasCapability('cycles')) {
await this.addCapability('cycles').catch(this.error);
}
if (this.getCapabilityValue('cycles') != data.cycles)
{ await this.setCapabilityValue('cycles', data.cycles).catch(this.error); }
if (this.getCapabilityValue('cycles') != data.cycles) { await this.setCapabilityValue('cycles', data.cycles).catch(this.error); }
}

})
Expand Down
2 changes: 1 addition & 1 deletion drivers/plugin_battery/driver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Homey = require('homey');
const driver = require('../../common/v2/driver');
const driver = require('../../common/v2/Driver');

module.exports = driver;