Skip to content

Commit

Permalink
Merge pull request #45 from burnacid/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
burnacid authored Apr 4, 2022
2 parents f46c4e5 + 8cc74ce commit 90578cf
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 88 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
node_modules

# Added by Homey CLI
/.homeybuild/
9 changes: 9 additions & 0 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,14 @@
},
"0.5.4": {
"en": "Added improved error reporting and catch unexpected url input"
},
"0.6.0": {
"en": "Added ZTATS flow trigger to usage device"
},
"0.6.1": {
"en": "Added ZTATZ update flow trigger to usage device"
},
"0.6.2": {
"en": "Added ZTATZ update flow trigger to usage device"
}
}
79 changes: 77 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "com.burnacid.ztatz",
"version": "0.5.4",
"version": "0.6.2",
"compatibility": ">=5.0.0",
"sdk": 3,
"brandColor": "#384042",
Expand Down Expand Up @@ -55,6 +55,16 @@
"decimals": 2,
"insights": true
},
"version_number": {
"type": "number",
"title": {
"en": "Version Number",
"nl": "Versie Nummer"
},
"getable": true,
"setable": false,
"uiComponent": null
},
"tariff_high": {
"type": "boolean",
"title": {
Expand Down Expand Up @@ -120,7 +130,8 @@
"meter_power.consumedL1",
"meter_power.consumedL2",
"meter_gas.current",
"tariff_high"
"tariff_high",
"version_number"
],
"energy": {
"cumulative": true
Expand Down Expand Up @@ -764,6 +775,70 @@
}
]
},
{
"id": "version_number.changed",
"title": {
"nl": "Een nieuwe versie van ZTATZ is beschikbaar",
"en": "A new version of ZTATZ is available"
},
"tokens": [
{
"name": "version",
"type": "string",
"title": {
"en": "Version",
"nl": "Versie"
}
},
{
"name": "version_number",
"type": "number",
"title": {
"en": "Version Number",
"nl": "Versie Nummer"
}
},
{
"name": "version_url",
"type": "string",
"title": {
"en": "Version URL",
"nl": "Versie URL"
}
},
{
"name": "version_date",
"type": "string",
"title": {
"en": "Version Release Date",
"nl": "Versie Publicatie Datum"
}
},
{
"name": "version_name",
"type": "string",
"title": {
"en": "Version Name",
"nl": "Versie Naam"
}
},
{
"name": "current_version_number",
"type": "number",
"title": {
"en": "Current Version Number",
"nl": "Huidige Versie Nummer"
}
}
],
"args": [
{
"name": "ChangedVersion",
"type": "device",
"filter": "driver_id=ztatz_p1_Usage"
}
]
},
{
"id": "meter_power.generatedL1.changed",
"title": {
Expand Down
42 changes: 40 additions & 2 deletions drivers/ztatz_p1_Usage/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Device = require('../../lib/Device.js');
const boolean = require('boolean');

const refreshTimeout = 1000 * 10; // 10 sec
const refreshTimeoutStats = 1000 * 300; // 5 min

module.exports = class ztatzP1SmartMeterDevice extends Device {

Expand All @@ -17,32 +18,40 @@ module.exports = class ztatzP1SmartMeterDevice extends Device {
//this._registerFlowCardTriggers();

// Update server data
//this._syncDevice();
this.intervalStatsId = setInterval(this._syncStats.bind(this), refreshTimeoutStats);

// Set update timer
this.intervalId = setInterval(this._syncDevice.bind(this), refreshTimeout);
this.setSettings({
url: device.url,
});

// Add version Capablity
if(!this.hasCapability('version_number')){
this.addCapability('version_number');
}

console.log("register flow triggers");
// register Flow triggers
this._flowTriggerPowerMeterL1Changed = this.homey.flow.getDeviceTriggerCard('meter_power.consumedL1.changed');
this._flowTriggerPowerMeterL2Changed = this.homey.flow.getDeviceTriggerCard('meter_power.consumedL2.changed');
this._flowTriggerGasMeterChanged = this.homey.flow.getDeviceTriggerCard('meter_gas.current.changed');
this._flowTriggerVersionChanged = this.homey.flow.getDeviceTriggerCard('version_number.changed');

this._syncStats();
}

async _deleteDevice() {
this.log('_deleteDevice');

clearInterval(this.intervalStatsId);
clearInterval(this.intervalId);
}

// Update server data
async _syncDevice() {
try {
let status = await this.api.getSmartmeter();

if (status.length != 0) {
this.setAvailable();

Expand Down Expand Up @@ -76,6 +85,35 @@ module.exports = class ztatzP1SmartMeterDevice extends Device {
}
}

// Update stats
async _syncStats() {
try {
let status = await this.api.getStatus();
let configuration = await this.api.getConfiguration();

if (status.length != 0 && configuration != 0) {
this.setAvailable();

let lastVersion = this.api.filterValueByLabel(status,"Laatste P1 monitor versie:")[0]
let lastVersionText = this.api.filterValueByLabel(status,"Laatste P1 monitor versie tekst:")[0]
let lastVersionDate = this.api.filterValueByLabel(status,"Laatste P1 monitor versie datum:")[0]
let lastVersionUrl = this.api.filterValueByLabel(status,"Laatste P1 monitor versie URL:")[0]
let lastVersionNumber = this.api.filterValueByLabel(status,"Laatste P1 monitor versie nummer:")[0]

let currentVersion = this.api.filterValueByLabel(configuration,"Versie:","PARAMETER")[0]
let currentVersionNumber = this.api.filterValueByLabel(configuration,"Versie nummer:")[0]

this.changeCapabilityValue('version_number', Number(lastVersionNumber['STATUS']), this._flowTriggerVersionChanged, {'version':String(lastVersion['STATUS']),"version_number":Number(lastVersionNumber['STATUS']),"version_url":String(lastVersionUrl['STATUS']),"version_date":String(lastVersionDate['STATUS']),"version_name":String(lastVersionText['STATUS']),"current_version_number":Number(currentVersionNumber['PARAMETER'])});
} else {
this.setUnavailable('Cannot refresh / Connect');
}

} catch (error) {
this.error(error);
this.setUnavailable(error.message);
}
}

// this method is called when the Device has requested a state change (turned on or off)
//async p1_kwh_used( value, opts ) {

Expand Down
61 changes: 17 additions & 44 deletions lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,19 @@ class Api {
}

async getStatus() {
this.command = '/api/v1/status';
this.command = '/api/v1/status?json=object';

const data = await this.httpRequest();
const json = this.getJsonData(data);
if (json.length != 0) {
return json;
} else {
return false;
}
}

async getConfiguration() {
this.command = '/api/v1/configuration?json=object';

const data = await this.httpRequest();
const json = this.getJsonData(data);
Expand Down Expand Up @@ -207,50 +219,11 @@ class Api {
return false;
}

/*
async getAdditionalDomains () {
if (domain !== null) {
this.command = 'ADDITIONAL_DOMAINS?domain=' + domain + '&json=yes';
} else {
this.command = 'ADDITIONAL_DOMAINS?json=yes';
}
const data = await this.httpRequest();
return this.getJsonData(data);
}
async getAdminStats () {
this.command = 'ADMIN_STATS?json=yes';
const data = await this.httpRequest();
return this.getJsonData(data);
}
async getLicense () {
this.command = 'LICENSE?json=yes';
const data = await this.httpRequest();
return this.getJsonData(data);
}
async getPopStats (domain) {
this.command = 'POP?domain=' + domain + '&action=full_list';
let data = await this.httpRequest();
let popObj = { count: 0, usage: 0 };
if (data.length === 0) {
return popObj;
}
let lines = querystring.parse(data);
for (var user in lines) {
let userdata = querystring.parse(lines[user]);
popObj.count++;
popObj.usage += parseFloat(userdata.usage);
}
return popObj;
filterValueByLabel(obj, value) {
return obj.filter((object) => {
return object["LABEL"] == value
})
}
*/

}

Expand Down
6 changes: 3 additions & 3 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 5 additions & 30 deletions node_modules/safe-buffer/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90578cf

Please sign in to comment.