@rfranzoi/scrypted-mqtt-securitysystem 1.0.19 → 1.0.21

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.
@@ -34083,9 +34083,6 @@ function normalize(s) {
34083
34083
  function clamp(n, min, max) {
34084
34084
  return Math.max(min, Math.min(max, n));
34085
34085
  }
34086
- function delay(ms) {
34087
- return new Promise(res => setTimeout(res, ms));
34088
- }
34089
34086
  /** SecuritySystem outgoing defaults (PAI-like) */
34090
34087
  const DEFAULT_OUTGOING = {
34091
34088
  [sdk_1.SecuritySystemMode.Disarmed]: 'disarm',
@@ -34115,7 +34112,7 @@ class BaseMqttSensor extends sdk_1.ScryptedDeviceBase {
34115
34112
  constructor(nativeId, cfg) {
34116
34113
  super(nativeId);
34117
34114
  this.cfg = cfg;
34118
- this.online = this.online ?? true;
34115
+ // ⚠️ Non impostare stati qui: il discovery non è ancora completato
34119
34116
  }
34120
34117
  /** Called by parent on each MQTT message */
34121
34118
  handleMqtt(topic, payload) {
@@ -34144,7 +34141,9 @@ class BaseMqttSensor extends sdk_1.ScryptedDeviceBase {
34144
34141
  this.batteryLevel = n;
34145
34142
  }
34146
34143
  else if (topic === this.cfg.topics.lowBattery && !this.cfg.topics.batteryLevel) {
34147
- // sintetizza se non c'è batteryLevel
34144
+ // Solo se abbiamo il topic lowBattery e NON c'è un batteryLevel numerico:
34145
+ // True -> 10% (warning)
34146
+ // False -> 100% (ok)
34148
34147
  this.batteryLevel = truthy(np) ? 10 : 100;
34149
34148
  }
34150
34149
  // primary handled by subclasses
@@ -34208,7 +34207,7 @@ class ParadoxMqttSecuritySystem extends sdk_1.ScryptedDeviceBase {
34208
34207
  this.online = this.online ?? false;
34209
34208
  // Load sensors config and announce devices
34210
34209
  this.loadSensorsFromStorage();
34211
- this.discoverSensors();
34210
+ this.discoverSensors().catch(e => this.console.error('discoverSensors error', e));
34212
34211
  // Connect on start
34213
34212
  this.connectMqtt().catch(e => this.console.error('MQTT connect error:', e));
34214
34213
  // chiusura pulita del client MQTT ai reload/stop del plugin
@@ -34276,7 +34275,7 @@ class ParadoxMqttSecuritySystem extends sdk_1.ScryptedDeviceBase {
34276
34275
  async getDevice(nativeId) {
34277
34276
  return this.devices.get(nativeId);
34278
34277
  }
34279
- async releaseDevice(id, nativeId) {
34278
+ async releaseDevice(_id, nativeId) {
34280
34279
  try {
34281
34280
  const dev = this.devices.get(nativeId);
34282
34281
  if (dev) {
@@ -34303,75 +34302,71 @@ class ParadoxMqttSecuritySystem extends sdk_1.ScryptedDeviceBase {
34303
34302
  this.sensorsCfg = [];
34304
34303
  }
34305
34304
  }
34306
- /** ===== discoverSensors con interfacce dinamiche (Battery/Tamper opzionali) ===== */
34305
+ /** ===== discoverSensors: annuncia PRIMA, istanzia DOPO ===== */
34307
34306
  async discoverSensors() {
34307
+ // 1) Prepara i manifest (niente istanze qui)
34308
34308
  const manifests = this.sensorsCfg.map(cfg => {
34309
34309
  const nativeId = `sensor:${cfg.id}`;
34310
34310
  const t = cfg.topics || {};
34311
- // Interfacce di base: Online sempre
34312
- const interfaces = [sdk_1.ScryptedInterface.Online];
34313
- // Aggiungi primaria in base al kind
34314
- switch (cfg.kind) {
34315
- case 'contact':
34316
- interfaces.unshift(sdk_1.ScryptedInterface.EntrySensor);
34317
- break;
34318
- case 'motion':
34319
- interfaces.unshift(sdk_1.ScryptedInterface.MotionSensor);
34320
- break;
34321
- case 'occupancy':
34322
- interfaces.unshift(sdk_1.ScryptedInterface.OccupancySensor);
34323
- break;
34324
- }
34325
- // Tamper solo se presente il topic
34311
+ const interfaces = [
34312
+ sdk_1.ScryptedInterface.Online,
34313
+ ];
34314
+ // Tamper solo se c'è un topic tamper
34326
34315
  if (t.tamper)
34327
34316
  interfaces.push(sdk_1.ScryptedInterface.TamperSensor);
34328
- // Battery solo se presente batteryLevel o lowBattery
34329
- if (t.batteryLevel || t.lowBattery)
34317
+ // Interfaccia primaria
34318
+ if (cfg.kind === 'contact')
34319
+ interfaces.unshift(sdk_1.ScryptedInterface.EntrySensor);
34320
+ else if (cfg.kind === 'motion')
34321
+ interfaces.unshift(sdk_1.ScryptedInterface.MotionSensor);
34322
+ else
34323
+ interfaces.unshift(sdk_1.ScryptedInterface.OccupancySensor);
34324
+ // Battery solo se previsto
34325
+ if (t.batteryLevel || t.lowBattery) {
34330
34326
  interfaces.push(sdk_1.ScryptedInterface.Battery);
34331
- // Tipo generico "Sensor": le capacità sono date dalle interfacce
34332
- const type = sdk_1.ScryptedDeviceType.Sensor;
34333
- // create/update instance
34334
- let dev = this.devices.get(nativeId);
34335
- if (!dev) {
34336
- if (cfg.kind === 'contact')
34337
- dev = new ContactMqttSensor(nativeId, cfg);
34338
- else if (cfg.kind === 'motion')
34339
- dev = new MotionMqttSensor(nativeId, cfg);
34340
- else
34341
- dev = new OccupancyMqttSensor(nativeId, cfg);
34342
- this.devices.set(nativeId, dev);
34343
- }
34344
- else {
34345
- dev.cfg = cfg;
34346
- // se non c'è più batteria nei topic, pulisci eventuale valore residuo
34347
- if (!(t.batteryLevel || t.lowBattery)) {
34348
- try {
34349
- dev.batteryLevel = undefined;
34350
- }
34351
- catch { }
34352
- }
34353
34327
  }
34354
34328
  return {
34355
34329
  nativeId,
34356
34330
  name: cfg.name,
34357
- type,
34331
+ type: sdk_1.ScryptedDeviceType.Sensor,
34358
34332
  interfaces,
34359
34333
  };
34360
34334
  });
34361
- // Annuncio in batch se disponibile, altrimenti uno per volta con un piccolo delay
34362
- const dm = deviceManager;
34363
- if (typeof dm.onDevicesChanged === 'function') {
34364
- dm.onDevicesChanged({ devices: manifests });
34335
+ // 2) Annuncia i device
34336
+ const dmAny = deviceManager;
34337
+ if (typeof dmAny.onDevicesChanged === 'function') {
34338
+ dmAny.onDevicesChanged({ devices: manifests });
34365
34339
  this.console.log('Annunciati (batch):', manifests.map(m => m.nativeId).join(', '));
34366
34340
  }
34367
34341
  else {
34368
34342
  for (const m of manifests) {
34369
34343
  deviceManager.onDeviceDiscovered(m);
34370
34344
  this.console.log('Annunciato:', m.nativeId);
34371
- await delay(50);
34372
34345
  }
34373
34346
  }
34374
- // Rimuovi eventuali sensori non più presenti
34347
+ // 3) Istanzia/aggiorna le classi DOPO l’annuncio
34348
+ for (const cfg of this.sensorsCfg) {
34349
+ const nativeId = `sensor:${cfg.id}`;
34350
+ let dev = this.devices.get(nativeId);
34351
+ if (!dev) {
34352
+ if (cfg.kind === 'contact')
34353
+ dev = new ContactMqttSensor(nativeId, cfg);
34354
+ else if (cfg.kind === 'motion')
34355
+ dev = new MotionMqttSensor(nativeId, cfg);
34356
+ else
34357
+ dev = new OccupancyMqttSensor(nativeId, cfg);
34358
+ this.devices.set(nativeId, dev);
34359
+ }
34360
+ else {
34361
+ dev.cfg = cfg;
34362
+ }
34363
+ // ★ Default “OK” se abbiamo Battery ma nessun valore ancora ricevuto
34364
+ const hasBattery = !!(cfg.topics.batteryLevel || cfg.topics.lowBattery);
34365
+ if (hasBattery && dev.batteryLevel === undefined) {
34366
+ dev.batteryLevel = 100;
34367
+ }
34368
+ }
34369
+ // 4) Rimuovi quelli spariti
34375
34370
  const announced = new Set(manifests.map(m => m.nativeId));
34376
34371
  for (const [nativeId] of this.devices) {
34377
34372
  if (!announced.has(nativeId)) {
@@ -34415,14 +34410,14 @@ class ParadoxMqttSecuritySystem extends sdk_1.ScryptedDeviceBase {
34415
34410
  }
34416
34411
  // sensors
34417
34412
  for (const s of this.sensorsCfg) {
34418
- const t = s.topics;
34413
+ const t = s.topics || {};
34419
34414
  [t.contact, t.motion, t.occupancy, t.batteryLevel, t.lowBattery, t.tamper, t.online]
34420
34415
  .filter(Boolean)
34421
34416
  .forEach(x => subs.add(String(x)));
34422
34417
  }
34423
34418
  return Array.from(subs);
34424
34419
  }
34425
- async connectMqtt(reconnect = false) {
34420
+ async connectMqtt(_reconnect = false) {
34426
34421
  const subs = this.collectAllSubscriptions();
34427
34422
  if (!subs.length && !this.storage.getItem('topicSetTarget')) {
34428
34423
  this.console.warn('Configura almeno un topic nelle impostazioni.');
package/dist/plugin.zip CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rfranzoi/scrypted-mqtt-securitysystem",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "Scrypted plugin: Paradox Security System via MQTT (PAI/PAI-MQTT style).",
5
5
  "license": "MIT",
6
6
  "main": "dist/main.nodejs.js",