@attraccess/plugin-shelly 0.1.0 → 0.2.0

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.
package/LICENSE.md ADDED
@@ -0,0 +1,14 @@
1
+ # Lizenz von @attraccess/plugin-shelly
2
+
3
+ Copyright (c) 2025 Jan Jaap (Github: JappyJan, E-Mail: mail@janjaap.de)
4
+ Project: Attraccess
5
+
6
+ Dieses Plugin hat keine eigene Lizenz. Es steht unter der Lizenz des Attraccess-Kerns, der **Prosperity Public License 3.0 – Modified Version**. Der vollständige Text steht in der Datei [LICENSE.md im Repository-Stamm](https://github.com/Attraccess/Attraccess/blob/main/LICENSE.md).
7
+
8
+ Kurz zusammengefasst (maßgeblich ist allein der Text der Kernlizenz):
9
+
10
+ - Kostenlos für Privatpersonen und gemeinnützige Organisationen, für nicht-kommerzielle Zwecke.
11
+ - Kommerzielle Nutzer dürfen 30 Tage kostenlos evaluieren, danach ist eine kommerzielle Lizenz erforderlich.
12
+ - Forks und abgeleitete Werke nur unter exakt denselben Lizenzbedingungen.
13
+
14
+ Für kommerzielle Lizenzen: [contact@attraccess.org](mailto:contact@attraccess.org)
package/dist/index.js CHANGED
@@ -2737,19 +2737,19 @@ var require_isReadableStreamLike = __commonJS({
2737
2737
  var _ = { label: 0, sent: function() {
2738
2738
  if (t[0] & 1) throw t[1];
2739
2739
  return t[1];
2740
- }, trys: [], ops: [] }, f, y, t, g;
2740
+ }, trys: [], ops: [] }, f2, y, t, g;
2741
2741
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
2742
2742
  return this;
2743
2743
  }), g;
2744
2744
  function verb(n) {
2745
2745
  return function(v) {
2746
- return step([n, v]);
2746
+ return step2([n, v]);
2747
2747
  };
2748
2748
  }
2749
- function step(op) {
2750
- if (f) throw new TypeError("Generator is already executing.");
2749
+ function step2(op) {
2750
+ if (f2) throw new TypeError("Generator is already executing.");
2751
2751
  while (_) try {
2752
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
2752
+ if (f2 = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
2753
2753
  if (y = 0, t) op = [op[0] & 2, t.value];
2754
2754
  switch (op[0]) {
2755
2755
  case 0:
@@ -2796,7 +2796,7 @@ var require_isReadableStreamLike = __commonJS({
2796
2796
  op = [6, e];
2797
2797
  y = 0;
2798
2798
  } finally {
2799
- f = t = 0;
2799
+ f2 = t = 0;
2800
2800
  }
2801
2801
  if (op[0] & 5) throw op[1];
2802
2802
  return { value: op[0] ? op[1] : void 0, done: true };
@@ -2820,12 +2820,12 @@ var require_isReadableStreamLike = __commonJS({
2820
2820
  }
2821
2821
  function resume(n, v) {
2822
2822
  try {
2823
- step(g[n](v));
2823
+ step2(g[n](v));
2824
2824
  } catch (e) {
2825
2825
  settle(q[0][3], e);
2826
2826
  }
2827
2827
  }
2828
- function step(r) {
2828
+ function step2(r) {
2829
2829
  r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r);
2830
2830
  }
2831
2831
  function fulfill(value) {
@@ -2834,8 +2834,8 @@ var require_isReadableStreamLike = __commonJS({
2834
2834
  function reject(value) {
2835
2835
  resume("throw", value);
2836
2836
  }
2837
- function settle(f, v) {
2838
- if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]);
2837
+ function settle(f2, v) {
2838
+ if (f2(v), q.shift(), q.length) resume(q[0][0], q[0][1]);
2839
2839
  }
2840
2840
  };
2841
2841
  Object.defineProperty(exports2, "__esModule", { value: true });
@@ -2900,41 +2900,41 @@ var require_innerFrom = __commonJS({
2900
2900
  return new (P || (P = Promise))(function(resolve, reject) {
2901
2901
  function fulfilled(value) {
2902
2902
  try {
2903
- step(generator.next(value));
2903
+ step2(generator.next(value));
2904
2904
  } catch (e) {
2905
2905
  reject(e);
2906
2906
  }
2907
2907
  }
2908
2908
  function rejected(value) {
2909
2909
  try {
2910
- step(generator["throw"](value));
2910
+ step2(generator["throw"](value));
2911
2911
  } catch (e) {
2912
2912
  reject(e);
2913
2913
  }
2914
2914
  }
2915
- function step(result) {
2915
+ function step2(result) {
2916
2916
  result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
2917
2917
  }
2918
- step((generator = generator.apply(thisArg, _arguments || [])).next());
2918
+ step2((generator = generator.apply(thisArg, _arguments || [])).next());
2919
2919
  });
2920
2920
  };
2921
2921
  var __generator = exports2 && exports2.__generator || function(thisArg, body) {
2922
2922
  var _ = { label: 0, sent: function() {
2923
2923
  if (t[0] & 1) throw t[1];
2924
2924
  return t[1];
2925
- }, trys: [], ops: [] }, f, y, t, g;
2925
+ }, trys: [], ops: [] }, f2, y, t, g;
2926
2926
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
2927
2927
  return this;
2928
2928
  }), g;
2929
2929
  function verb(n) {
2930
2930
  return function(v) {
2931
- return step([n, v]);
2931
+ return step2([n, v]);
2932
2932
  };
2933
2933
  }
2934
- function step(op) {
2935
- if (f) throw new TypeError("Generator is already executing.");
2934
+ function step2(op) {
2935
+ if (f2) throw new TypeError("Generator is already executing.");
2936
2936
  while (_) try {
2937
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
2937
+ if (f2 = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
2938
2938
  if (y = 0, t) op = [op[0] & 2, t.value];
2939
2939
  switch (op[0]) {
2940
2940
  case 0:
@@ -2981,7 +2981,7 @@ var require_innerFrom = __commonJS({
2981
2981
  op = [6, e];
2982
2982
  y = 0;
2983
2983
  } finally {
2984
- f = t = 0;
2984
+ f2 = t = 0;
2985
2985
  }
2986
2986
  if (op[0] & 5) throw op[1];
2987
2987
  return { value: op[0] ? op[1] : void 0, done: true };
@@ -4536,19 +4536,19 @@ var require_generate = __commonJS({
4536
4536
  var _ = { label: 0, sent: function() {
4537
4537
  if (t[0] & 1) throw t[1];
4538
4538
  return t[1];
4539
- }, trys: [], ops: [] }, f, y, t, g;
4539
+ }, trys: [], ops: [] }, f2, y, t, g;
4540
4540
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
4541
4541
  return this;
4542
4542
  }), g;
4543
4543
  function verb(n) {
4544
4544
  return function(v) {
4545
- return step([n, v]);
4545
+ return step2([n, v]);
4546
4546
  };
4547
4547
  }
4548
- function step(op) {
4549
- if (f) throw new TypeError("Generator is already executing.");
4548
+ function step2(op) {
4549
+ if (f2) throw new TypeError("Generator is already executing.");
4550
4550
  while (_) try {
4551
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
4551
+ if (f2 = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
4552
4552
  if (y = 0, t) op = [op[0] & 2, t.value];
4553
4553
  switch (op[0]) {
4554
4554
  case 0:
@@ -4595,7 +4595,7 @@ var require_generate = __commonJS({
4595
4595
  op = [6, e];
4596
4596
  y = 0;
4597
4597
  } finally {
4598
- f = t = 0;
4598
+ f2 = t = 0;
4599
4599
  }
4600
4600
  if (op[0] & 5) throw op[1];
4601
4601
  return { value: op[0] ? op[1] : void 0, done: true };
@@ -9451,87 +9451,72 @@ module.exports = __toCommonJS(plugin_exports);
9451
9451
  // backend/devices/registry/service.ts
9452
9452
  var import_common = require("@nestjs/common");
9453
9453
 
9454
- // backend/devices/registry/device.entity.ts
9454
+ // backend/devices/registry/storage.ts
9455
9455
  var import_plugins_backend_sdk = require("@attraccess/plugins-backend-sdk");
9456
- var ShellyDevice = class {
9457
- constructor() {
9458
- __publicField(this, "id");
9459
- __publicField(this, "name");
9460
- __publicField(this, "ipAddress");
9461
- __publicField(this, "generation");
9462
- __publicField(this, "model");
9463
- __publicField(this, "authState");
9464
- __publicField(this, "lastProbeAt");
9465
- __publicField(this, "lastProbeError");
9466
- __publicField(this, "createdAt");
9467
- __publicField(this, "updatedAt");
9468
- }
9469
- };
9470
- __decorateClass([
9471
- (0, import_plugins_backend_sdk.PrimaryGeneratedColumn)({ type: "integer" })
9472
- ], ShellyDevice.prototype, "id", 2);
9473
- __decorateClass([
9474
- (0, import_plugins_backend_sdk.Column)({ type: "varchar" })
9475
- ], ShellyDevice.prototype, "name", 2);
9476
- __decorateClass([
9477
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "ip_address", unique: true })
9478
- ], ShellyDevice.prototype, "ipAddress", 2);
9479
- __decorateClass([
9480
- (0, import_plugins_backend_sdk.Column)({ type: "integer", nullable: true })
9481
- ], ShellyDevice.prototype, "generation", 2);
9482
- __decorateClass([
9483
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", nullable: true })
9484
- ], ShellyDevice.prototype, "model", 2);
9485
- __decorateClass([
9486
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "auth_state", default: "unknown" })
9487
- ], ShellyDevice.prototype, "authState", 2);
9488
- __decorateClass([
9489
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "last_probe_at", nullable: true })
9490
- ], ShellyDevice.prototype, "lastProbeAt", 2);
9491
- __decorateClass([
9492
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "last_probe_error", nullable: true })
9493
- ], ShellyDevice.prototype, "lastProbeError", 2);
9494
- __decorateClass([
9495
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "created_at" })
9496
- ], ShellyDevice.prototype, "createdAt", 2);
9497
- __decorateClass([
9498
- (0, import_plugins_backend_sdk.Column)({ type: "varchar", name: "updated_at" })
9499
- ], ShellyDevice.prototype, "updatedAt", 2);
9500
- ShellyDevice = __decorateClass([
9501
- (0, import_plugins_backend_sdk.Entity)({ name: "plugin_shelly_devices" })
9502
- ], ShellyDevice);
9456
+ var devices = (0, import_plugins_backend_sdk.table)(
9457
+ {
9458
+ id: import_plugins_backend_sdk.f.id(),
9459
+ /** Operator-entered, without a length limit (like the device-reported model and the probe error). */
9460
+ name: import_plugins_backend_sdk.f.text(),
9461
+ ipAddress: import_plugins_backend_sdk.f.string({ column: "ip_address" }),
9462
+ /** Shelly generation: 1 for Gen1, 2/3/… for Gen2+. Null until first probe. */
9463
+ generation: import_plugins_backend_sdk.f.integer().optional(),
9464
+ /** Device model string (Gen2+ `model`, Gen1 `type`). Null until first probe. */
9465
+ model: import_plugins_backend_sdk.f.text().optional(),
9466
+ authState: import_plugins_backend_sdk.f.string({ column: "auth_state" }).withDefault("unknown"),
9467
+ /** ISO timestamp of the last probe attempt, or null if never attempted. */
9468
+ lastProbeAt: import_plugins_backend_sdk.f.string({ column: "last_probe_at" }).optional(),
9469
+ /** Error message from the last probe attempt, or null if the last probe succeeded. */
9470
+ lastProbeError: import_plugins_backend_sdk.f.text({ column: "last_probe_error" }).optional(),
9471
+ // Stored as ISO strings: the table predates the record port and keeps its original textual timestamp shape.
9472
+ createdAt: import_plugins_backend_sdk.f.string({ column: "created_at" }),
9473
+ updatedAt: import_plugins_backend_sdk.f.string({ column: "updated_at" })
9474
+ },
9475
+ {
9476
+ indexes: [{ name: "UQ_plugin_shelly_devices_ip_address", fields: ["ipAddress"], unique: true }],
9477
+ physicalName: "plugin_shelly_devices"
9478
+ }
9479
+ );
9480
+ var SHELLY_LEGACY_MIGRATIONS = ["CreateShellyDevices1749470400000"];
9481
+ var shellyStorage = (0, import_plugins_backend_sdk.definePluginStorage)({
9482
+ tables: { devices },
9483
+ migrations: [{ version: 1, name: "baseline", steps: [import_plugins_backend_sdk.step.createTable("devices", devices)] }],
9484
+ adoption: { legacyMigrations: SHELLY_LEGACY_MIGRATIONS }
9485
+ });
9503
9486
 
9504
9487
  // backend/devices/registry/service.ts
9505
9488
  var PLUGIN_CONTEXT = /* @__PURE__ */ Symbol.for("attraccess.plugin.context");
9506
9489
  var DeviceRegistryService = class {
9507
9490
  constructor(context) {
9508
9491
  __publicField(this, "context", context);
9509
- __publicField(this, "repository", null);
9510
- }
9511
- // Resolve the repository lazily, not in the constructor: plugin providers are
9512
- // instantiated during the host's DI pass, before the host DataSource ref is
9513
- // wired up (PluginContext.getRepository throws "Host DataSource is not
9514
- // available yet" until bootstrap completes). The first query happens on an
9515
- // HTTP request, well after bootstrap, so resolving on demand is safe; cache it
9516
- // once resolved.
9492
+ __publicField(this, "tables", null);
9493
+ }
9494
+ // Open the tables lazily, not in the constructor: plugin providers are instantiated during the host's DI pass,
9495
+ // before the storage is served. The first query happens on an HTTP request, well after bootstrap.
9517
9496
  get devices() {
9518
- if (this.repository === null) {
9519
- this.repository = this.context.getRepository(ShellyDevice);
9497
+ if (this.tables === null) {
9498
+ this.tables = this.context.storage.open(shellyStorage);
9520
9499
  }
9521
- return this.repository;
9500
+ return this.tables.devices;
9522
9501
  }
9523
- list() {
9524
- return this.devices.find({ order: { createdAt: "ASC", id: "ASC" } });
9502
+ async list() {
9503
+ const rows = await this.devices.find({
9504
+ orderBy: [
9505
+ { field: "createdAt", direction: "asc" },
9506
+ { field: "id", direction: "asc" }
9507
+ ]
9508
+ });
9509
+ return rows;
9525
9510
  }
9526
- findById(id) {
9527
- return this.devices.findOne({ where: { id } });
9511
+ async findById(id) {
9512
+ return await this.devices.findOne({ where: { id } });
9528
9513
  }
9529
- findByIp(ipAddress) {
9530
- return this.devices.findOne({ where: { ipAddress } });
9514
+ async findByIp(ipAddress) {
9515
+ return await this.devices.findOne({ where: { ipAddress } });
9531
9516
  }
9532
- create(input) {
9517
+ async create(input) {
9533
9518
  const now = (/* @__PURE__ */ new Date()).toISOString();
9534
- const device = this.devices.create({
9519
+ const row = await this.devices.insert({
9535
9520
  name: input.name,
9536
9521
  ipAddress: input.ipAddress,
9537
9522
  generation: input.generation,
@@ -9542,26 +9527,26 @@ var DeviceRegistryService = class {
9542
9527
  createdAt: now,
9543
9528
  updatedAt: now
9544
9529
  });
9545
- return this.devices.save(device);
9530
+ return row;
9546
9531
  }
9547
9532
  async updateProbe(id, patch) {
9548
- await this.devices.update(id, {
9549
- generation: patch.generation,
9550
- model: patch.model,
9551
- authState: patch.authState,
9552
- lastProbeAt: patch.lastProbeAt,
9553
- lastProbeError: patch.lastProbeError,
9554
- updatedAt: (/* @__PURE__ */ new Date()).toISOString()
9555
- });
9533
+ await this.devices.update(
9534
+ { id },
9535
+ {
9536
+ generation: patch.generation,
9537
+ model: patch.model,
9538
+ authState: patch.authState,
9539
+ lastProbeAt: patch.lastProbeAt,
9540
+ lastProbeError: patch.lastProbeError,
9541
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
9542
+ }
9543
+ );
9556
9544
  }
9557
9545
  async updateAuthState(id, authState) {
9558
- await this.devices.update(id, {
9559
- authState,
9560
- updatedAt: (/* @__PURE__ */ new Date()).toISOString()
9561
- });
9546
+ await this.devices.update({ id }, { authState, updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
9562
9547
  }
9563
9548
  async delete(id) {
9564
- await this.devices.delete(id);
9549
+ await this.devices.delete({ id });
9565
9550
  }
9566
9551
  };
9567
9552
  DeviceRegistryService = __decorateClass([
@@ -9793,7 +9778,7 @@ var ShellyProbeService = class {
9793
9778
  json = await res.json();
9794
9779
  } catch (err) {
9795
9780
  const reason = err instanceof Error ? err.message : String(err);
9796
- throw new Error(`probe of ${url} failed: ${reason}`);
9781
+ throw new Error(`probe of ${url} failed: ${reason}`, { cause: err });
9797
9782
  }
9798
9783
  return this.interpret(json);
9799
9784
  }
@@ -9858,11 +9843,11 @@ var DiscoveryService = class {
9858
9843
  return null;
9859
9844
  }
9860
9845
  })).filter((hit) => hit !== null);
9861
- const devices = [];
9846
+ const devices2 = [];
9862
9847
  for (const hit of hits) {
9863
- devices.push(await this.persist(hit.ipAddress, hit.source, hit.result));
9848
+ devices2.push(await this.persist(hit.ipAddress, hit.source, hit.result));
9864
9849
  }
9865
- return { subnets, probed: candidates.length, devices };
9850
+ return { subnets, probed: candidates.length, devices: devices2 };
9866
9851
  }
9867
9852
  async persist(ipAddress, source, result) {
9868
9853
  const probeFields = {
@@ -10277,9 +10262,9 @@ var ShellyController = class {
10277
10262
  });
10278
10263
  }
10279
10264
  async firmwareOverview() {
10280
- const devices = await this.registry.list();
10265
+ const devices2 = await this.registry.list();
10281
10266
  return Promise.all(
10282
- devices.map(async (device) => {
10267
+ devices2.map(async (device) => {
10283
10268
  if (device.generation === null) {
10284
10269
  return { deviceId: device.id, status: null, error: "device generation is unknown; probe it first" };
10285
10270
  }
@@ -10309,15 +10294,22 @@ var ShellyController = class {
10309
10294
  }
10310
10295
  const name = (body?.name ?? "").trim() || ipAddress;
10311
10296
  const probed = await this.tryProbe(ipAddress);
10312
- return this.registry.create({
10313
- name,
10314
- ipAddress,
10315
- generation: probed.result?.generation ?? null,
10316
- model: probed.result?.model ?? null,
10317
- authState: probed.result?.authState ?? "unknown",
10318
- lastProbeAt: probed.at,
10319
- lastProbeError: probed.error
10320
- });
10297
+ try {
10298
+ return await this.registry.create({
10299
+ name,
10300
+ ipAddress,
10301
+ generation: probed.result?.generation ?? null,
10302
+ model: probed.result?.model ?? null,
10303
+ authState: probed.result?.authState ?? "unknown",
10304
+ lastProbeAt: probed.at,
10305
+ lastProbeError: probed.error
10306
+ });
10307
+ } catch (error) {
10308
+ if (error instanceof import_plugins_backend_sdk3.PluginStorageUniqueViolation) {
10309
+ throw new import_common8.ConflictException(`a device with IP ${ipAddress} already exists`);
10310
+ }
10311
+ throw error;
10312
+ }
10321
10313
  }
10322
10314
  list() {
10323
10315
  return this.registry.list();
@@ -10429,9 +10421,8 @@ var PLUGIN_CONTEXT2 = /* @__PURE__ */ Symbol.for("attraccess.plugin.context");
10429
10421
  var ShellyPluginModule = class {
10430
10422
  };
10431
10423
  var plugin = {
10432
- // Host registers these into the shared DataSource at load time (gated on
10433
- // DATABASE_ACCESS) so the registry service can use a real repository.
10434
- entities: [ShellyDevice],
10424
+ // The host creates/adopts the declared tables at load time (gated on PLUGIN_STORAGE).
10425
+ storage: shellyStorage,
10435
10426
  register(context) {
10436
10427
  return {
10437
10428
  module: ShellyPluginModule,