@riddix/hamh 2.1.0-alpha.390 → 2.1.0-alpha.392

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.
@@ -150760,6 +150760,7 @@ var HomeAssistantRegistry = class extends Service {
150760
150760
  this.options = options;
150761
150761
  }
150762
150762
  autoRefresh;
150763
+ lastRegistryFingerprint = "";
150763
150764
  _devices = {};
150764
150765
  get devices() {
150765
150766
  return this._devices;
@@ -150790,8 +150791,10 @@ var HomeAssistantRegistry = class extends Service {
150790
150791
  this.disableAutoRefresh();
150791
150792
  this.autoRefresh = setInterval(async () => {
150792
150793
  try {
150793
- await this.reload();
150794
- onRefresh();
150794
+ const changed = await this.reload();
150795
+ if (changed) {
150796
+ onRefresh();
150797
+ }
150795
150798
  } catch (e) {
150796
150799
  logger141.warn("Failed to refresh registry, will retry next interval:", e);
150797
150800
  }
@@ -150804,7 +150807,7 @@ var HomeAssistantRegistry = class extends Service {
150804
150807
  this.autoRefresh = void 0;
150805
150808
  }
150806
150809
  async reload() {
150807
- await withRetry(() => this.fetchRegistries(), {
150810
+ return await withRetry(() => this.fetchRegistries(), {
150808
150811
  maxAttempts: 5,
150809
150812
  baseDelayMs: 2e3,
150810
150813
  maxDelayMs: 15e3,
@@ -150819,43 +150822,64 @@ var HomeAssistantRegistry = class extends Service {
150819
150822
  async fetchRegistries() {
150820
150823
  const connection = this.client.connection;
150821
150824
  const entityRegistry = await getRegistry(connection);
150825
+ const statesList = await getStates(connection);
150826
+ const deviceRegistry = await getDeviceRegistry(connection);
150827
+ let labels = [];
150828
+ try {
150829
+ labels = await getLabelRegistry(connection);
150830
+ } catch {
150831
+ }
150832
+ let areas = [];
150833
+ try {
150834
+ areas = await getAreaRegistry(connection);
150835
+ } catch {
150836
+ }
150837
+ const hash2 = createHash("md5");
150838
+ for (const e of entityRegistry) {
150839
+ hash2.update(
150840
+ `${e.entity_id}\0${e.device_id ?? ""}\0${e.disabled_by ?? ""}\0${e.hidden_by ?? ""}
150841
+ `
150842
+ );
150843
+ }
150844
+ for (const s of statesList) hash2.update(`${s.entity_id}
150845
+ `);
150846
+ for (const d of deviceRegistry) hash2.update(`${d.id}
150847
+ `);
150848
+ for (const l of labels) hash2.update(`${l.label_id}
150849
+ `);
150850
+ for (const a of areas) hash2.update(`${a.area_id}\0${a.name}
150851
+ `);
150852
+ const fingerprint = hash2.digest("hex");
150853
+ this._states = keyBy(statesList, "entity_id");
150854
+ if (fingerprint === this.lastRegistryFingerprint) {
150855
+ logger141.debug("Registry unchanged, skipping full refresh");
150856
+ return false;
150857
+ }
150858
+ this.lastRegistryFingerprint = fingerprint;
150822
150859
  entityRegistry.forEach((e) => {
150823
150860
  e.device_id = e.device_id ?? mockDeviceId(e.entity_id);
150824
150861
  });
150825
150862
  const entities = keyBy(entityRegistry, "entity_id");
150826
- const states = keyBy(
150827
- await getStates(connection),
150828
- "entity_id"
150829
- );
150830
- const entityIds = uniq(keys(entities).concat(keys(states)));
150863
+ const entityIds = uniq(keys(entities).concat(keys(this._states)));
150831
150864
  const allEntities = keyBy(
150832
150865
  entityIds.map((id) => entities[id] ?? { entity_id: id, device_id: id }),
150833
150866
  "entity_id"
150834
150867
  );
150835
- const deviceIds = values(allEntities).map(
150868
+ const deviceIdsList = values(allEntities).map(
150836
150869
  (e) => e.device_id ?? e.entity_id
150837
150870
  );
150838
- const realDevices = keyBy(await getDeviceRegistry(connection), "id");
150839
- const missingDeviceIds = uniq(deviceIds.filter((d) => !realDevices[d]));
150871
+ const realDevices = keyBy(deviceRegistry, "id");
150872
+ const missingDeviceIds = uniq(deviceIdsList.filter((d) => !realDevices[d]));
150840
150873
  const missingDevices = fromPairs(missingDeviceIds.map((d) => [d, { id: d }]));
150841
150874
  this._devices = { ...missingDevices, ...realDevices };
150842
150875
  this._entities = allEntities;
150843
- this._states = states;
150844
150876
  logger141.debug(
150845
- `Loaded HA registry: ${keys(allEntities).length} entities, ${keys(realDevices).length} devices, ${keys(states).length} states`
150877
+ `Loaded HA registry: ${keys(allEntities).length} entities, ${keys(realDevices).length} devices, ${keys(this._states).length} states`
150846
150878
  );
150847
150879
  logMemoryUsage(logger141, "after HA registry load");
150848
- try {
150849
- this._labels = await getLabelRegistry(connection);
150850
- } catch {
150851
- this._labels = [];
150852
- }
150853
- try {
150854
- const areaRegistry = await getAreaRegistry(connection);
150855
- this._areas = new Map(areaRegistry.map((a) => [a.area_id, a.name]));
150856
- } catch {
150857
- this._areas = /* @__PURE__ */ new Map();
150858
- }
150880
+ this._labels = labels;
150881
+ this._areas = new Map(areas.map((a) => [a.area_id, a.name]));
150882
+ return true;
150859
150883
  }
150860
150884
  };
150861
150885
  function mockDeviceId(entityId) {
@@ -176293,7 +176317,7 @@ function startOptionsBuilder(yargs2) {
176293
176317
  }).option("home-assistant-refresh-interval", {
176294
176318
  type: "number",
176295
176319
  description: "The refresh rate (in seconds) to detect new devices & entities or their configurations",
176296
- default: 30
176320
+ default: 60
176297
176321
  }).option("http-auth-username", {
176298
176322
  type: "string",
176299
176323
  description: "Username for HTTP basic authentication (optional)"