Skip to content
Closed
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
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: 'standard',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
'n/no-callback-literal': 0,
camelcase: 0
}
}
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Lint

on:
push:
pull_request:
paths:
- '**.js'

jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Install modules
run: npm install
- name: Run ESLint
run: npx eslint . --ext .js,.jsx,.ts,.tsx

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules/

# Added by Homey CLI
/.homeybuild/

# Jetbrains
.idea
14 changes: 6 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"use strict";

const Homey = require('homey');
'use strict'

const Homey = require('homey')

class HomeWizardApp extends Homey.App {
onInit() {
console.log("HomeWizard app ready!");

}
onInit () {
console.log('HomeWizard app ready!')
}
}

module.exports = HomeWizardApp;
module.exports = HomeWizardApp
117 changes: 56 additions & 61 deletions drivers/SDM230/device.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,74 @@
'use strict';
'use strict'

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

const POLL_INTERVAL = 1000 * 10; // 10 seconds
const POLL_INTERVAL = 1000 * 10 // 10 seconds

module.exports = class HomeWizardEnergyDevice extends Homey.Device {

onInit() {
this.onPollInterval = setInterval(this.onPoll.bind(this), POLL_INTERVAL);
onInit () {
this.onPollInterval = setInterval(this.onPoll.bind(this), POLL_INTERVAL)
if (this.getClass() == 'sensor') {
this.setClass('socket');
console.log('Changed sensor to socket.');
this.setClass('socket')
console.log('Changed sensor to socket.')
}
}

onDeleted() {
if( this.onPollInterval ) {
clearInterval(this.onPollInterval);
onDeleted () {
if (this.onPollInterval) {
clearInterval(this.onPollInterval)
}
}

async onDiscoveryAvailable(discoveryResult) {
this.url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}`;
this.log(`URL: ${this.url}`);
this.onPoll();
async onDiscoveryAvailable (discoveryResult) {
this.url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}`
this.log(`URL: ${this.url}`)
this.onPoll()
}

onDiscoveryAddressChanged(discoveryResult) {
this.url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}`;
this.log(`URL: ${this.url}`);
this.onPoll();
onDiscoveryAddressChanged (discoveryResult) {
this.url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}`
this.log(`URL: ${this.url}`)
this.onPoll()
}

onPoll() {
if( !this.url ) return;
onPoll () {
if (!this.url) return

Promise.resolve().then(async () => {
const res = await fetch(`${this.url}/data`);
if( !res.ok )
throw new Error(res.statusText);
const res = await fetch(`${this.url}/data`)
if (!res.ok) { throw new Error(res.statusText) }

const data = await res.json();
const data = await res.json()

if (this.getClass() == 'sensor') {
this.setClass('socket');
console.log('Changed sensor to socket.');
this.setClass('socket')
console.log('Changed sensor to socket.')
}

// Save export data check if capabilities are present first
if (!this.hasCapability('measure_power')) {
await this.addCapability('measure_power').catch(this.error);
await this.addCapability('measure_power').catch(this.error)
}

if (this.hasCapability('measure_power.active_power_w')) {
await this.removeCapability('measure_power.active_power_w').catch(this.error);
await this.removeCapability('measure_power.active_power_w').catch(this.error)
} // remove

if (!this.hasCapability('meter_power.consumed.t1')) {
await this.addCapability('meter_power.consumed.t1').catch(this.error);
//await this.addCapability('meter_power.consumed.t2').catch(this.error);
await this.addCapability('meter_power.consumed.t1').catch(this.error)
// await this.addCapability('meter_power.consumed.t2').catch(this.error);
}

if (!this.hasCapability('measure_power.l1')) {
await this.addCapability('measure_power.l1').catch(this.error);
await this.addCapability('measure_power.l1').catch(this.error)
}

if (!this.hasCapability('rssi')) {
await this.addCapability('rssi').catch(this.error);
await this.addCapability('rssi').catch(this.error)
}

if (this.getCapabilityValue('rssi') != data.wifi_strength)
await this.setCapabilityValue('rssi', data.wifi_strength).catch(this.error);
if (this.getCapabilityValue('rssi') != data.wifi_strength) { await this.setCapabilityValue('rssi', data.wifi_strength).catch(this.error) }

// Update values 3phase kwh
// KWH 1 fase
Expand All @@ -84,45 +81,43 @@ module.exports = class HomeWizardEnergyDevice extends Homey.Device {

// First we need to check if the kwh active_power_w is negative (solar)

// if ((data.active_power_w < 0) || (data.active_power_l1_w < 0) || (this.getClass() != 'socket')) {
// if (this.getClass() != 'solarpanel') {
// await this.setClass('solarpanel').catch(this.error);
// }
// }
// if ((data.active_power_w < 0) || (data.active_power_l1_w < 0) || (this.getClass() != 'socket')) {
// if (this.getClass() != 'solarpanel') {
// await this.setClass('solarpanel').catch(this.error);
// }
// }

await this.setCapabilityValue('measure_power', data.active_power_w).catch(this.error);
//await this.setCapabilityValue('measure_power.active_power_w', data.active_power_w).catch(this.error);
await this.setCapabilityValue('meter_power.consumed.t1', data.total_power_import_t1_kwh).catch(this.error);
await this.setCapabilityValue('measure_power.l1', data.active_power_l1_w).catch(this.error);
//await this.setCapabilityValue('meter_power.consumed.t2', data.total_power_import_t2_kwh).catch(this.error);
await this.setCapabilityValue('measure_power', data.active_power_w).catch(this.error)
// await this.setCapabilityValue('measure_power.active_power_w', data.active_power_w).catch(this.error);
await this.setCapabilityValue('meter_power.consumed.t1', data.total_power_import_t1_kwh).catch(this.error)
await this.setCapabilityValue('measure_power.l1', data.active_power_l1_w).catch(this.error)
// await this.setCapabilityValue('meter_power.consumed.t2', data.total_power_import_t2_kwh).catch(this.error);

// Check to see if there is solar panel production exported if received value is more than 1 it returned back to the power grid
if (data.total_power_export_t1_kwh > 1) {
if (!this.hasCapability('meter_power.produced.t1')) {
// add production meters
await this.addCapability('meter_power.produced.t1').catch(this.error);
}
// update values for solar production
await this.setCapabilityValue('meter_power.produced.t1', data.total_power_export_t1_kwh).catch(this.error);
}
else if (data.total_power_export_t1_kwh < 1) {
await this.removeCapability('meter_power.produced.t1').catch(this.error);
if (!this.hasCapability('meter_power.produced.t1')) {
// add production meters
await this.addCapability('meter_power.produced.t1').catch(this.error)
}
// update values for solar production
await this.setCapabilityValue('meter_power.produced.t1', data.total_power_export_t1_kwh).catch(this.error)
} else if (data.total_power_export_t1_kwh < 1) {
await this.removeCapability('meter_power.produced.t1').catch(this.error)
}

// aggregated meter for Power by the hour support
if (!this.hasCapability('meter_power')) {
await this.addCapability('meter_power').catch(this.error);
await this.addCapability('meter_power').catch(this.error)
}
// update calculated value which is sum of import deducted by the sum of the export this overall kwh number is used for Power by the hour app
await this.setCapabilityValue('meter_power', (data.total_power_import_t1_kwh-data.total_power_export_t1_kwh)).catch(this.error);
await this.setCapabilityValue('meter_power', (data.total_power_import_t1_kwh - data.total_power_export_t1_kwh)).catch(this.error)
})
.then(() => {
this.setAvailable().catch(this.error);
this.setAvailable().catch(this.error)
})
.catch(err => {
this.error(err);
this.setUnavailable(err).catch(this.error);
this.error(err)
this.setUnavailable(err).catch(this.error)
})
}

}
52 changes: 24 additions & 28 deletions drivers/SDM230/driver.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
'use strict';
'use strict'

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

module.exports = class HomeWizardEnergyDriver extends Homey.Driver {
async onPairListDevices () {
const discoveryStrategy = this.getDiscoveryStrategy()
const discoveryResults = discoveryStrategy.getDiscoveryResults()

async onPairListDevices() {

const discoveryStrategy = this.getDiscoveryStrategy();
const discoveryResults = discoveryStrategy.getDiscoveryResults();

const devices = [];
await Promise.all(Object.values(discoveryResults).map(async discoveryResult => {
try {
const url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}/data`;
const res = await fetch(url);
if( !res.ok )
throw new Error(res.statusText);

const data = await res.json();
devices.push({
name: data.meter_model,
data: {
id: discoveryResult.id,
},
})
} catch( err ) {
this.error(discoveryResult.id, err);
}
}));
return devices;
const devices = []
await Promise.all(Object.values(discoveryResults).map(async discoveryResult => {
try {
const url = `http://${discoveryResult.address}:${discoveryResult.port}${discoveryResult.txt.path}/data`
const res = await fetch(url)
if (!res.ok) { throw new Error(res.statusText) }

const data = await res.json()
devices.push({
name: data.meter_model,
data: {
id: discoveryResult.id
}
})
} catch (err) {
this.error(discoveryResult.id, err)
}
}))
return devices
}
}
Loading