@enyo-energy/sunspec-sdk 1.6.0 → 1.7.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.
@@ -4,6 +4,7 @@ exports.SunspecBatteryFeatureCalibrator = void 0;
4
4
  exports.decodeFeatureResults = decodeFeatureResults;
5
5
  const appliance_calibration_1 = require("@enyo-energy/appliance-calibration");
6
6
  const enyo_battery_appliance_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js");
7
+ const sunspec_battery_grid_charge_js_1 = require("./sunspec-battery-grid-charge.cjs");
7
8
  const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
8
9
  /**
9
10
  * SoC band each probe needs to operate in. Outside the band the probe returns
@@ -57,6 +58,7 @@ class SunspecBatteryFeatureCalibrator extends appliance_calibration_1.BatteryCal
57
58
  sunspecClient;
58
59
  unitId;
59
60
  readBatteryData;
61
+ gridChargeMethod;
60
62
  probeConfig;
61
63
  probeClock;
62
64
  // Local refs to the same instances the parent stores privately. We keep
@@ -71,6 +73,7 @@ class SunspecBatteryFeatureCalibrator extends appliance_calibration_1.BatteryCal
71
73
  this.sunspecClient = options.sunspecClient;
72
74
  this.unitId = options.unitId;
73
75
  this.readBatteryData = options.readBatteryData;
76
+ this.gridChargeMethod = options.gridChargeMethod ?? sunspec_interfaces_js_1.SunspecGridChargeMethod.ChargeRateLimit;
74
77
  this.probeConfig = { ...appliance_calibration_1.DEFAULT_BATTERY_CALIBRATOR_CONFIG, ...options.config };
75
78
  this.probeSnapshotService = options.snapshotService;
76
79
  this.probeResultStore = options.resultStore;
@@ -161,11 +164,10 @@ class SunspecBatteryFeatureCalibrator extends appliance_calibration_1.BatteryCal
161
164
  }
162
165
  const baselineBatW = this.sunspecDriver.readBatteryPowerW() ?? 0;
163
166
  const baselineGridW = this.sunspecDriver.readGridPowerW() ?? 0;
167
+ // wChaMax is lowered to the test power, so a 100 % rate charges at exactly testPowerW.
164
168
  const wrote = await this.sunspecClient.writeBatteryControls(this.unitId, {
165
- chaGriSet: sunspec_interfaces_js_1.SunspecChargeSource.GRID,
166
169
  wChaMax: this.probeConfig.testPowerW,
167
- inWRte: 100,
168
- storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE,
170
+ ...(0, sunspec_battery_grid_charge_js_1.gridChargeControls)(this.gridChargeMethod, 100),
169
171
  });
170
172
  if (!wrote) {
171
173
  return { verdict: 'failed', note: 'writeBatteryControls returned false for grid charge' };
@@ -1,12 +1,14 @@
1
1
  import { BatteryCalibrator, type BatteryCalibratorOptions, type CalibrationResult } from "@enyo-energy/appliance-calibration";
2
2
  import { EnyoBatteryFeature } from "@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js";
3
3
  import { BatteryControlsClient } from "./sunspec-battery-schedule-handler.cjs";
4
- import { type SunspecBatteryControls, type SunspecBatteryData } from "./sunspec-interfaces.cjs";
4
+ import { type SunspecBatteryControls, type SunspecBatteryData, SunspecGridChargeMethod } from "./sunspec-interfaces.cjs";
5
5
  export interface SunspecBatteryFeatureCalibratorOptions extends BatteryCalibratorOptions<SunspecBatteryControls> {
6
6
  sunspecClient: BatteryControlsClient;
7
7
  unitId: number;
8
8
  /** Pulls the latest battery data so the orchestrator can pre-check SoC. */
9
9
  readBatteryData: () => Promise<SunspecBatteryData | null>;
10
+ /** Register form the grid-charge probe tests; must match the schedule handler's. Defaults to ChargeRateLimit. */
11
+ gridChargeMethod?: SunspecGridChargeMethod;
10
12
  }
11
13
  /**
12
14
  * SunSpec-specific calibrator that exercises each of the four controllable
@@ -48,6 +50,7 @@ export declare class SunspecBatteryFeatureCalibrator extends BatteryCalibrator<S
48
50
  private readonly sunspecClient;
49
51
  private readonly unitId;
50
52
  private readonly readBatteryData;
53
+ private readonly gridChargeMethod;
51
54
  private readonly probeConfig;
52
55
  private readonly probeClock;
53
56
  private readonly probeSnapshotService;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gridChargeControls = gridChargeControls;
4
+ exports.gridChargeFields = gridChargeFields;
5
+ const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
6
+ /**
7
+ * The Model 124 register set that charges the battery from the grid at `ratePct` of `WChaMax`,
8
+ * in the form the device acts on — see {@link SunspecGridChargeMethod}. Shared by the schedule
9
+ * handler and the calibrator's grid-charge probe, so the probe tests the write production uses.
10
+ */
11
+ function gridChargeControls(method, ratePct) {
12
+ switch (method) {
13
+ case sunspec_interfaces_js_1.SunspecGridChargeMethod.ChargeRateLimit:
14
+ return {
15
+ chaGriSet: sunspec_interfaces_js_1.SunspecChargeSource.GRID,
16
+ inWRte: ratePct,
17
+ storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE,
18
+ };
19
+ case sunspec_interfaces_js_1.SunspecGridChargeMethod.NegativeDischargeRate:
20
+ return {
21
+ chaGriSet: sunspec_interfaces_js_1.SunspecChargeSource.GRID,
22
+ inWRte: 100,
23
+ outWRte: -ratePct,
24
+ storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE,
25
+ };
26
+ }
27
+ }
28
+ /** The fields {@link gridChargeControls} writes, for the snapshot service's modification record. */
29
+ function gridChargeFields(controls) {
30
+ const fields = ['chaGriSet', 'inWRte', 'outWRte', 'storCtlMod'];
31
+ return fields.filter(field => controls[field] !== undefined);
32
+ }
@@ -0,0 +1,9 @@
1
+ import { type SunspecBatteryControls, SunspecGridChargeMethod } from "./sunspec-interfaces.cjs";
2
+ /**
3
+ * The Model 124 register set that charges the battery from the grid at `ratePct` of `WChaMax`,
4
+ * in the form the device acts on — see {@link SunspecGridChargeMethod}. Shared by the schedule
5
+ * handler and the calibrator's grid-charge probe, so the probe tests the write production uses.
6
+ */
7
+ export declare function gridChargeControls(method: SunspecGridChargeMethod, ratePct: number): Partial<SunspecBatteryControls>;
8
+ /** The fields {@link gridChargeControls} writes, for the snapshot service's modification record. */
9
+ export declare function gridChargeFields(controls: Partial<SunspecBatteryControls>): (keyof SunspecBatteryControls)[];
@@ -4,6 +4,7 @@ exports.SunspecBatteryScheduleHandler = void 0;
4
4
  const storage_schedule_handler_js_1 = require("@enyo-energy/energy-app-sdk/dist/implementations/storage/storage-schedule-handler.js");
5
5
  const enyo_data_bus_value_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js");
6
6
  const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
7
+ const sunspec_battery_grid_charge_js_1 = require("./sunspec-battery-grid-charge.cjs");
7
8
  /**
8
9
  * How often the keepalive checks whether the active limit is due a refresh.
9
10
  * The refresh itself happens every third of the revert timeout; this only
@@ -23,6 +24,7 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
23
24
  applianceIdForLog;
24
25
  getSnapshotService;
25
26
  onErrorCallback;
27
+ gridChargeMethod;
26
28
  installedWChaMax;
27
29
  /**
28
30
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
@@ -73,6 +75,7 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
73
75
  this.applianceIdForLog = opts.applianceId;
74
76
  this.getSnapshotService = opts.getSnapshotService;
75
77
  this.onErrorCallback = opts.onErrorCallback;
78
+ this.gridChargeMethod = opts.gridChargeMethod ?? sunspec_interfaces_js_1.SunspecGridChargeMethod.ChargeRateLimit;
76
79
  this.interval = opts.interval;
77
80
  this.clock = opts.now ?? (() => Date.now());
78
81
  }
@@ -295,12 +298,9 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
295
298
  }
296
299
  const pct = Math.min(100, Math.max(0, (powerW / baseline) * 100));
297
300
  if (direction === enyo_data_bus_value_js_1.EnyoStorageScheduleDirectionEnum.Charge) {
298
- await this.sunspecClient.writeBatteryControls(this.unitId, {
299
- chaGriSet: sunspec_interfaces_js_1.SunspecChargeSource.GRID,
300
- inWRte: pct,
301
- storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE,
302
- });
303
- await this.getSnapshotService()?.recordModification(['chaGriSet', 'inWRte', 'storCtlMod']);
301
+ const controls = (0, sunspec_battery_grid_charge_js_1.gridChargeControls)(this.gridChargeMethod, pct);
302
+ await this.sunspecClient.writeBatteryControls(this.unitId, controls);
303
+ await this.getSnapshotService()?.recordModification((0, sunspec_battery_grid_charge_js_1.gridChargeFields)(controls));
304
304
  }
305
305
  else {
306
306
  await this.sunspecClient.writeBatteryControls(this.unitId, {
@@ -1,7 +1,7 @@
1
1
  import { StorageScheduleHandler, type ActiveStorageScheduleEntry, type StorageScheduleHandlerOptions } from "@enyo-energy/energy-app-sdk/dist/implementations/storage/storage-schedule-handler.js";
2
2
  import { type EnyoDataBusSetStorageScheduleV1 } from "@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js";
3
3
  import { type SnapshotService } from "@enyo-energy/appliance-calibration";
4
- import { type SunspecBatteryControls } from "./sunspec-interfaces.cjs";
4
+ import { type SunspecBatteryControls, SunspecGridChargeMethod } from "./sunspec-interfaces.cjs";
5
5
  /**
6
6
  * Narrow interface covering only the modbus reads/writes the schedule handler
7
7
  * needs. The full `SunspecModbusClient` structurally satisfies this; pinning
@@ -47,6 +47,8 @@ export interface SunspecBatteryScheduleHandlerOptions extends StorageScheduleHan
47
47
  /** Lazy so the snapshot service can be initialised after the handler. */
48
48
  getSnapshotService: () => BatteryModificationRecorder | undefined;
49
49
  onErrorCallback?: (err: Error) => void;
50
+ /** Register form of a Charge entry. Defaults to {@link SunspecGridChargeMethod.ChargeRateLimit}. */
51
+ gridChargeMethod?: SunspecGridChargeMethod;
50
52
  }
51
53
  /**
52
54
  * Concrete `StorageScheduleHandler` for SunSpec Model 124 batteries. The base
@@ -61,6 +63,7 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
61
63
  private readonly applianceIdForLog;
62
64
  private readonly getSnapshotService;
63
65
  private readonly onErrorCallback?;
66
+ private readonly gridChargeMethod;
64
67
  private installedWChaMax;
65
68
  /**
66
69
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
@@ -1518,6 +1518,14 @@ function deriveStorageMode(controls) {
1518
1518
  if (chargeLimited && dischargeLimited && controls.inWRte === 0 && controls.outWRte === 0) {
1519
1519
  return sunspec_interfaces_js_1.SunspecStorageMode.HOLDING;
1520
1520
  }
1521
+ // A limit below zero reverses its direction: a negative discharge rate is a forced charge
1522
+ // (SunspecGridChargeMethod.NegativeDischargeRate), a negative charge rate a forced discharge.
1523
+ if (dischargeLimited && controls.outWRte !== undefined && controls.outWRte < 0) {
1524
+ return sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
1525
+ }
1526
+ if (chargeLimited && controls.inWRte !== undefined && controls.inWRte < 0) {
1527
+ return sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE;
1528
+ }
1521
1529
  return dischargeLimited && !chargeLimited
1522
1530
  ? sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE
1523
1531
  : sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
@@ -1540,6 +1548,8 @@ class SunspecBattery extends BaseSunspecDevice {
1540
1548
  batteryCalibrator;
1541
1549
  calibrationResultStore;
1542
1550
  scheduleHandler;
1551
+ /** Register form of a grid charge, for both the schedule handler and the calibration probe. */
1552
+ gridChargeMethod;
1543
1553
  /**
1544
1554
  * Battery features that imply outbound control writes from the host action-taker.
1545
1555
  * These are stripped from `appliance.battery.features` until the calibration result
@@ -1553,13 +1563,14 @@ class SunspecBattery extends BaseSunspecDevice {
1553
1563
  enyo_battery_appliance_js_1.EnyoBatteryFeature.DischargeLimitation,
1554
1564
  ];
1555
1565
  constructor(energyApp, name, networkDevice, sunspecClient, applianceManager, featureMode, unitId = 1, port = 502, baseAddress = 40000, capabilities = [], retryConfig, appliance, useTls,
1556
- // Battery facts no register carries (currently the measured round-trip efficiency).
1557
- // Forwarded onto `appliance.battery` on connect; see SunspecBatteryMetadataConfig.
1558
- metadataConfig = {}) {
1566
+ // Battery facts no register carries (forwarded onto `appliance.battery` on connect;
1567
+ // see SunspecBatteryMetadataConfig) plus how the device is driven.
1568
+ config = {}) {
1559
1569
  super(energyApp, name, networkDevice, sunspecClient, applianceManager, unitId, port, baseAddress, retryConfig, appliance, useTls);
1560
1570
  this.featureMode = featureMode;
1561
1571
  this.capabilities = capabilities;
1562
- this.roundTripEfficiencyPercent = metadataConfig.roundTripEfficiencyPercent;
1572
+ this.roundTripEfficiencyPercent = config.roundTripEfficiencyPercent;
1573
+ this.gridChargeMethod = config.gridChargeMethod ?? sunspec_interfaces_js_1.SunspecGridChargeMethod.ChargeRateLimit;
1563
1574
  }
1564
1575
  /**
1565
1576
  * Last known round-trip efficiency, seeded from the constructor config and replaceable at
@@ -1606,6 +1617,19 @@ class SunspecBattery extends BaseSunspecDevice {
1606
1617
  ? {}
1607
1618
  : { roundTripEfficiencyPercent: this.roundTripEfficiencyPercent };
1608
1619
  }
1620
+ /**
1621
+ * Whether the host may charge this battery from the grid. Follows the advertised
1622
+ * `GridCharging` feature, NOT the live `chaGriSet` register: our own schedule handler
1623
+ * writes GRID on every Charge and PV on Discharge/Auto, so the register only says
1624
+ * whether we are grid-charging right now. Publishing it made the flag `false` whenever
1625
+ * the pack sat on Auto (all night), and enyo-control honours an explicit `false` by
1626
+ * planning no buys at all (ENY-1241). The value is always set, never omitted, because
1627
+ * `updateAppliance` merges `battery` and would otherwise keep a stale `false`.
1628
+ * The live register stays available through {@link isGridChargingEnabled}.
1629
+ */
1630
+ gridChargingEnabled(features) {
1631
+ return features.includes(enyo_battery_appliance_js_1.EnyoBatteryFeature.GridCharging);
1632
+ }
1609
1633
  /**
1610
1634
  * Wire the battery into `@enyo-energy/appliance-calibration`'s
1611
1635
  * `BatteryCalibrator` test-charge flow. Call once after {@link connect}.
@@ -1640,6 +1664,7 @@ class SunspecBattery extends BaseSunspecDevice {
1640
1664
  sunspecClient: this.sunspecClient,
1641
1665
  unitId: this.unitId,
1642
1666
  readBatteryData: () => this.sunspecClient.readBatteryData(this.unitId),
1667
+ gridChargeMethod: this.gridChargeMethod,
1643
1668
  snapshotService: this.snapshotService,
1644
1669
  resultStore: opts.resultStore,
1645
1670
  config: opts.config,
@@ -1734,7 +1759,7 @@ class SunspecBattery extends BaseSunspecDevice {
1734
1759
  activeChargeLimitW: activeChargeLimitW,
1735
1760
  activeDischargeLimitW: activeDischargeLimitW,
1736
1761
  features,
1737
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1762
+ gridChargingEnabled: this.gridChargingEnabled(features),
1738
1763
  ...this.configuredBatteryMetadata()
1739
1764
  }
1740
1765
  });
@@ -1763,7 +1788,7 @@ class SunspecBattery extends BaseSunspecDevice {
1763
1788
  activeChargeLimitW: activeChargeLimitW,
1764
1789
  activeDischargeLimitW: activeDischargeLimitW,
1765
1790
  features,
1766
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1791
+ gridChargingEnabled: this.gridChargingEnabled(features),
1767
1792
  ...this.configuredBatteryMetadata()
1768
1793
  }
1769
1794
  });
@@ -1932,7 +1957,7 @@ class SunspecBattery extends BaseSunspecDevice {
1932
1957
  activeChargeLimitW: this.computeActiveChargeLimitW(batteryData),
1933
1958
  activeDischargeLimitW: this.computeActiveDischargeLimitW(batteryData),
1934
1959
  features,
1935
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1960
+ gridChargingEnabled: this.gridChargingEnabled(features),
1936
1961
  ...this.configuredBatteryMetadata()
1937
1962
  }
1938
1963
  });
@@ -2116,7 +2141,7 @@ class SunspecBattery extends BaseSunspecDevice {
2116
2141
  }
2117
2142
  if (batteryData.inWRte >= 100)
2118
2143
  return null;
2119
- return (batteryData.wChaMax * batteryData.inWRte) / 100;
2144
+ return (batteryData.wChaMax * Math.max(0, batteryData.inWRte)) / 100;
2120
2145
  }
2121
2146
  /**
2122
2147
  * Compute the currently active discharge limit in Watts from Model 124's
@@ -2129,7 +2154,8 @@ class SunspecBattery extends BaseSunspecDevice {
2129
2154
  }
2130
2155
  if (batteryData.outWRte >= 100)
2131
2156
  return null;
2132
- return (batteryData.wChaMax * batteryData.outWRte) / 100;
2157
+ // A negative rate is a forced charge, not a negative limit: nothing may be discharged.
2158
+ return (batteryData.wChaMax * Math.max(0, batteryData.outWRte)) / 100;
2133
2159
  }
2134
2160
  determineStorageMode(controls) {
2135
2161
  return deriveStorageMode(controls);
@@ -2171,6 +2197,7 @@ class SunspecBattery extends BaseSunspecDevice {
2171
2197
  sunspecClient: this.sunspecClient,
2172
2198
  unitId: this.unitId,
2173
2199
  getSnapshotService: () => this.snapshotService,
2200
+ gridChargeMethod: this.gridChargeMethod,
2174
2201
  onErrorCallback: (err) => console.warn(`Battery ${this.applianceId}: schedule handler rejected input: ${err.message}`),
2175
2202
  });
2176
2203
  console.log(`Battery ${this.applianceId}: started data bus listening (listener ${this.dataBusListenerId})`);
@@ -1,4 +1,4 @@
1
- import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, type SunspecInverterMetadataConfig, type SunspecBatteryMetadataConfig, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.cjs";
1
+ import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, type SunspecInverterMetadataConfig, type SunspecBatteryConfig, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.cjs";
2
2
  import { ApplianceManager, EnergyApp } from "@enyo-energy/energy-app-sdk";
3
3
  import { type EnyoAppliance, type EnyoApplianceErrorCode, EnyoApplianceName } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
4
4
  import { EnyoNetworkDevice } from "@enyo-energy/energy-app-sdk/dist/types/enyo-network-device.js";
@@ -557,6 +557,8 @@ export declare class SunspecBattery extends BaseSunspecDevice {
557
557
  private batteryCalibrator?;
558
558
  private calibrationResultStore?;
559
559
  private scheduleHandler;
560
+ /** Register form of a grid charge, for both the schedule handler and the calibration probe. */
561
+ private readonly gridChargeMethod;
560
562
  /**
561
563
  * Battery features that imply outbound control writes from the host action-taker.
562
564
  * These are stripped from `appliance.battery.features` until the calibration result
@@ -564,7 +566,7 @@ export declare class SunspecBattery extends BaseSunspecDevice {
564
566
  * {@link computeAdvertisedFeatures}.
565
567
  */
566
568
  private static readonly CONTROLLABLE_FEATURES;
567
- constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, metadataConfig?: SunspecBatteryMetadataConfig);
569
+ constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, config?: SunspecBatteryConfig);
568
570
  /**
569
571
  * Last known round-trip efficiency, seeded from the constructor config and replaceable at
570
572
  * runtime via {@link setRoundTripEfficiencyPercent}. Undefined = not yet known; the SDK then
@@ -587,6 +589,17 @@ export declare class SunspecBattery extends BaseSunspecDevice {
587
589
  * Spreading an explicit `undefined` would erase a value the host set elsewhere.
588
590
  */
589
591
  private configuredBatteryMetadata;
592
+ /**
593
+ * Whether the host may charge this battery from the grid. Follows the advertised
594
+ * `GridCharging` feature, NOT the live `chaGriSet` register: our own schedule handler
595
+ * writes GRID on every Charge and PV on Discharge/Auto, so the register only says
596
+ * whether we are grid-charging right now. Publishing it made the flag `false` whenever
597
+ * the pack sat on Auto (all night), and enyo-control honours an explicit `false` by
598
+ * planning no buys at all (ENY-1241). The value is always set, never omitted, because
599
+ * `updateAppliance` merges `battery` and would otherwise keep a stale `false`.
600
+ * The live register stays available through {@link isGridChargingEnabled}.
601
+ */
602
+ private gridChargingEnabled;
590
603
  /**
591
604
  * Wire the battery into `@enyo-energy/appliance-calibration`'s
592
605
  * `BatteryCalibrator` test-charge flow. Call once after {@link connect}.
@@ -3,7 +3,7 @@
3
3
  * SunSpec block interfaces with block numbers
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SUNSPEC_CONTROLLABLE_FEATURES = exports.SunspecMeterCapability = exports.SunspecBatteryFeatureModeKind = exports.SunspecBatteryCapability = exports.SunspecPvPowerOrigin = exports.SunspecPvPowerSource = exports.SunspecInverterCapability = exports.SunspecStorageMode = exports.SunspecChargeSource = exports.SunspecVArPctMode = exports.SunspecEnableControl = exports.SunspecConnectionControl = exports.MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.DEFAULT_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.SunspecStorageControlMode = exports.SunspecBatteryEvent1 = exports.SunspecBatteryBankState = exports.SunspecBatteryType = exports.SunspecBatteryControlMode = exports.SunspecBatteryChargeState = exports.SunspecInverterOperatingState = exports.SunspecMPPTOperatingState = exports.SUNSPEC_CONNECTION_LOST_CODE = exports.SunspecInverterEvent1 = exports.SunspecModelId = exports.DEFAULT_MODBUS_RESILIENCE_CONFIG = exports.DEFAULT_RETRY_CONFIG = void 0;
6
+ exports.SUNSPEC_CONTROLLABLE_FEATURES = exports.SunspecMeterCapability = exports.SunspecBatteryFeatureModeKind = exports.SunspecGridChargeMethod = exports.SunspecBatteryCapability = exports.SunspecPvPowerOrigin = exports.SunspecPvPowerSource = exports.SunspecInverterCapability = exports.SunspecStorageMode = exports.SunspecChargeSource = exports.SunspecVArPctMode = exports.SunspecEnableControl = exports.SunspecConnectionControl = exports.MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.DEFAULT_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = exports.SunspecStorageControlMode = exports.SunspecBatteryEvent1 = exports.SunspecBatteryBankState = exports.SunspecBatteryType = exports.SunspecBatteryControlMode = exports.SunspecBatteryChargeState = exports.SunspecInverterOperatingState = exports.SunspecMPPTOperatingState = exports.SUNSPEC_CONNECTION_LOST_CODE = exports.SunspecInverterEvent1 = exports.SunspecModelId = exports.DEFAULT_MODBUS_RESILIENCE_CONFIG = exports.DEFAULT_RETRY_CONFIG = void 0;
7
7
  const enyo_battery_appliance_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js");
8
8
  exports.DEFAULT_RETRY_CONFIG = {
9
9
  phases: [
@@ -348,6 +348,25 @@ var SunspecBatteryCapability;
348
348
  SunspecBatteryCapability["DischargeLimit"] = "discharge-limit";
349
349
  SunspecBatteryCapability["ChargeLimit"] = "charge-limit";
350
350
  })(SunspecBatteryCapability || (exports.SunspecBatteryCapability = SunspecBatteryCapability = {}));
351
+ /**
352
+ * How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
353
+ * differently, so the host picks the form its hardware actually acts on.
354
+ */
355
+ var SunspecGridChargeMethod;
356
+ (function (SunspecGridChargeMethod) {
357
+ /**
358
+ * StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
359
+ * InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
360
+ */
361
+ SunspecGridChargeMethod["ChargeRateLimit"] = "charge-rate-limit";
362
+ /**
363
+ * StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
364
+ * below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
365
+ * by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
366
+ * Home Assistant fronius_modbus integration's "charge from grid").
367
+ */
368
+ SunspecGridChargeMethod["NegativeDischargeRate"] = "negative-discharge-rate";
369
+ })(SunspecGridChargeMethod || (exports.SunspecGridChargeMethod = SunspecGridChargeMethod = {}));
351
370
  /**
352
371
  * Selects how `SunspecBattery` populates `appliance.battery.features`.
353
372
  *
@@ -977,6 +977,24 @@ export declare enum SunspecBatteryCapability {
977
977
  DischargeLimit = "discharge-limit",
978
978
  ChargeLimit = "charge-limit"
979
979
  }
980
+ /**
981
+ * How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
982
+ * differently, so the host picks the form its hardware actually acts on.
983
+ */
984
+ export declare enum SunspecGridChargeMethod {
985
+ /**
986
+ * StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
987
+ * InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
988
+ */
989
+ ChargeRateLimit = "charge-rate-limit",
990
+ /**
991
+ * StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
992
+ * below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
993
+ * by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
994
+ * Home Assistant fronius_modbus integration's "charge from grid").
995
+ */
996
+ NegativeDischargeRate = "negative-discharge-rate"
997
+ }
980
998
  /**
981
999
  * Battery facts that live outside the SunSpec register map, published onto `appliance.battery` when
982
1000
  * the device connects. Fields left `undefined` are not written, so an existing value in the
@@ -996,6 +1014,11 @@ export interface SunspecBatteryMetadataConfig {
996
1014
  */
997
1015
  roundTripEfficiencyPercent?: number;
998
1016
  }
1017
+ /** Per-battery configuration: the published metadata plus how the device is driven. */
1018
+ export interface SunspecBatteryConfig extends SunspecBatteryMetadataConfig {
1019
+ /** Register form for a grid charge. Defaults to {@link SunspecGridChargeMethod.ChargeRateLimit}. */
1020
+ gridChargeMethod?: SunspecGridChargeMethod;
1021
+ }
999
1022
  /**
1000
1023
  * Selects how `SunspecBattery` populates `appliance.battery.features`.
1001
1024
  *
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
9
9
  /**
10
10
  * Current version of the enyo Energy App SDK.
11
11
  */
12
- exports.SDK_VERSION = '1.6.0';
12
+ exports.SDK_VERSION = '1.7.0';
13
13
  /**
14
14
  * Gets the current SDK version.
15
15
  * @returns The semantic version string of the SDK
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "1.6.0";
8
+ export declare const SDK_VERSION = "1.7.0";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -1,12 +1,14 @@
1
1
  import { BatteryCalibrator, type BatteryCalibratorOptions, type CalibrationResult } from "@enyo-energy/appliance-calibration";
2
2
  import { EnyoBatteryFeature } from "@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js";
3
3
  import { BatteryControlsClient } from "./sunspec-battery-schedule-handler.js";
4
- import { type SunspecBatteryControls, type SunspecBatteryData } from "./sunspec-interfaces.js";
4
+ import { type SunspecBatteryControls, type SunspecBatteryData, SunspecGridChargeMethod } from "./sunspec-interfaces.js";
5
5
  export interface SunspecBatteryFeatureCalibratorOptions extends BatteryCalibratorOptions<SunspecBatteryControls> {
6
6
  sunspecClient: BatteryControlsClient;
7
7
  unitId: number;
8
8
  /** Pulls the latest battery data so the orchestrator can pre-check SoC. */
9
9
  readBatteryData: () => Promise<SunspecBatteryData | null>;
10
+ /** Register form the grid-charge probe tests; must match the schedule handler's. Defaults to ChargeRateLimit. */
11
+ gridChargeMethod?: SunspecGridChargeMethod;
10
12
  }
11
13
  /**
12
14
  * SunSpec-specific calibrator that exercises each of the four controllable
@@ -48,6 +50,7 @@ export declare class SunspecBatteryFeatureCalibrator extends BatteryCalibrator<S
48
50
  private readonly sunspecClient;
49
51
  private readonly unitId;
50
52
  private readonly readBatteryData;
53
+ private readonly gridChargeMethod;
51
54
  private readonly probeConfig;
52
55
  private readonly probeClock;
53
56
  private readonly probeSnapshotService;
@@ -1,6 +1,7 @@
1
1
  import { BatteryCalibrator, DEFAULT_BATTERY_CALIBRATOR_CONFIG, } from "@enyo-energy/appliance-calibration";
2
2
  import { EnyoBatteryFeature } from "@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js";
3
- import { SUNSPEC_CONTROLLABLE_FEATURES, SunspecChargeSource, SunspecStorageControlMode, } from "./sunspec-interfaces.js";
3
+ import { gridChargeControls } from "./sunspec-battery-grid-charge.js";
4
+ import { SUNSPEC_CONTROLLABLE_FEATURES, SunspecChargeSource, SunspecGridChargeMethod, SunspecStorageControlMode, } from "./sunspec-interfaces.js";
4
5
  /**
5
6
  * SoC band each probe needs to operate in. Outside the band the probe returns
6
7
  * `not-supported` instead of running — there's no useful signal when the
@@ -53,6 +54,7 @@ export class SunspecBatteryFeatureCalibrator extends BatteryCalibrator {
53
54
  sunspecClient;
54
55
  unitId;
55
56
  readBatteryData;
57
+ gridChargeMethod;
56
58
  probeConfig;
57
59
  probeClock;
58
60
  // Local refs to the same instances the parent stores privately. We keep
@@ -67,6 +69,7 @@ export class SunspecBatteryFeatureCalibrator extends BatteryCalibrator {
67
69
  this.sunspecClient = options.sunspecClient;
68
70
  this.unitId = options.unitId;
69
71
  this.readBatteryData = options.readBatteryData;
72
+ this.gridChargeMethod = options.gridChargeMethod ?? SunspecGridChargeMethod.ChargeRateLimit;
70
73
  this.probeConfig = { ...DEFAULT_BATTERY_CALIBRATOR_CONFIG, ...options.config };
71
74
  this.probeSnapshotService = options.snapshotService;
72
75
  this.probeResultStore = options.resultStore;
@@ -157,11 +160,10 @@ export class SunspecBatteryFeatureCalibrator extends BatteryCalibrator {
157
160
  }
158
161
  const baselineBatW = this.sunspecDriver.readBatteryPowerW() ?? 0;
159
162
  const baselineGridW = this.sunspecDriver.readGridPowerW() ?? 0;
163
+ // wChaMax is lowered to the test power, so a 100 % rate charges at exactly testPowerW.
160
164
  const wrote = await this.sunspecClient.writeBatteryControls(this.unitId, {
161
- chaGriSet: SunspecChargeSource.GRID,
162
165
  wChaMax: this.probeConfig.testPowerW,
163
- inWRte: 100,
164
- storCtlMod: SunspecStorageControlMode.CHARGE,
166
+ ...gridChargeControls(this.gridChargeMethod, 100),
165
167
  });
166
168
  if (!wrote) {
167
169
  return { verdict: 'failed', note: 'writeBatteryControls returned false for grid charge' };
@@ -0,0 +1,9 @@
1
+ import { type SunspecBatteryControls, SunspecGridChargeMethod } from "./sunspec-interfaces.js";
2
+ /**
3
+ * The Model 124 register set that charges the battery from the grid at `ratePct` of `WChaMax`,
4
+ * in the form the device acts on — see {@link SunspecGridChargeMethod}. Shared by the schedule
5
+ * handler and the calibrator's grid-charge probe, so the probe tests the write production uses.
6
+ */
7
+ export declare function gridChargeControls(method: SunspecGridChargeMethod, ratePct: number): Partial<SunspecBatteryControls>;
8
+ /** The fields {@link gridChargeControls} writes, for the snapshot service's modification record. */
9
+ export declare function gridChargeFields(controls: Partial<SunspecBatteryControls>): (keyof SunspecBatteryControls)[];
@@ -0,0 +1,28 @@
1
+ import { SunspecChargeSource, SunspecGridChargeMethod, SunspecStorageControlMode, } from "./sunspec-interfaces.js";
2
+ /**
3
+ * The Model 124 register set that charges the battery from the grid at `ratePct` of `WChaMax`,
4
+ * in the form the device acts on — see {@link SunspecGridChargeMethod}. Shared by the schedule
5
+ * handler and the calibrator's grid-charge probe, so the probe tests the write production uses.
6
+ */
7
+ export function gridChargeControls(method, ratePct) {
8
+ switch (method) {
9
+ case SunspecGridChargeMethod.ChargeRateLimit:
10
+ return {
11
+ chaGriSet: SunspecChargeSource.GRID,
12
+ inWRte: ratePct,
13
+ storCtlMod: SunspecStorageControlMode.CHARGE,
14
+ };
15
+ case SunspecGridChargeMethod.NegativeDischargeRate:
16
+ return {
17
+ chaGriSet: SunspecChargeSource.GRID,
18
+ inWRte: 100,
19
+ outWRte: -ratePct,
20
+ storCtlMod: SunspecStorageControlMode.DISCHARGE,
21
+ };
22
+ }
23
+ }
24
+ /** The fields {@link gridChargeControls} writes, for the snapshot service's modification record. */
25
+ export function gridChargeFields(controls) {
26
+ const fields = ['chaGriSet', 'inWRte', 'outWRte', 'storCtlMod'];
27
+ return fields.filter(field => controls[field] !== undefined);
28
+ }
@@ -1,7 +1,7 @@
1
1
  import { StorageScheduleHandler, type ActiveStorageScheduleEntry, type StorageScheduleHandlerOptions } from "@enyo-energy/energy-app-sdk/dist/implementations/storage/storage-schedule-handler.js";
2
2
  import { type EnyoDataBusSetStorageScheduleV1 } from "@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js";
3
3
  import { type SnapshotService } from "@enyo-energy/appliance-calibration";
4
- import { type SunspecBatteryControls } from "./sunspec-interfaces.js";
4
+ import { type SunspecBatteryControls, SunspecGridChargeMethod } from "./sunspec-interfaces.js";
5
5
  /**
6
6
  * Narrow interface covering only the modbus reads/writes the schedule handler
7
7
  * needs. The full `SunspecModbusClient` structurally satisfies this; pinning
@@ -47,6 +47,8 @@ export interface SunspecBatteryScheduleHandlerOptions extends StorageScheduleHan
47
47
  /** Lazy so the snapshot service can be initialised after the handler. */
48
48
  getSnapshotService: () => BatteryModificationRecorder | undefined;
49
49
  onErrorCallback?: (err: Error) => void;
50
+ /** Register form of a Charge entry. Defaults to {@link SunspecGridChargeMethod.ChargeRateLimit}. */
51
+ gridChargeMethod?: SunspecGridChargeMethod;
50
52
  }
51
53
  /**
52
54
  * Concrete `StorageScheduleHandler` for SunSpec Model 124 batteries. The base
@@ -61,6 +63,7 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
61
63
  private readonly applianceIdForLog;
62
64
  private readonly getSnapshotService;
63
65
  private readonly onErrorCallback?;
66
+ private readonly gridChargeMethod;
64
67
  private installedWChaMax;
65
68
  /**
66
69
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
@@ -1,6 +1,7 @@
1
1
  import { StorageScheduleHandler, } from "@enyo-energy/energy-app-sdk/dist/implementations/storage/storage-schedule-handler.js";
2
2
  import { EnyoStorageScheduleDirectionEnum, EnyoStorageScheduleModeEnum, } from "@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js";
3
- import { SunspecChargeSource, SunspecStorageControlMode, } from "./sunspec-interfaces.js";
3
+ import { SunspecChargeSource, SunspecGridChargeMethod, SunspecStorageControlMode, } from "./sunspec-interfaces.js";
4
+ import { gridChargeControls, gridChargeFields } from "./sunspec-battery-grid-charge.js";
4
5
  /**
5
6
  * How often the keepalive checks whether the active limit is due a refresh.
6
7
  * The refresh itself happens every third of the revert timeout; this only
@@ -20,6 +21,7 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
20
21
  applianceIdForLog;
21
22
  getSnapshotService;
22
23
  onErrorCallback;
24
+ gridChargeMethod;
23
25
  installedWChaMax;
24
26
  /**
25
27
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
@@ -70,6 +72,7 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
70
72
  this.applianceIdForLog = opts.applianceId;
71
73
  this.getSnapshotService = opts.getSnapshotService;
72
74
  this.onErrorCallback = opts.onErrorCallback;
75
+ this.gridChargeMethod = opts.gridChargeMethod ?? SunspecGridChargeMethod.ChargeRateLimit;
73
76
  this.interval = opts.interval;
74
77
  this.clock = opts.now ?? (() => Date.now());
75
78
  }
@@ -292,12 +295,9 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
292
295
  }
293
296
  const pct = Math.min(100, Math.max(0, (powerW / baseline) * 100));
294
297
  if (direction === EnyoStorageScheduleDirectionEnum.Charge) {
295
- await this.sunspecClient.writeBatteryControls(this.unitId, {
296
- chaGriSet: SunspecChargeSource.GRID,
297
- inWRte: pct,
298
- storCtlMod: SunspecStorageControlMode.CHARGE,
299
- });
300
- await this.getSnapshotService()?.recordModification(['chaGriSet', 'inWRte', 'storCtlMod']);
298
+ const controls = gridChargeControls(this.gridChargeMethod, pct);
299
+ await this.sunspecClient.writeBatteryControls(this.unitId, controls);
300
+ await this.getSnapshotService()?.recordModification(gridChargeFields(controls));
301
301
  }
302
302
  else {
303
303
  await this.sunspecClient.writeBatteryControls(this.unitId, {
@@ -1,4 +1,4 @@
1
- import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, type SunspecInverterMetadataConfig, type SunspecBatteryMetadataConfig, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.js";
1
+ import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, type SunspecInverterMetadataConfig, type SunspecBatteryConfig, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.js";
2
2
  import { ApplianceManager, EnergyApp } from "@enyo-energy/energy-app-sdk";
3
3
  import { type EnyoAppliance, type EnyoApplianceErrorCode, EnyoApplianceName } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
4
4
  import { EnyoNetworkDevice } from "@enyo-energy/energy-app-sdk/dist/types/enyo-network-device.js";
@@ -557,6 +557,8 @@ export declare class SunspecBattery extends BaseSunspecDevice {
557
557
  private batteryCalibrator?;
558
558
  private calibrationResultStore?;
559
559
  private scheduleHandler;
560
+ /** Register form of a grid charge, for both the schedule handler and the calibration probe. */
561
+ private readonly gridChargeMethod;
560
562
  /**
561
563
  * Battery features that imply outbound control writes from the host action-taker.
562
564
  * These are stripped from `appliance.battery.features` until the calibration result
@@ -564,7 +566,7 @@ export declare class SunspecBattery extends BaseSunspecDevice {
564
566
  * {@link computeAdvertisedFeatures}.
565
567
  */
566
568
  private static readonly CONTROLLABLE_FEATURES;
567
- constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, metadataConfig?: SunspecBatteryMetadataConfig);
569
+ constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, config?: SunspecBatteryConfig);
568
570
  /**
569
571
  * Last known round-trip efficiency, seeded from the constructor config and replaceable at
570
572
  * runtime via {@link setRoundTripEfficiencyPercent}. Undefined = not yet known; the SDK then
@@ -587,6 +589,17 @@ export declare class SunspecBattery extends BaseSunspecDevice {
587
589
  * Spreading an explicit `undefined` would erase a value the host set elsewhere.
588
590
  */
589
591
  private configuredBatteryMetadata;
592
+ /**
593
+ * Whether the host may charge this battery from the grid. Follows the advertised
594
+ * `GridCharging` feature, NOT the live `chaGriSet` register: our own schedule handler
595
+ * writes GRID on every Charge and PV on Discharge/Auto, so the register only says
596
+ * whether we are grid-charging right now. Publishing it made the flag `false` whenever
597
+ * the pack sat on Auto (all night), and enyo-control honours an explicit `false` by
598
+ * planning no buys at all (ENY-1241). The value is always set, never omitted, because
599
+ * `updateAppliance` merges `battery` and would otherwise keep a stale `false`.
600
+ * The live register stays available through {@link isGridChargingEnabled}.
601
+ */
602
+ private gridChargingEnabled;
590
603
  /**
591
604
  * Wire the battery into `@enyo-energy/appliance-calibration`'s
592
605
  * `BatteryCalibrator` test-charge flow. Call once after {@link connect}.
@@ -1,4 +1,4 @@
1
- import { SunspecBatteryChargeState, SunspecBatteryFeatureModeKind, SunspecEnableControl, SunspecInverterCapability, SunspecPvPowerSource, SunspecPvPowerOrigin, SunspecInverterEvent1, SunspecInverterOperatingState, SunspecModelId, SunspecMPPTOperatingState, SunspecStorageMode, SUNSPEC_CONNECTION_LOST_CODE } from "./sunspec-interfaces.js";
1
+ import { SunspecBatteryChargeState, SunspecBatteryFeatureModeKind, SunspecEnableControl, SunspecInverterCapability, SunspecPvPowerSource, SunspecGridChargeMethod, SunspecPvPowerOrigin, SunspecInverterEvent1, SunspecInverterOperatingState, SunspecModelId, SunspecMPPTOperatingState, SunspecStorageMode, SUNSPEC_CONNECTION_LOST_CODE } from "./sunspec-interfaces.js";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { EnyoApplianceConnectionType, EnyoApplianceStateEnum, EnyoApplianceStatusEnum, EnyoApplianceTopologyFeatureEnum, EnyoApplianceTypeEnum } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
4
4
  import { ConnectionRetryManager } from "./connection-retry-manager.js";
@@ -1507,6 +1507,14 @@ export function deriveStorageMode(controls) {
1507
1507
  if (chargeLimited && dischargeLimited && controls.inWRte === 0 && controls.outWRte === 0) {
1508
1508
  return SunspecStorageMode.HOLDING;
1509
1509
  }
1510
+ // A limit below zero reverses its direction: a negative discharge rate is a forced charge
1511
+ // (SunspecGridChargeMethod.NegativeDischargeRate), a negative charge rate a forced discharge.
1512
+ if (dischargeLimited && controls.outWRte !== undefined && controls.outWRte < 0) {
1513
+ return SunspecStorageMode.CHARGE;
1514
+ }
1515
+ if (chargeLimited && controls.inWRte !== undefined && controls.inWRte < 0) {
1516
+ return SunspecStorageMode.DISCHARGE;
1517
+ }
1510
1518
  return dischargeLimited && !chargeLimited
1511
1519
  ? SunspecStorageMode.DISCHARGE
1512
1520
  : SunspecStorageMode.CHARGE;
@@ -1529,6 +1537,8 @@ export class SunspecBattery extends BaseSunspecDevice {
1529
1537
  batteryCalibrator;
1530
1538
  calibrationResultStore;
1531
1539
  scheduleHandler;
1540
+ /** Register form of a grid charge, for both the schedule handler and the calibration probe. */
1541
+ gridChargeMethod;
1532
1542
  /**
1533
1543
  * Battery features that imply outbound control writes from the host action-taker.
1534
1544
  * These are stripped from `appliance.battery.features` until the calibration result
@@ -1542,13 +1552,14 @@ export class SunspecBattery extends BaseSunspecDevice {
1542
1552
  EnyoBatteryFeature.DischargeLimitation,
1543
1553
  ];
1544
1554
  constructor(energyApp, name, networkDevice, sunspecClient, applianceManager, featureMode, unitId = 1, port = 502, baseAddress = 40000, capabilities = [], retryConfig, appliance, useTls,
1545
- // Battery facts no register carries (currently the measured round-trip efficiency).
1546
- // Forwarded onto `appliance.battery` on connect; see SunspecBatteryMetadataConfig.
1547
- metadataConfig = {}) {
1555
+ // Battery facts no register carries (forwarded onto `appliance.battery` on connect;
1556
+ // see SunspecBatteryMetadataConfig) plus how the device is driven.
1557
+ config = {}) {
1548
1558
  super(energyApp, name, networkDevice, sunspecClient, applianceManager, unitId, port, baseAddress, retryConfig, appliance, useTls);
1549
1559
  this.featureMode = featureMode;
1550
1560
  this.capabilities = capabilities;
1551
- this.roundTripEfficiencyPercent = metadataConfig.roundTripEfficiencyPercent;
1561
+ this.roundTripEfficiencyPercent = config.roundTripEfficiencyPercent;
1562
+ this.gridChargeMethod = config.gridChargeMethod ?? SunspecGridChargeMethod.ChargeRateLimit;
1552
1563
  }
1553
1564
  /**
1554
1565
  * Last known round-trip efficiency, seeded from the constructor config and replaceable at
@@ -1595,6 +1606,19 @@ export class SunspecBattery extends BaseSunspecDevice {
1595
1606
  ? {}
1596
1607
  : { roundTripEfficiencyPercent: this.roundTripEfficiencyPercent };
1597
1608
  }
1609
+ /**
1610
+ * Whether the host may charge this battery from the grid. Follows the advertised
1611
+ * `GridCharging` feature, NOT the live `chaGriSet` register: our own schedule handler
1612
+ * writes GRID on every Charge and PV on Discharge/Auto, so the register only says
1613
+ * whether we are grid-charging right now. Publishing it made the flag `false` whenever
1614
+ * the pack sat on Auto (all night), and enyo-control honours an explicit `false` by
1615
+ * planning no buys at all (ENY-1241). The value is always set, never omitted, because
1616
+ * `updateAppliance` merges `battery` and would otherwise keep a stale `false`.
1617
+ * The live register stays available through {@link isGridChargingEnabled}.
1618
+ */
1619
+ gridChargingEnabled(features) {
1620
+ return features.includes(EnyoBatteryFeature.GridCharging);
1621
+ }
1598
1622
  /**
1599
1623
  * Wire the battery into `@enyo-energy/appliance-calibration`'s
1600
1624
  * `BatteryCalibrator` test-charge flow. Call once after {@link connect}.
@@ -1629,6 +1653,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1629
1653
  sunspecClient: this.sunspecClient,
1630
1654
  unitId: this.unitId,
1631
1655
  readBatteryData: () => this.sunspecClient.readBatteryData(this.unitId),
1656
+ gridChargeMethod: this.gridChargeMethod,
1632
1657
  snapshotService: this.snapshotService,
1633
1658
  resultStore: opts.resultStore,
1634
1659
  config: opts.config,
@@ -1723,7 +1748,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1723
1748
  activeChargeLimitW: activeChargeLimitW,
1724
1749
  activeDischargeLimitW: activeDischargeLimitW,
1725
1750
  features,
1726
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1751
+ gridChargingEnabled: this.gridChargingEnabled(features),
1727
1752
  ...this.configuredBatteryMetadata()
1728
1753
  }
1729
1754
  });
@@ -1752,7 +1777,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1752
1777
  activeChargeLimitW: activeChargeLimitW,
1753
1778
  activeDischargeLimitW: activeDischargeLimitW,
1754
1779
  features,
1755
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1780
+ gridChargingEnabled: this.gridChargingEnabled(features),
1756
1781
  ...this.configuredBatteryMetadata()
1757
1782
  }
1758
1783
  });
@@ -1921,7 +1946,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1921
1946
  activeChargeLimitW: this.computeActiveChargeLimitW(batteryData),
1922
1947
  activeDischargeLimitW: this.computeActiveDischargeLimitW(batteryData),
1923
1948
  features,
1924
- gridChargingEnabled: batteryData?.chaGriSet === 1,
1949
+ gridChargingEnabled: this.gridChargingEnabled(features),
1925
1950
  ...this.configuredBatteryMetadata()
1926
1951
  }
1927
1952
  });
@@ -2105,7 +2130,7 @@ export class SunspecBattery extends BaseSunspecDevice {
2105
2130
  }
2106
2131
  if (batteryData.inWRte >= 100)
2107
2132
  return null;
2108
- return (batteryData.wChaMax * batteryData.inWRte) / 100;
2133
+ return (batteryData.wChaMax * Math.max(0, batteryData.inWRte)) / 100;
2109
2134
  }
2110
2135
  /**
2111
2136
  * Compute the currently active discharge limit in Watts from Model 124's
@@ -2118,7 +2143,8 @@ export class SunspecBattery extends BaseSunspecDevice {
2118
2143
  }
2119
2144
  if (batteryData.outWRte >= 100)
2120
2145
  return null;
2121
- return (batteryData.wChaMax * batteryData.outWRte) / 100;
2146
+ // A negative rate is a forced charge, not a negative limit: nothing may be discharged.
2147
+ return (batteryData.wChaMax * Math.max(0, batteryData.outWRte)) / 100;
2122
2148
  }
2123
2149
  determineStorageMode(controls) {
2124
2150
  return deriveStorageMode(controls);
@@ -2160,6 +2186,7 @@ export class SunspecBattery extends BaseSunspecDevice {
2160
2186
  sunspecClient: this.sunspecClient,
2161
2187
  unitId: this.unitId,
2162
2188
  getSnapshotService: () => this.snapshotService,
2189
+ gridChargeMethod: this.gridChargeMethod,
2163
2190
  onErrorCallback: (err) => console.warn(`Battery ${this.applianceId}: schedule handler rejected input: ${err.message}`),
2164
2191
  });
2165
2192
  console.log(`Battery ${this.applianceId}: started data bus listening (listener ${this.dataBusListenerId})`);
@@ -977,6 +977,24 @@ export declare enum SunspecBatteryCapability {
977
977
  DischargeLimit = "discharge-limit",
978
978
  ChargeLimit = "charge-limit"
979
979
  }
980
+ /**
981
+ * How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
982
+ * differently, so the host picks the form its hardware actually acts on.
983
+ */
984
+ export declare enum SunspecGridChargeMethod {
985
+ /**
986
+ * StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
987
+ * InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
988
+ */
989
+ ChargeRateLimit = "charge-rate-limit",
990
+ /**
991
+ * StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
992
+ * below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
993
+ * by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
994
+ * Home Assistant fronius_modbus integration's "charge from grid").
995
+ */
996
+ NegativeDischargeRate = "negative-discharge-rate"
997
+ }
980
998
  /**
981
999
  * Battery facts that live outside the SunSpec register map, published onto `appliance.battery` when
982
1000
  * the device connects. Fields left `undefined` are not written, so an existing value in the
@@ -996,6 +1014,11 @@ export interface SunspecBatteryMetadataConfig {
996
1014
  */
997
1015
  roundTripEfficiencyPercent?: number;
998
1016
  }
1017
+ /** Per-battery configuration: the published metadata plus how the device is driven. */
1018
+ export interface SunspecBatteryConfig extends SunspecBatteryMetadataConfig {
1019
+ /** Register form for a grid charge. Defaults to {@link SunspecGridChargeMethod.ChargeRateLimit}. */
1020
+ gridChargeMethod?: SunspecGridChargeMethod;
1021
+ }
999
1022
  /**
1000
1023
  * Selects how `SunspecBattery` populates `appliance.battery.features`.
1001
1024
  *
@@ -345,6 +345,25 @@ export var SunspecBatteryCapability;
345
345
  SunspecBatteryCapability["DischargeLimit"] = "discharge-limit";
346
346
  SunspecBatteryCapability["ChargeLimit"] = "charge-limit";
347
347
  })(SunspecBatteryCapability || (SunspecBatteryCapability = {}));
348
+ /**
349
+ * How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
350
+ * differently, so the host picks the form its hardware actually acts on.
351
+ */
352
+ export var SunspecGridChargeMethod;
353
+ (function (SunspecGridChargeMethod) {
354
+ /**
355
+ * StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
356
+ * InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
357
+ */
358
+ SunspecGridChargeMethod["ChargeRateLimit"] = "charge-rate-limit";
359
+ /**
360
+ * StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
361
+ * below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
362
+ * by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
363
+ * Home Assistant fronius_modbus integration's "charge from grid").
364
+ */
365
+ SunspecGridChargeMethod["NegativeDischargeRate"] = "negative-discharge-rate";
366
+ })(SunspecGridChargeMethod || (SunspecGridChargeMethod = {}));
348
367
  /**
349
368
  * Selects how `SunspecBattery` populates `appliance.battery.features`.
350
369
  *
package/dist/version.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "1.6.0";
8
+ export declare const SDK_VERSION = "1.7.0";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/dist/version.js CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export const SDK_VERSION = '1.6.0';
8
+ export const SDK_VERSION = '1.7.0';
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enyo-energy/sunspec-sdk",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@enyo-energy/appliance-calibration": "0.0.3",
44
- "@enyo-energy/energy-app-sdk": "1.33.0"
44
+ "@enyo-energy/energy-app-sdk": "1.40.0"
45
45
  },
46
46
  "volta": {
47
47
  "node": "22.17.0"