Skip to content

Commit

Permalink
feat(mqtt): Added sw_version to mqtt autodiscovery to fix #568
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jul 30, 2020
1 parent 4f7ec2c commit a23d5af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/MqttClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ class MqttClient {
manufacturer: this.model.getManufacturerName(),
model: this.model.getModelName(),
name: this.identifier,
identifiers: [this.identifier]
identifiers: [this.identifier],
sw_version: this.model.getValetudoVersion()
},
schema: "state",
supported_features: [
Expand Down
24 changes: 24 additions & 0 deletions lib/miio/Model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require("path");
const fs = require("fs");
const Logger = require("../Logger");

Expand Down Expand Up @@ -162,6 +163,29 @@ class Model {
getIP() {
return this.config.ip;
}

/**
* TODO Maybe model isn't the right place, but it's better than copy-paste for now
*
* @public
* @returns {string}
*/
getValetudoVersion() {
let valetudoVersion = "?"; //Could not read ../package.json

try {
const rootDirectory = path.resolve(__dirname, "../..");
const packageContent = fs.readFileSync(rootDirectory + "/package.json", {"encoding": "utf-8"});

if (packageContent) {
valetudoVersion = JSON.parse(packageContent.toString()).version;
}
} catch (e) {
//intentional
}

return valetudoVersion;
}
}


Expand Down
14 changes: 1 addition & 13 deletions lib/webserver/ApiRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,9 @@ const ApiRouter = function (webserver) {
});

router.get("/fw_version", async (req, res) => {
let valetudoVersion = "?"; //Could not read ../package.json
let valetudoVersion = model.getValetudoVersion();
let firmwareVersion = "?";

try {
const rootDirectory = path.resolve(__dirname, "../..");
const packageContent = fs.readFileSync(rootDirectory + "/package.json", {"encoding": "utf-8"});

if (packageContent) {
valetudoVersion = JSON.parse(packageContent.toString()).version;
}
} catch (e) {
//intentional
}


if (model.isEmbedded()) {
try {
let data = await fs.promises.readFile("/etc/os-release");
Expand Down

0 comments on commit a23d5af

Please sign in to comment.