myio-js-library 0.1.340 → 0.1.342

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/dist/index.cjs CHANGED
@@ -971,7 +971,7 @@ module.exports = __toCommonJS(index_exports);
971
971
  // package.json
972
972
  var package_default = {
973
973
  name: "myio-js-library",
974
- version: "0.1.340",
974
+ version: "0.1.342",
975
975
  description: "A clean, standalone JS SDK for MYIO projects",
976
976
  license: "MIT",
977
977
  repository: "github:gh-myio/myio-js-library",
@@ -2999,6 +2999,132 @@ function buildEntityObject(config, item, context) {
2999
2999
  powerRanges: item.powerRanges || null
3000
3000
  };
3001
3001
  }
3002
+ function createDefaultActionHandlers(config, entityObject, item) {
3003
+ const MyIOLibrary2 = typeof window !== "undefined" && window.MyIOLibrary;
3004
+ const MyIOUtils = typeof window !== "undefined" && window.MyIOUtils;
3005
+ const MyIOAuth = MyIOUtils?.MyIOAuth;
3006
+ const CLIENT_ID = MyIOUtils?.CLIENT_ID;
3007
+ const CLIENT_SECRET = MyIOUtils?.CLIENT_SECRET;
3008
+ const getDataApiHost = MyIOUtils?.getDataApiHost || (() => "https://api.data.apps.myio-bas.com");
3009
+ return {
3010
+ handleActionDashboard: async () => {
3011
+ LogHelper2.log(`[${config.widgetName}] Opening dashboard for:`, entityObject.entityId);
3012
+ try {
3013
+ if (typeof MyIOLibrary2?.openDashboardPopupEnergy !== "function") {
3014
+ window.alert("Dashboard component n\xE3o dispon\xEDvel");
3015
+ return;
3016
+ }
3017
+ const startDate = window.STATE?.period?.startDateISO || MyIOUtils?.currentPeriod?.startDateISO;
3018
+ const endDate = window.STATE?.period?.endDateISO || MyIOUtils?.currentPeriod?.endDateISO;
3019
+ if (!startDate || !endDate) {
3020
+ window.alert("Per\xEDodo de datas n\xE3o definido.");
3021
+ return;
3022
+ }
3023
+ if (!MyIOAuth || typeof MyIOAuth.getToken !== "function") {
3024
+ LogHelper2.error(`[${config.widgetName}] MyIOAuth not available`);
3025
+ window.alert("Autentica\xE7\xE3o n\xE3o dispon\xEDvel. Recarregue a p\xE1gina.");
3026
+ return;
3027
+ }
3028
+ const tokenIngestionDashBoard = await MyIOAuth.getToken();
3029
+ const myTbTokenDashBoard = localStorage.getItem("jwt_token");
3030
+ MyIOLibrary2.openDashboardPopupEnergy({
3031
+ deviceId: entityObject.entityId,
3032
+ readingType: config.domain,
3033
+ // 'water' or 'energy'
3034
+ startDate,
3035
+ endDate,
3036
+ tbJwtToken: myTbTokenDashBoard,
3037
+ ingestionToken: tokenIngestionDashBoard,
3038
+ clientId: CLIENT_ID,
3039
+ clientSecret: CLIENT_SECRET,
3040
+ onClose: () => {
3041
+ const o = document.querySelector(".myio-modal-overlay");
3042
+ if (o) o.remove();
3043
+ }
3044
+ });
3045
+ } catch (err) {
3046
+ LogHelper2.error(`[${config.widgetName}] Error opening dashboard:`, err);
3047
+ window.alert("Erro ao abrir dashboard");
3048
+ }
3049
+ },
3050
+ handleActionReport: async () => {
3051
+ LogHelper2.log(`[${config.widgetName}] Opening report for:`, item.ingestionId);
3052
+ try {
3053
+ if (!MyIOAuth || typeof MyIOAuth.getToken !== "function") {
3054
+ LogHelper2.error(`[${config.widgetName}] MyIOAuth not available for report`);
3055
+ window.alert("Autentica\xE7\xE3o n\xE3o dispon\xEDvel. Recarregue a p\xE1gina.");
3056
+ return;
3057
+ }
3058
+ const ingestionToken2 = await MyIOAuth.getToken();
3059
+ await MyIOLibrary2?.openDashboardPopupReport?.({
3060
+ ingestionId: item.ingestionId,
3061
+ identifier: item.identifier,
3062
+ label: item.label,
3063
+ domain: config.domain,
3064
+ api: {
3065
+ dataApiBaseUrl: getDataApiHost(),
3066
+ clientId: CLIENT_ID,
3067
+ clientSecret: CLIENT_SECRET,
3068
+ ingestionToken: ingestionToken2
3069
+ }
3070
+ });
3071
+ } catch (err) {
3072
+ LogHelper2.error(`[${config.widgetName}] Error opening report:`, err);
3073
+ window.alert("Erro ao abrir relat\xF3rio");
3074
+ }
3075
+ },
3076
+ handleActionSettings: async () => {
3077
+ LogHelper2.log(`[${config.widgetName}] Opening settings for:`, entityObject.entityId);
3078
+ const jwt = localStorage.getItem("jwt_token");
3079
+ if (!jwt) {
3080
+ LogHelper2.error(`[${config.widgetName}] JWT token not found`);
3081
+ window.alert("Token de autentica\xE7\xE3o n\xE3o encontrado");
3082
+ return;
3083
+ }
3084
+ const tbId = entityObject.entityId;
3085
+ if (!tbId || tbId === item.ingestionId) {
3086
+ window.alert("ID inv\xE1lido");
3087
+ return;
3088
+ }
3089
+ try {
3090
+ await MyIOLibrary2?.openDashboardPopupSettings?.({
3091
+ deviceId: tbId,
3092
+ label: item.label,
3093
+ jwtToken: jwt,
3094
+ domain: config.domain,
3095
+ deviceType: entityObject.deviceType,
3096
+ deviceProfile: entityObject.deviceProfile,
3097
+ customerName: entityObject.customerName,
3098
+ connectionData: {
3099
+ centralName: entityObject.centralName,
3100
+ connectionStatusTime: entityObject.connectionStatusTime,
3101
+ timeVal: entityObject.timeVal || (/* @__PURE__ */ new Date("1970-01-01")).getTime(),
3102
+ deviceStatus: entityObject.deviceStatus !== "power_off" && entityObject.deviceStatus !== "not_installed" ? "power_on" : "power_off",
3103
+ lastDisconnectTime: entityObject.lastDisconnectTime || 0
3104
+ },
3105
+ ui: { title: "Configura\xE7\xF5es", width: 900 },
3106
+ mapInstantaneousPower: entityObject.mapInstantaneousPower,
3107
+ onSaved: (payload) => {
3108
+ LogHelper2.log(`[${config.widgetName}] Settings saved:`, payload);
3109
+ if (typeof window.showGlobalSuccessModal === "function") {
3110
+ window.showGlobalSuccessModal(6);
3111
+ }
3112
+ },
3113
+ onClose: () => {
3114
+ const settingsOverlay = document.querySelector(".myio-settings-modal-overlay");
3115
+ if (settingsOverlay) settingsOverlay.remove();
3116
+ const overlay = document.querySelector(".myio-modal-overlay");
3117
+ if (overlay) overlay.remove();
3118
+ LogHelper2.log(`[${config.widgetName}] Settings modal closed`);
3119
+ }
3120
+ });
3121
+ } catch (e) {
3122
+ LogHelper2.error(`[${config.widgetName}] Error opening settings:`, e);
3123
+ window.alert("Erro ao abrir configura\xE7\xF5es");
3124
+ }
3125
+ }
3126
+ };
3127
+ }
3002
3128
  async function renderList(config, STATE, visible, context) {
3003
3129
  const listElement = document.getElementById(config.listElementId);
3004
3130
  if (!listElement) {
@@ -3011,6 +3137,7 @@ async function renderList(config, STATE, visible, context) {
3011
3137
  const container = document.createElement("div");
3012
3138
  listElement.appendChild(container);
3013
3139
  const entityObject = buildEntityObject(config, item, context);
3140
+ const defaultHandlers = createDefaultActionHandlers(config, entityObject, item);
3014
3141
  if (MyIOLibrary2?.renderCardComponentHeadOffice) {
3015
3142
  MyIOLibrary2.renderCardComponentHeadOffice(container, {
3016
3143
  entityObject,
@@ -3020,9 +3147,10 @@ async function renderList(config, STATE, visible, context) {
3020
3147
  handleClickCard: (ev, entity) => {
3021
3148
  LogHelper2.log(`[${config.widgetName}] Card clicked: ${entity.name}`);
3022
3149
  },
3023
- handleActionDashboard: () => context?.handleActionDashboard?.(entityObject, item),
3024
- handleActionReport: () => context?.handleActionReport?.(entityObject, item),
3025
- handleActionSettings: () => context?.handleActionSettings?.(entityObject, item),
3150
+ // RFC-0144: Use context handlers if provided, otherwise use defaults
3151
+ handleActionDashboard: context?.handleActionDashboard ? () => context.handleActionDashboard(entityObject, item) : defaultHandlers.handleActionDashboard,
3152
+ handleActionReport: context?.handleActionReport ? () => context.handleActionReport(entityObject, item) : defaultHandlers.handleActionReport,
3153
+ handleActionSettings: context?.handleActionSettings ? () => context.handleActionSettings(entityObject, item) : defaultHandlers.handleActionSettings,
3026
3154
  handleSelect: (checked, entity) => {
3027
3155
  if (!STATE.selectedIds) STATE.selectedIds = /* @__PURE__ */ new Set();
3028
3156
  const MyIOSelectionStore2 = typeof window !== "undefined" && window.MyIOLibrary?.MyIOSelectionStore || typeof window !== "undefined" && window.MyIOSelectionStore;
@@ -3078,11 +3206,11 @@ function updateStats(config, items, context) {
3078
3206
  }
3079
3207
  });
3080
3208
  const connectivityPercentage = total > 0 ? (onlineCount / total * 100).toFixed(1) : "0.0";
3081
- if (context?.headerController?.update) {
3082
- context.headerController.update({
3083
- connectivity: `${onlineCount}/${total} (${connectivityPercentage}%)`,
3209
+ if (context?.headerController?.updateStats) {
3210
+ context.headerController.updateStats({
3211
+ online: onlineCount,
3084
3212
  total,
3085
- consumption: config.formatValue(totalConsumption),
3213
+ consumption: totalConsumption,
3086
3214
  zeroCount: zeroConsumptionCount
3087
3215
  });
3088
3216
  }
@@ -3251,7 +3379,9 @@ function createWidgetController(config) {
3251
3379
  updateStats(config, visible, context);
3252
3380
  await renderList(config, STATE, visible, context);
3253
3381
  }
3382
+ let busyModalRef = null;
3254
3383
  function registerEventHandlers(busyModal) {
3384
+ busyModalRef = busyModal;
3255
3385
  const dataReadyHandler = () => {
3256
3386
  const items = getCachedData(config);
3257
3387
  if (items && items.length > 0) {
@@ -3274,7 +3404,7 @@ function createWidgetController(config) {
3274
3404
  };
3275
3405
  const summaryReadyHandler = () => {
3276
3406
  const items = getCachedData(config);
3277
- if (items && items.length > 0 && STATE.itemsBase.length === 0) {
3407
+ if (items && items.length > 0) {
3278
3408
  LogHelper2.log(`[${config.widgetName}] Summary ready, loading from cache: ${items.length} items`);
3279
3409
  STATE.itemsBase = items;
3280
3410
  STATE.itemsEnriched = items.map((item) => ({
@@ -3290,7 +3420,7 @@ function createWidgetController(config) {
3290
3420
  const { domain, items } = ev.detail || {};
3291
3421
  if (domain !== config.domain) return;
3292
3422
  const filteredItems = config.deviceFilter ? items.filter(config.deviceFilter) : items;
3293
- if (filteredItems && filteredItems.length > 0 && STATE.itemsBase.length === 0) {
3423
+ if (filteredItems && filteredItems.length > 0) {
3294
3424
  LogHelper2.log(`[${config.widgetName}] Provide-data event: ${filteredItems.length} items (from ${items?.length || 0})`);
3295
3425
  STATE.itemsBase = filteredItems;
3296
3426
  STATE.itemsEnriched = filteredItems.map((item) => ({
@@ -3313,7 +3443,7 @@ function createWidgetController(config) {
3313
3443
  } else if (config.context === "entrada") {
3314
3444
  items = waterClassified.entrada?.items;
3315
3445
  }
3316
- if (items && items.length > 0 && STATE.itemsBase.length === 0) {
3446
+ if (items && items.length > 0) {
3317
3447
  LogHelper2.log(`[${config.widgetName}] water-tb-data-ready event: ${items.length} items for context ${config.context}`);
3318
3448
  STATE.itemsBase = items;
3319
3449
  STATE.itemsEnriched = items.map((item) => ({
@@ -3403,6 +3533,18 @@ function createWidgetController(config) {
3403
3533
  }
3404
3534
  },
3405
3535
  onDataUpdated: function() {
3536
+ const items = getCachedData(config);
3537
+ if (items && items.length > 0) {
3538
+ LogHelper2.log(`[${config.widgetName}] onDataUpdated: refreshing from cache with ${items.length} items`);
3539
+ STATE.itemsBase = items;
3540
+ STATE.itemsEnriched = items.map((item) => ({
3541
+ ...item,
3542
+ value: Number(item.value || item.consumption || item.pulses || 0)
3543
+ }));
3544
+ STATE.dataFromMain = true;
3545
+ if (busyModalRef) busyModalRef.hideBusy();
3546
+ reflow();
3547
+ }
3406
3548
  },
3407
3549
  onDestroy: function() {
3408
3550
  LogHelper2.log(`[${config.widgetName}] RFC-0143 Factory Controller - onDestroy`);
package/dist/index.js CHANGED
@@ -546,7 +546,7 @@ var init_template_card = __esm({
546
546
  // package.json
547
547
  var package_default = {
548
548
  name: "myio-js-library",
549
- version: "0.1.340",
549
+ version: "0.1.342",
550
550
  description: "A clean, standalone JS SDK for MYIO projects",
551
551
  license: "MIT",
552
552
  repository: "github:gh-myio/myio-js-library",
@@ -2574,6 +2574,132 @@ function buildEntityObject(config, item, context) {
2574
2574
  powerRanges: item.powerRanges || null
2575
2575
  };
2576
2576
  }
2577
+ function createDefaultActionHandlers(config, entityObject, item) {
2578
+ const MyIOLibrary2 = typeof window !== "undefined" && window.MyIOLibrary;
2579
+ const MyIOUtils = typeof window !== "undefined" && window.MyIOUtils;
2580
+ const MyIOAuth = MyIOUtils?.MyIOAuth;
2581
+ const CLIENT_ID = MyIOUtils?.CLIENT_ID;
2582
+ const CLIENT_SECRET = MyIOUtils?.CLIENT_SECRET;
2583
+ const getDataApiHost = MyIOUtils?.getDataApiHost || (() => "https://api.data.apps.myio-bas.com");
2584
+ return {
2585
+ handleActionDashboard: async () => {
2586
+ LogHelper2.log(`[${config.widgetName}] Opening dashboard for:`, entityObject.entityId);
2587
+ try {
2588
+ if (typeof MyIOLibrary2?.openDashboardPopupEnergy !== "function") {
2589
+ window.alert("Dashboard component n\xE3o dispon\xEDvel");
2590
+ return;
2591
+ }
2592
+ const startDate = window.STATE?.period?.startDateISO || MyIOUtils?.currentPeriod?.startDateISO;
2593
+ const endDate = window.STATE?.period?.endDateISO || MyIOUtils?.currentPeriod?.endDateISO;
2594
+ if (!startDate || !endDate) {
2595
+ window.alert("Per\xEDodo de datas n\xE3o definido.");
2596
+ return;
2597
+ }
2598
+ if (!MyIOAuth || typeof MyIOAuth.getToken !== "function") {
2599
+ LogHelper2.error(`[${config.widgetName}] MyIOAuth not available`);
2600
+ window.alert("Autentica\xE7\xE3o n\xE3o dispon\xEDvel. Recarregue a p\xE1gina.");
2601
+ return;
2602
+ }
2603
+ const tokenIngestionDashBoard = await MyIOAuth.getToken();
2604
+ const myTbTokenDashBoard = localStorage.getItem("jwt_token");
2605
+ MyIOLibrary2.openDashboardPopupEnergy({
2606
+ deviceId: entityObject.entityId,
2607
+ readingType: config.domain,
2608
+ // 'water' or 'energy'
2609
+ startDate,
2610
+ endDate,
2611
+ tbJwtToken: myTbTokenDashBoard,
2612
+ ingestionToken: tokenIngestionDashBoard,
2613
+ clientId: CLIENT_ID,
2614
+ clientSecret: CLIENT_SECRET,
2615
+ onClose: () => {
2616
+ const o = document.querySelector(".myio-modal-overlay");
2617
+ if (o) o.remove();
2618
+ }
2619
+ });
2620
+ } catch (err) {
2621
+ LogHelper2.error(`[${config.widgetName}] Error opening dashboard:`, err);
2622
+ window.alert("Erro ao abrir dashboard");
2623
+ }
2624
+ },
2625
+ handleActionReport: async () => {
2626
+ LogHelper2.log(`[${config.widgetName}] Opening report for:`, item.ingestionId);
2627
+ try {
2628
+ if (!MyIOAuth || typeof MyIOAuth.getToken !== "function") {
2629
+ LogHelper2.error(`[${config.widgetName}] MyIOAuth not available for report`);
2630
+ window.alert("Autentica\xE7\xE3o n\xE3o dispon\xEDvel. Recarregue a p\xE1gina.");
2631
+ return;
2632
+ }
2633
+ const ingestionToken2 = await MyIOAuth.getToken();
2634
+ await MyIOLibrary2?.openDashboardPopupReport?.({
2635
+ ingestionId: item.ingestionId,
2636
+ identifier: item.identifier,
2637
+ label: item.label,
2638
+ domain: config.domain,
2639
+ api: {
2640
+ dataApiBaseUrl: getDataApiHost(),
2641
+ clientId: CLIENT_ID,
2642
+ clientSecret: CLIENT_SECRET,
2643
+ ingestionToken: ingestionToken2
2644
+ }
2645
+ });
2646
+ } catch (err) {
2647
+ LogHelper2.error(`[${config.widgetName}] Error opening report:`, err);
2648
+ window.alert("Erro ao abrir relat\xF3rio");
2649
+ }
2650
+ },
2651
+ handleActionSettings: async () => {
2652
+ LogHelper2.log(`[${config.widgetName}] Opening settings for:`, entityObject.entityId);
2653
+ const jwt = localStorage.getItem("jwt_token");
2654
+ if (!jwt) {
2655
+ LogHelper2.error(`[${config.widgetName}] JWT token not found`);
2656
+ window.alert("Token de autentica\xE7\xE3o n\xE3o encontrado");
2657
+ return;
2658
+ }
2659
+ const tbId = entityObject.entityId;
2660
+ if (!tbId || tbId === item.ingestionId) {
2661
+ window.alert("ID inv\xE1lido");
2662
+ return;
2663
+ }
2664
+ try {
2665
+ await MyIOLibrary2?.openDashboardPopupSettings?.({
2666
+ deviceId: tbId,
2667
+ label: item.label,
2668
+ jwtToken: jwt,
2669
+ domain: config.domain,
2670
+ deviceType: entityObject.deviceType,
2671
+ deviceProfile: entityObject.deviceProfile,
2672
+ customerName: entityObject.customerName,
2673
+ connectionData: {
2674
+ centralName: entityObject.centralName,
2675
+ connectionStatusTime: entityObject.connectionStatusTime,
2676
+ timeVal: entityObject.timeVal || (/* @__PURE__ */ new Date("1970-01-01")).getTime(),
2677
+ deviceStatus: entityObject.deviceStatus !== "power_off" && entityObject.deviceStatus !== "not_installed" ? "power_on" : "power_off",
2678
+ lastDisconnectTime: entityObject.lastDisconnectTime || 0
2679
+ },
2680
+ ui: { title: "Configura\xE7\xF5es", width: 900 },
2681
+ mapInstantaneousPower: entityObject.mapInstantaneousPower,
2682
+ onSaved: (payload) => {
2683
+ LogHelper2.log(`[${config.widgetName}] Settings saved:`, payload);
2684
+ if (typeof window.showGlobalSuccessModal === "function") {
2685
+ window.showGlobalSuccessModal(6);
2686
+ }
2687
+ },
2688
+ onClose: () => {
2689
+ const settingsOverlay = document.querySelector(".myio-settings-modal-overlay");
2690
+ if (settingsOverlay) settingsOverlay.remove();
2691
+ const overlay = document.querySelector(".myio-modal-overlay");
2692
+ if (overlay) overlay.remove();
2693
+ LogHelper2.log(`[${config.widgetName}] Settings modal closed`);
2694
+ }
2695
+ });
2696
+ } catch (e) {
2697
+ LogHelper2.error(`[${config.widgetName}] Error opening settings:`, e);
2698
+ window.alert("Erro ao abrir configura\xE7\xF5es");
2699
+ }
2700
+ }
2701
+ };
2702
+ }
2577
2703
  async function renderList(config, STATE, visible, context) {
2578
2704
  const listElement = document.getElementById(config.listElementId);
2579
2705
  if (!listElement) {
@@ -2586,6 +2712,7 @@ async function renderList(config, STATE, visible, context) {
2586
2712
  const container = document.createElement("div");
2587
2713
  listElement.appendChild(container);
2588
2714
  const entityObject = buildEntityObject(config, item, context);
2715
+ const defaultHandlers = createDefaultActionHandlers(config, entityObject, item);
2589
2716
  if (MyIOLibrary2?.renderCardComponentHeadOffice) {
2590
2717
  MyIOLibrary2.renderCardComponentHeadOffice(container, {
2591
2718
  entityObject,
@@ -2595,9 +2722,10 @@ async function renderList(config, STATE, visible, context) {
2595
2722
  handleClickCard: (ev, entity) => {
2596
2723
  LogHelper2.log(`[${config.widgetName}] Card clicked: ${entity.name}`);
2597
2724
  },
2598
- handleActionDashboard: () => context?.handleActionDashboard?.(entityObject, item),
2599
- handleActionReport: () => context?.handleActionReport?.(entityObject, item),
2600
- handleActionSettings: () => context?.handleActionSettings?.(entityObject, item),
2725
+ // RFC-0144: Use context handlers if provided, otherwise use defaults
2726
+ handleActionDashboard: context?.handleActionDashboard ? () => context.handleActionDashboard(entityObject, item) : defaultHandlers.handleActionDashboard,
2727
+ handleActionReport: context?.handleActionReport ? () => context.handleActionReport(entityObject, item) : defaultHandlers.handleActionReport,
2728
+ handleActionSettings: context?.handleActionSettings ? () => context.handleActionSettings(entityObject, item) : defaultHandlers.handleActionSettings,
2601
2729
  handleSelect: (checked, entity) => {
2602
2730
  if (!STATE.selectedIds) STATE.selectedIds = /* @__PURE__ */ new Set();
2603
2731
  const MyIOSelectionStore2 = typeof window !== "undefined" && window.MyIOLibrary?.MyIOSelectionStore || typeof window !== "undefined" && window.MyIOSelectionStore;
@@ -2653,11 +2781,11 @@ function updateStats(config, items, context) {
2653
2781
  }
2654
2782
  });
2655
2783
  const connectivityPercentage = total > 0 ? (onlineCount / total * 100).toFixed(1) : "0.0";
2656
- if (context?.headerController?.update) {
2657
- context.headerController.update({
2658
- connectivity: `${onlineCount}/${total} (${connectivityPercentage}%)`,
2784
+ if (context?.headerController?.updateStats) {
2785
+ context.headerController.updateStats({
2786
+ online: onlineCount,
2659
2787
  total,
2660
- consumption: config.formatValue(totalConsumption),
2788
+ consumption: totalConsumption,
2661
2789
  zeroCount: zeroConsumptionCount
2662
2790
  });
2663
2791
  }
@@ -2826,7 +2954,9 @@ function createWidgetController(config) {
2826
2954
  updateStats(config, visible, context);
2827
2955
  await renderList(config, STATE, visible, context);
2828
2956
  }
2957
+ let busyModalRef = null;
2829
2958
  function registerEventHandlers(busyModal) {
2959
+ busyModalRef = busyModal;
2830
2960
  const dataReadyHandler = () => {
2831
2961
  const items = getCachedData(config);
2832
2962
  if (items && items.length > 0) {
@@ -2849,7 +2979,7 @@ function createWidgetController(config) {
2849
2979
  };
2850
2980
  const summaryReadyHandler = () => {
2851
2981
  const items = getCachedData(config);
2852
- if (items && items.length > 0 && STATE.itemsBase.length === 0) {
2982
+ if (items && items.length > 0) {
2853
2983
  LogHelper2.log(`[${config.widgetName}] Summary ready, loading from cache: ${items.length} items`);
2854
2984
  STATE.itemsBase = items;
2855
2985
  STATE.itemsEnriched = items.map((item) => ({
@@ -2865,7 +2995,7 @@ function createWidgetController(config) {
2865
2995
  const { domain, items } = ev.detail || {};
2866
2996
  if (domain !== config.domain) return;
2867
2997
  const filteredItems = config.deviceFilter ? items.filter(config.deviceFilter) : items;
2868
- if (filteredItems && filteredItems.length > 0 && STATE.itemsBase.length === 0) {
2998
+ if (filteredItems && filteredItems.length > 0) {
2869
2999
  LogHelper2.log(`[${config.widgetName}] Provide-data event: ${filteredItems.length} items (from ${items?.length || 0})`);
2870
3000
  STATE.itemsBase = filteredItems;
2871
3001
  STATE.itemsEnriched = filteredItems.map((item) => ({
@@ -2888,7 +3018,7 @@ function createWidgetController(config) {
2888
3018
  } else if (config.context === "entrada") {
2889
3019
  items = waterClassified.entrada?.items;
2890
3020
  }
2891
- if (items && items.length > 0 && STATE.itemsBase.length === 0) {
3021
+ if (items && items.length > 0) {
2892
3022
  LogHelper2.log(`[${config.widgetName}] water-tb-data-ready event: ${items.length} items for context ${config.context}`);
2893
3023
  STATE.itemsBase = items;
2894
3024
  STATE.itemsEnriched = items.map((item) => ({
@@ -2978,6 +3108,18 @@ function createWidgetController(config) {
2978
3108
  }
2979
3109
  },
2980
3110
  onDataUpdated: function() {
3111
+ const items = getCachedData(config);
3112
+ if (items && items.length > 0) {
3113
+ LogHelper2.log(`[${config.widgetName}] onDataUpdated: refreshing from cache with ${items.length} items`);
3114
+ STATE.itemsBase = items;
3115
+ STATE.itemsEnriched = items.map((item) => ({
3116
+ ...item,
3117
+ value: Number(item.value || item.consumption || item.pulses || 0)
3118
+ }));
3119
+ STATE.dataFromMain = true;
3120
+ if (busyModalRef) busyModalRef.hideBusy();
3121
+ reflow();
3122
+ }
2981
3123
  },
2982
3124
  onDestroy: function() {
2983
3125
  LogHelper2.log(`[${config.widgetName}] RFC-0143 Factory Controller - onDestroy`);