@rfranzoi/scrypted-mqtt-securitysystem 1.0.41 → 1.0.42

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.
@@ -34024,30 +34024,46 @@ function socketOnError() {
34024
34024
 
34025
34025
  "use strict";
34026
34026
 
34027
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
34028
+ if (k2 === undefined) k2 = k;
34029
+ var desc = Object.getOwnPropertyDescriptor(m, k);
34030
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
34031
+ desc = { enumerable: true, get: function() { return m[k]; } };
34032
+ }
34033
+ Object.defineProperty(o, k2, desc);
34034
+ }) : (function(o, m, k, k2) {
34035
+ if (k2 === undefined) k2 = k;
34036
+ o[k2] = m[k];
34037
+ }));
34038
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
34039
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
34040
+ }) : function(o, v) {
34041
+ o["default"] = v;
34042
+ });
34043
+ var __importStar = (this && this.__importStar) || (function () {
34044
+ var ownKeys = function(o) {
34045
+ ownKeys = Object.getOwnPropertyNames || function (o) {
34046
+ var ar = [];
34047
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
34048
+ return ar;
34049
+ };
34050
+ return ownKeys(o);
34051
+ };
34052
+ return function (mod) {
34053
+ if (mod && mod.__esModule) return mod;
34054
+ var result = {};
34055
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34056
+ __setModuleDefault(result, mod);
34057
+ return result;
34058
+ };
34059
+ })();
34027
34060
  var __importDefault = (this && this.__importDefault) || function (mod) {
34028
34061
  return (mod && mod.__esModule) ? mod : { "default": mod };
34029
34062
  };
34030
34063
  Object.defineProperty(exports, "__esModule", ({ value: true }));
34031
- // --- Silence ONLY the optional sdk.json warning from @scrypted/sdk ---
34032
- const __origConsoleError = console.error.bind(console);
34033
- console.error = (...args) => {
34034
- const first = args?.[0];
34035
- const msg = typeof first === 'string' ? first : (first?.message || '');
34036
- if (typeof msg === 'string' && msg.includes('failed to load custom interface descriptors')) {
34037
- // swallow just this warning
34038
- return;
34039
- }
34040
- __origConsoleError(...args);
34041
- };
34042
- // Carica lo SDK (non tipizziamo qui per non perdere le proprietà runtime)
34043
- const sdk = __webpack_require__(/*! @scrypted/sdk */ "./node_modules/@scrypted/sdk/dist/src/index.js");
34044
- // Valori runtime (enum/classi/manager) dal modulo SDK
34045
- const { ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, // valore (enum)
34046
- SecuritySystemMode, // valore (enum)
34047
- systemManager,
34048
- // ⚠️ NON destrutturare deviceManager: va letto sempre "al volo" da sdk.deviceManager.
34049
- } = sdk;
34064
+ const sdk_1 = __importStar(__webpack_require__(/*! @scrypted/sdk */ "./node_modules/@scrypted/sdk/dist/src/index.js"));
34050
34065
  const mqtt_1 = __importDefault(__webpack_require__(/*! mqtt */ "./node_modules/mqtt/build/index.js"));
34066
+ const { systemManager, deviceManager } = sdk_1.default;
34051
34067
  /** utils */
34052
34068
  function truthy(v) {
34053
34069
  if (!v)
@@ -34067,13 +34083,12 @@ function normalize(s) {
34067
34083
  function clamp(n, min, max) {
34068
34084
  return Math.max(min, Math.min(max, n));
34069
34085
  }
34070
- /** SecuritySystem outgoing defaults (PAI-like)
34071
- * Nota: usare number come chiave evita stranezze con gli enum in TS. */
34086
+ /** SecuritySystem outgoing defaults (PAI-like) */
34072
34087
  const DEFAULT_OUTGOING = {
34073
- [SecuritySystemMode.Disarmed]: 'disarm',
34074
- [SecuritySystemMode.HomeArmed]: 'arm_home',
34075
- [SecuritySystemMode.AwayArmed]: 'arm_away',
34076
- [SecuritySystemMode.NightArmed]: 'arm_night',
34088
+ [sdk_1.SecuritySystemMode.Disarmed]: 'disarm',
34089
+ [sdk_1.SecuritySystemMode.HomeArmed]: 'arm_home',
34090
+ [sdk_1.SecuritySystemMode.AwayArmed]: 'arm_away',
34091
+ [sdk_1.SecuritySystemMode.NightArmed]: 'arm_night',
34077
34092
  };
34078
34093
  /** Parse incoming payload -> final mode (ignore transition states) */
34079
34094
  function payloadToMode(payload) {
@@ -34081,22 +34096,23 @@ function payloadToMode(payload) {
34081
34096
  return;
34082
34097
  const p = normalize(payload.toString());
34083
34098
  if (['disarm', 'disarmed', 'off', '0', 'idle', 'ready'].includes(p))
34084
- return SecuritySystemMode.Disarmed;
34099
+ return sdk_1.SecuritySystemMode.Disarmed;
34085
34100
  if (['arm_home', 'home', 'stay', 'armed_home'].includes(p))
34086
- return SecuritySystemMode.HomeArmed;
34101
+ return sdk_1.SecuritySystemMode.HomeArmed;
34087
34102
  if (['arm_away', 'away', 'armed_away', 'away_armed'].includes(p))
34088
- return SecuritySystemMode.AwayArmed;
34103
+ return sdk_1.SecuritySystemMode.AwayArmed;
34089
34104
  if (['arm_night', 'night', 'armed_night', 'sleep', 'arm_sleep', 'armed_sleep'].includes(p))
34090
- return SecuritySystemMode.NightArmed;
34105
+ return sdk_1.SecuritySystemMode.NightArmed;
34091
34106
  // transitori: non cambiano il mode
34092
34107
  if (['entry_delay', 'exit_delay', 'pending', 'arming', 'disarming'].includes(p))
34093
34108
  return undefined;
34094
34109
  return undefined;
34095
34110
  }
34096
- class BaseMqttSensor extends ScryptedDeviceBase {
34111
+ class BaseMqttSensor extends sdk_1.ScryptedDeviceBase {
34097
34112
  constructor(nativeId, cfg) {
34098
34113
  super(nativeId);
34099
34114
  this.cfg = cfg;
34115
+ // non impostare stati qui: l'annuncio device deve avvenire prima
34100
34116
  }
34101
34117
  /** Called by parent on each MQTT message */
34102
34118
  handleMqtt(topic, payload) {
@@ -34125,7 +34141,9 @@ class BaseMqttSensor extends ScryptedDeviceBase {
34125
34141
  this.batteryLevel = n;
34126
34142
  }
34127
34143
  else if (topic === this.cfg.topics.lowBattery && !this.cfg.topics.batteryLevel) {
34128
- // se abbiamo SOLO lowBattery (bool):
34144
+ // Solo se abbiamo lowBattery (booleano) ma NON batteryLevel:
34145
+ // True -> 10% (warning)
34146
+ // False -> 100% (ok)
34129
34147
  this.batteryLevel = truthy(np) ? 10 : 100;
34130
34148
  }
34131
34149
  // primary handled by subclasses
@@ -34133,28 +34151,37 @@ class BaseMqttSensor extends ScryptedDeviceBase {
34133
34151
  }
34134
34152
  }
34135
34153
  class ContactMqttSensor extends BaseMqttSensor {
34136
- handlePrimary(topic, np) {
34154
+ constructor(nativeId, cfg) {
34155
+ super(nativeId, cfg);
34156
+ }
34157
+ handlePrimary(topic, np, _raw) {
34137
34158
  if (topic === this.cfg.topics.contact) {
34138
34159
  this.entryOpen = truthy(np);
34139
34160
  }
34140
34161
  }
34141
34162
  }
34142
34163
  class MotionMqttSensor extends BaseMqttSensor {
34143
- handlePrimary(topic, np) {
34164
+ constructor(nativeId, cfg) {
34165
+ super(nativeId, cfg);
34166
+ }
34167
+ handlePrimary(topic, np, _raw) {
34144
34168
  if (topic === this.cfg.topics.motion) {
34145
34169
  this.motionDetected = truthy(np);
34146
34170
  }
34147
34171
  }
34148
34172
  }
34149
34173
  class OccupancyMqttSensor extends BaseMqttSensor {
34150
- handlePrimary(topic, np) {
34174
+ constructor(nativeId, cfg) {
34175
+ super(nativeId, cfg);
34176
+ }
34177
+ handlePrimary(topic, np, _raw) {
34151
34178
  if (topic === this.cfg.topics.occupancy) {
34152
34179
  this.occupied = truthy(np);
34153
34180
  }
34154
34181
  }
34155
34182
  }
34156
34183
  /** ----------------- Main Plugin ----------------- */
34157
- class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34184
+ class ParadoxMqttSecuritySystem extends sdk_1.ScryptedDeviceBase {
34158
34185
  constructor() {
34159
34186
  super();
34160
34187
  // sensor management
@@ -34163,26 +34190,26 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34163
34190
  // (facoltativo) Imposta il device type in UI
34164
34191
  setTimeout(() => {
34165
34192
  try {
34166
- systemManager.getDeviceById(this.id)?.setType?.(ScryptedDeviceType.SecuritySystem);
34193
+ systemManager.getDeviceById(this.id)?.setType?.(sdk_1.ScryptedDeviceType.SecuritySystem);
34167
34194
  }
34168
34195
  catch { }
34169
34196
  });
34170
34197
  // Default state
34171
34198
  this.securitySystemState = this.securitySystemState || {
34172
- mode: SecuritySystemMode.Disarmed,
34199
+ mode: sdk_1.SecuritySystemMode.Disarmed,
34173
34200
  supportedModes: [
34174
- SecuritySystemMode.Disarmed,
34175
- SecuritySystemMode.HomeArmed,
34176
- SecuritySystemMode.AwayArmed,
34177
- SecuritySystemMode.NightArmed,
34201
+ sdk_1.SecuritySystemMode.Disarmed,
34202
+ sdk_1.SecuritySystemMode.HomeArmed,
34203
+ sdk_1.SecuritySystemMode.AwayArmed,
34204
+ sdk_1.SecuritySystemMode.NightArmed,
34178
34205
  ],
34179
34206
  };
34180
34207
  this.online = this.online ?? false;
34181
34208
  // Load sensors config and announce devices
34182
34209
  this.loadSensorsFromStorage();
34183
- this.discoverSensors().catch((e) => this.console.error('discoverSensors error', e));
34210
+ this.discoverSensors().catch(e => this.console.error('discoverSensors error', e));
34184
34211
  // Connect on start
34185
- this.connectMqtt().catch((e) => this.console.error('MQTT connect error:', e));
34212
+ this.connectMqtt().catch(e => this.console.error('MQTT connect error:', e));
34186
34213
  // chiusura pulita del client MQTT ai reload/stop del plugin
34187
34214
  try {
34188
34215
  process.once('SIGTERM', () => { try {
@@ -34292,7 +34319,7 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34292
34319
  this.saveSensorsToStorage();
34293
34320
  try {
34294
34321
  this.devices.delete(`sensor:${sid}`);
34295
- sdk?.deviceManager?.onDeviceRemoved?.(`sensor:${sid}`);
34322
+ deviceManager.onDeviceRemoved?.(`sensor:${sid}`);
34296
34323
  }
34297
34324
  catch { }
34298
34325
  // pulisci flag
@@ -34338,7 +34365,7 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34338
34365
  this.devices.delete(nativeId);
34339
34366
  }
34340
34367
  try {
34341
- sdk?.deviceManager?.onDeviceRemoved?.(nativeId);
34368
+ deviceManager.onDeviceRemoved?.(nativeId);
34342
34369
  }
34343
34370
  catch { }
34344
34371
  }
@@ -34358,48 +34385,42 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34358
34385
  this.sensorsCfg = [];
34359
34386
  }
34360
34387
  }
34361
- /** ===== discoverSensors: annuncia PRIMA, istanzia DOPO (con retry se manager non pronto) ===== */
34388
+ /** ===== discoverSensors: annuncia PRIMA, istanzia DOPO ===== */
34362
34389
  async discoverSensors() {
34363
- const dmAny = sdk?.deviceManager;
34364
- if (!dmAny) {
34365
- this.console.warn('deviceManager not ready yet, retrying in 1s…');
34366
- setTimeout(() => this.discoverSensors().catch(e => this.console.error('discoverSensors retry error', e)), 1000);
34367
- return;
34368
- }
34369
- // 1) Prepara i manifest
34390
+ // 1) Prepara i manifest (niente istanze qui)
34370
34391
  const manifests = this.sensorsCfg.map(cfg => {
34371
34392
  const nativeId = `sensor:${cfg.id}`;
34372
34393
  const t = cfg.topics || {};
34373
- const interfaces = [ScryptedInterface.Online];
34394
+ const interfaces = [sdk_1.ScryptedInterface.Online];
34395
+ // Tamper solo se c'è un topic tamper
34374
34396
  if (t.tamper)
34375
- interfaces.push(ScryptedInterface.TamperSensor);
34397
+ interfaces.push(sdk_1.ScryptedInterface.TamperSensor);
34398
+ // Interfaccia primaria
34376
34399
  if (cfg.kind === 'contact')
34377
- interfaces.unshift(ScryptedInterface.EntrySensor);
34400
+ interfaces.unshift(sdk_1.ScryptedInterface.EntrySensor);
34378
34401
  else if (cfg.kind === 'motion')
34379
- interfaces.unshift(ScryptedInterface.MotionSensor);
34402
+ interfaces.unshift(sdk_1.ScryptedInterface.MotionSensor);
34380
34403
  else
34381
- interfaces.unshift(ScryptedInterface.OccupancySensor);
34382
- if (t.batteryLevel || t.lowBattery)
34383
- interfaces.push(ScryptedInterface.Battery);
34384
- return { nativeId, name: cfg.name, type: ScryptedDeviceType.Sensor, interfaces };
34404
+ interfaces.unshift(sdk_1.ScryptedInterface.OccupancySensor);
34405
+ // Battery solo se previsto
34406
+ if (t.batteryLevel || t.lowBattery) {
34407
+ interfaces.push(sdk_1.ScryptedInterface.Battery);
34408
+ }
34409
+ return { nativeId, name: cfg.name, type: sdk_1.ScryptedDeviceType.Sensor, interfaces };
34385
34410
  });
34386
34411
  // 2) Annuncio
34412
+ const dmAny = deviceManager;
34387
34413
  if (typeof dmAny.onDevicesChanged === 'function') {
34388
34414
  dmAny.onDevicesChanged({ devices: manifests });
34389
34415
  this.console.log('Annunciati (batch):', manifests.map(m => m.nativeId).join(', '));
34390
34416
  }
34391
- else if (typeof dmAny.onDeviceDiscovered === 'function') {
34417
+ else {
34392
34418
  for (const m of manifests) {
34393
- dmAny.onDeviceDiscovered(m);
34419
+ deviceManager.onDeviceDiscovered(m);
34394
34420
  this.console.log('Annunciato:', m.nativeId);
34395
34421
  }
34396
34422
  }
34397
- else {
34398
- this.console.warn('deviceManager has no discovery methods yet, retrying in 1s…');
34399
- setTimeout(() => this.discoverSensors().catch(e => this.console.error('discoverSensors retry error', e)), 1000);
34400
- return;
34401
- }
34402
- // 3) Istanzia/aggiorna
34423
+ // 3) Istanzia/aggiorna DOPO l’annuncio
34403
34424
  for (const cfg of this.sensorsCfg) {
34404
34425
  const nativeId = `sensor:${cfg.id}`;
34405
34426
  let dev = this.devices.get(nativeId);
@@ -34415,6 +34436,7 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34415
34436
  else {
34416
34437
  dev.cfg = cfg;
34417
34438
  }
34439
+ // Default “OK” se abbiamo Battery ma nessun valore ancora ricevuto
34418
34440
  const hasBattery = !!(cfg.topics.batteryLevel || cfg.topics.lowBattery);
34419
34441
  if (hasBattery && dev.batteryLevel === undefined) {
34420
34442
  dev.batteryLevel = 100;
@@ -34426,7 +34448,7 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34426
34448
  if (!announced.has(nativeId)) {
34427
34449
  try {
34428
34450
  this.devices.delete(nativeId);
34429
- sdk?.deviceManager?.onDeviceRemoved?.(nativeId);
34451
+ deviceManager.onDeviceRemoved?.(nativeId);
34430
34452
  this.console.log('Rimosso:', nativeId);
34431
34453
  }
34432
34454
  catch { }
@@ -34529,14 +34551,14 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34529
34551
  if (topic === tCurrent) {
34530
34552
  const mode = payloadToMode(payload);
34531
34553
  const isAlarm = ['alarm', 'triggered'].includes(np);
34532
- const current = this.securitySystemState || { mode: SecuritySystemMode.Disarmed };
34554
+ const current = this.securitySystemState || { mode: sdk_1.SecuritySystemMode.Disarmed };
34533
34555
  const newState = {
34534
34556
  mode: mode ?? current.mode,
34535
34557
  supportedModes: current.supportedModes ?? [
34536
- SecuritySystemMode.Disarmed,
34537
- SecuritySystemMode.HomeArmed,
34538
- SecuritySystemMode.AwayArmed,
34539
- SecuritySystemMode.NightArmed,
34558
+ sdk_1.SecuritySystemMode.Disarmed,
34559
+ sdk_1.SecuritySystemMode.HomeArmed,
34560
+ sdk_1.SecuritySystemMode.AwayArmed,
34561
+ sdk_1.SecuritySystemMode.NightArmed,
34540
34562
  ],
34541
34563
  triggered: isAlarm || undefined,
34542
34564
  };
@@ -34580,17 +34602,17 @@ class ParadoxMqttSecuritySystem extends ScryptedDeviceBase {
34580
34602
  this.publishSetTarget(payload);
34581
34603
  }
34582
34604
  async disarmSecuritySystem() {
34583
- const payload = this.getOutgoing(SecuritySystemMode.Disarmed);
34605
+ const payload = this.getOutgoing(sdk_1.SecuritySystemMode.Disarmed);
34584
34606
  this.console.log('disarmSecuritySystem ->', payload);
34585
- this.pendingTarget = SecuritySystemMode.Disarmed;
34607
+ this.pendingTarget = sdk_1.SecuritySystemMode.Disarmed;
34586
34608
  this.publishSetTarget(payload);
34587
34609
  }
34588
34610
  getOutgoing(mode) {
34589
34611
  const map = {
34590
- [SecuritySystemMode.Disarmed]: this.storage.getItem('payloadDisarm') || DEFAULT_OUTGOING[SecuritySystemMode.Disarmed],
34591
- [SecuritySystemMode.HomeArmed]: this.storage.getItem('payloadHome') || DEFAULT_OUTGOING[SecuritySystemMode.HomeArmed],
34592
- [SecuritySystemMode.AwayArmed]: this.storage.getItem('payloadAway') || DEFAULT_OUTGOING[SecuritySystemMode.AwayArmed],
34593
- [SecuritySystemMode.NightArmed]: this.storage.getItem('payloadNight') || DEFAULT_OUTGOING[SecuritySystemMode.NightArmed],
34612
+ [sdk_1.SecuritySystemMode.Disarmed]: this.storage.getItem('payloadDisarm') || DEFAULT_OUTGOING[sdk_1.SecuritySystemMode.Disarmed],
34613
+ [sdk_1.SecuritySystemMode.HomeArmed]: this.storage.getItem('payloadHome') || DEFAULT_OUTGOING[sdk_1.SecuritySystemMode.HomeArmed],
34614
+ [sdk_1.SecuritySystemMode.AwayArmed]: this.storage.getItem('payloadAway') || DEFAULT_OUTGOING[sdk_1.SecuritySystemMode.AwayArmed],
34615
+ [sdk_1.SecuritySystemMode.NightArmed]: this.storage.getItem('payloadNight') || DEFAULT_OUTGOING[sdk_1.SecuritySystemMode.NightArmed],
34594
34616
  };
34595
34617
  return map[mode];
34596
34618
  }
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.41",
3
+ "version": "1.0.42",
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",