homebridge-savantlightingcn 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +40 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1252,7 +1252,8 @@ function HomeAssistantPlatform(log, config, api) {
1252
1252
  ASSITANT = this;
1253
1253
 
1254
1254
  // Ensure config is initialized with defaults if needed, now that we have the path
1255
- initializationConfig(configFile);
1255
+ if (ASSITANT) ASSITANT.log('Using config file: ' + configFile);
1256
+ initializationConfig();
1256
1257
 
1257
1258
  CONFIGDEVICE = config.deviceInfo || [];
1258
1259
  this.password = config.password;
@@ -1488,7 +1489,7 @@ function getConfigDevices() {
1488
1489
  var deviceInfo = null;
1489
1490
  for (let index = 0; index < data.platforms.length; index++) {
1490
1491
  const platform = data.platforms[index];
1491
- if (platform.name == "savant") {
1492
+ if (platform.platform == "savant") {
1492
1493
  deviceInfo = platform.deviceInfo;
1493
1494
  }
1494
1495
  }
@@ -1504,23 +1505,48 @@ function getConfigDevices() {
1504
1505
  }
1505
1506
 
1506
1507
  function setConfigDevices(deviceInfo) {
1507
- //ASSITANT.log('setConfigDevices');
1508
+ if (ASSITANT) ASSITANT.log('setConfigDevices called with ' + (deviceInfo ? deviceInfo.length : 0) + ' devices');
1508
1509
  var fs = require("fs");
1510
+ if (!configFile) {
1511
+ if (ASSITANT) ASSITANT.log.error('configFile path is null');
1512
+ return false;
1513
+ }
1514
+
1509
1515
  var checkDir = fs.existsSync(configFile);
1510
1516
  if (checkDir) {
1511
- var strData = fs.readFileSync(configFile, 'utf8');
1512
- var data = JSON.parse(strData);
1513
- for (let index = 0; index < data.platforms.length; index++) {
1514
- var platform = data.platforms[index];
1515
- if (platform.name == "savant") {
1516
- platform.deviceInfo = deviceInfo;
1517
+ try {
1518
+ var strData = fs.readFileSync(configFile, 'utf8');
1519
+ var data = JSON.parse(strData);
1520
+ var found = false;
1521
+
1522
+ if (data.platforms) {
1523
+ for (let index = 0; index < data.platforms.length; index++) {
1524
+ var platform = data.platforms[index];
1525
+ // Use platform type identifier, not user-defined name
1526
+ if (platform.platform == "savant") {
1527
+ if (ASSITANT) ASSITANT.log('Found savant platform config, updating devices...');
1528
+ platform.deviceInfo = deviceInfo;
1529
+ found = true;
1530
+ break;
1531
+ }
1532
+ }
1517
1533
  }
1534
+
1535
+ if (!found) {
1536
+ if (ASSITANT) ASSITANT.log.error('Savant platform not found in config.json');
1537
+ return false;
1538
+ }
1539
+
1540
+ strData = JSON.stringify(data, undefined, 4);
1541
+ fs.writeFileSync(configFile, strData);
1542
+ if (ASSITANT) ASSITANT.log('Config file saved successfully.');
1543
+ return true;
1544
+ } catch (e) {
1545
+ if (ASSITANT) ASSITANT.log.error('Error saving config: ' + e);
1546
+ return false;
1518
1547
  }
1519
- strData = JSON.stringify(data, undefined, 4);
1520
- //ASSITANT.log(strData);
1521
- fs.writeFileSync(configFile, strData);
1522
- return true;
1523
1548
  } else {
1549
+ if (ASSITANT) ASSITANT.log.error('Config file does not exist at: ' + configFile);
1524
1550
  return false;
1525
1551
  }
1526
1552
  }
@@ -1533,7 +1559,7 @@ function initializationConfig() {
1533
1559
  var data = JSON.parse(strData);
1534
1560
  for (let index = 0; index < data.platforms.length; index++) {
1535
1561
  var platform = data.platforms[index];
1536
- if (platform.name == "savant") {
1562
+ if (platform.platform == "savant") {
1537
1563
  if (!platform.port) {
1538
1564
  platform.port = 9899;
1539
1565
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-savantlightingcn",
3
3
  "displayName": "Savant Lighting CN",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "description": "Integrate Savant Smart Home System with HomeKit via Homebridge. Supports Lights, HVAC, Curtains, Sensors and more.",
6
6
  "homepage": "https://github.com/rickyangkai/homebridge-savantlightingcn#readme",
7
7
  "license": "MIT",