homebridge-nuheat2 1.2.28 → 1.4.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/CHANGELOG.md CHANGED
@@ -4,6 +4,30 @@ All notable changes to this project should be documented in this file
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.4.0] - 2026-10-17
8
+
9
+ ### Fixed
10
+
11
+ - Report a requested `Off` back to HomeKit as `Off`: standby is now derived from a manual or hold mode parked at the minimum target temperature, so the Home app no longer bounces the tile back to `Heat` after turning a thermostat off
12
+ - Stop treating an auto schedule combined with a manual operating mode as `Off`; an auto schedule is always a scheduled run and is reported as `Auto`
13
+ - Fail gracefully when a Nuheat API or OAuth token response is not valid JSON instead of throwing an unhandled parse error
14
+ - Acknowledge failed away-mode switch writes and re-read the group so the switch reconciles with the real state instead of wedging the Home app
15
+
16
+ ## [1.3.0] - 2026-10-15
17
+
18
+ ### Added
19
+
20
+ - Opt-in `enableThermostatModeControl` setting that adds `Off` and `Auto` target states to each thermostat and routes those requests through Nuheat's v2 mode API (`PUT /api/v2/Mode/Manual`, `Mode/Hold`, `Mode/Auto`). Defaults to `false`, so existing installations keep advertising heat only
21
+
22
+ ### Changed
23
+
24
+ - Derive the HomeKit target state from Nuheat's `operatingMode`/`scheduleMode` fields, replacing a switch that returned `Heat` in every branch
25
+
26
+ ### Fixed
27
+
28
+ - Stop pushing the empty `204 No Content` response from setpoint writes into HomeKit, which briefly flashed `0.2 C` / `10 C` readings and a wrong target state after every change; write paths now re-read the thermostat and non-object payloads are ignored
29
+ - Stop sending a stale cached setpoint with heat-mode requests, which could silently clobber a concurrent setpoint write from the Home app; heat requests now omit the optional temperature field
30
+
7
31
  ## [1.2.28] - 2026-09-14
8
32
 
9
33
  ### Fixed
package/README.md CHANGED
@@ -64,6 +64,7 @@ The equivalent JSON looks like this:
64
64
  "groups": [{ "groupName": "Main Floor", "disabled": false }],
65
65
  "exposeScheduleSwitches": false,
66
66
  "enableNotifications": true,
67
+ "enableThermostatModeControl": false,
67
68
  "holdLength": 1440,
68
69
  "refresh": 60
69
70
  }
@@ -86,6 +87,7 @@ The equivalent JSON looks like this:
86
87
  - `holdLength`: Hold duration in minutes, default `1440`. Values are clamped from `0` to `1440`
87
88
  - `refresh`: Poll interval in seconds, default `60`. Values lower than `30` are raised to `30` to reduce API traffic
88
89
  - `enableNotifications`: Enables Nuheat SignalR notifications for faster updates. Defaults to `true`; set to `false` only while troubleshooting
90
+ - `enableThermostatModeControl`: Adds `Off` and `Auto` target states to each thermostat so HomeKit can request standby or resume the schedule. Defaults to `false`. Experimental; see [Thermostat Mode Control](#thermostat-mode-control)
89
91
  - `debug`: Enables verbose Nuheat API, notification, and accessory logging. Defaults to `false`
90
92
 
91
93
  ### Hold Length Behavior
@@ -102,6 +104,22 @@ What the thermostats do while a group is in away mode is decided by Nuheat's ser
102
104
 
103
105
  If you need a specific temperature held while you are away, set that temperature on the thermostat itself. The plugin will hold it for `holdLength` minutes, or permanently when `holdLength` is `1440`.
104
106
 
107
+ ### Thermostat Mode Control
108
+
109
+ By default each thermostat advertises only `Heat` as a target state, because Nuheat's v1 thermostat update API has no operating-mode field and therefore cannot request standby.
110
+
111
+ Setting `enableThermostatModeControl` to `true` adds `Off` and `Auto` to the target states and routes those requests through Nuheat's v2 mode API:
112
+
113
+ | HomeKit target | Nuheat v2 request |
114
+ | --- | --- |
115
+ | `Off` | `PUT /api/v2/Mode/Manual` at the minimum target temperature (10 °C / 50 °F) |
116
+ | `Heat` | `PUT /api/v2/Mode/Hold` at the current target temperature, or `Mode/Manual` when `holdLength` is `1440` |
117
+ | `Auto` | `PUT /api/v2/Mode/Auto`, resuming the schedule |
118
+
119
+ Reads still come from the v1 API, which is the only version that reports `groupId` (needed for away mode) and `operatingMode`/`scheduleMode`. The plugin derives the HomeKit target state from those fields: a manual or hold mode parked at the minimum target temperature is reported as `Off` (Nuheat's Standby — exactly how this plugin requests it), a warmer manual or hold setpoint is reported as `Heat`, and an auto schedule is reported as `Auto` regardless of the thermostat's operating mode.
120
+
121
+ Nuheat has no explicit "off" concept, so `Off` is implemented as a permanent manual setpoint at the minimum temperature, and reads recognize that state by the parked setpoint. Mode writes through the v2 mode API are reflected in subsequent v1 reads, so a requested `Off` reads back as `Off`.
122
+
105
123
  ### Device Discovery
106
124
 
107
125
  If `devices` is omitted or empty, the plugin will automatically expose every thermostat on the authenticated account.
@@ -95,6 +95,12 @@
95
95
  "default": false,
96
96
  "description": "Create one switch per thermostat. The switch is on when the thermostat follows its Nuheat schedule and can be turned on to resume the schedule."
97
97
  },
98
+ "enableThermostatModeControl": {
99
+ "title": "Enable Thermostat Mode Control (Off/Auto)",
100
+ "type": "boolean",
101
+ "default": false,
102
+ "description": "Adds Off and Auto target states to each thermostat using Nuheat's v2 mode API, so HomeKit can request standby or resume the schedule. Experimental: leave disabled unless you want HomeKit to drive thermostat mode."
103
+ },
98
104
  "holdLength": {
99
105
  "title": "Hold Length (minutes)",
100
106
  "type": "integer",
@@ -152,6 +158,7 @@
152
158
  "holdLength",
153
159
  "refresh",
154
160
  "enableNotifications",
161
+ "enableThermostatModeControl",
155
162
  "debug"
156
163
  ]
157
164
  }
@@ -146,6 +146,19 @@
146
146
  </span>
147
147
  </label>
148
148
 
149
+ <label class="checkbox">
150
+ <input id="enable-thermostat-mode-control" type="checkbox" />
151
+ <span>
152
+ Enable thermostat mode control (Off / Auto)
153
+ <small>
154
+ Adds Off and Auto target states to each thermostat using Nuheat's v2
155
+ mode API, so HomeKit can request standby or resume the schedule.
156
+ Experimental; leave off unless you want HomeKit to drive thermostat
157
+ mode.
158
+ </small>
159
+ </span>
160
+ </label>
161
+
149
162
  <label class="checkbox">
150
163
  <input id="debug" type="checkbox" />
151
164
  <span>
@@ -12,6 +12,9 @@ const elements = {
12
12
  holdLength: document.getElementById("hold-length"),
13
13
  refresh: document.getElementById("refresh"),
14
14
  enableNotifications: document.getElementById("enable-notifications"),
15
+ enableThermostatModeControl: document.getElementById(
16
+ "enable-thermostat-mode-control",
17
+ ),
15
18
  debug: document.getElementById("debug"),
16
19
  saveAccount: document.getElementById("save-account"),
17
20
  addDevice: document.getElementById("add-device"),
@@ -102,6 +105,7 @@ function createDefaultConfig() {
102
105
  autoPopulateAwayModeSwitches: false,
103
106
  exposeScheduleSwitches: false,
104
107
  enableNotifications: true,
108
+ enableThermostatModeControl: false,
105
109
  holdLength: 1440,
106
110
  refresh: 60,
107
111
  debug: false,
@@ -126,6 +130,9 @@ function renderConfig(config) {
126
130
  elements.holdLength.value = String(normalizeHoldLength(config.holdLength));
127
131
  elements.refresh.value = String(normalizeRefresh(config.refresh));
128
132
  elements.enableNotifications.checked = config.enableNotifications !== false;
133
+ elements.enableThermostatModeControl.checked = Boolean(
134
+ config.enableThermostatModeControl,
135
+ );
129
136
  elements.debug.checked = Boolean(config.debug);
130
137
 
131
138
  updateStatuses();
@@ -313,6 +320,9 @@ async function saveBehavior() {
313
320
  holdLength,
314
321
  refresh,
315
322
  enableNotifications: Boolean(elements.enableNotifications.checked),
323
+ enableThermostatModeControl: Boolean(
324
+ elements.enableThermostatModeControl.checked,
325
+ ),
316
326
  debug: Boolean(elements.debug.checked),
317
327
  });
318
328
  showToast("success", "Behavior settings saved.");
@@ -438,6 +448,10 @@ function renderDiagnostics() {
438
448
  "Notifications",
439
449
  config.enableNotifications === false ? "disabled" : "enabled",
440
450
  ],
451
+ [
452
+ "Mode Control",
453
+ config.enableThermostatModeControl ? "Off / Auto enabled" : "heat only",
454
+ ],
441
455
  ["Debug Logs", config.debug ? "enabled" : "disabled"],
442
456
  ]);
443
457
  }
@@ -475,6 +489,9 @@ function getDraftConfig() {
475
489
  holdLength: normalizeHoldLength(elements.holdLength.value),
476
490
  refresh: normalizeRefresh(elements.refresh.value),
477
491
  enableNotifications: Boolean(elements.enableNotifications.checked),
492
+ enableThermostatModeControl: Boolean(
493
+ elements.enableThermostatModeControl.checked,
494
+ ),
478
495
  debug: Boolean(elements.debug.checked),
479
496
  };
480
497
  }
@@ -568,6 +585,7 @@ function bindEvents() {
568
585
  elements.holdLength,
569
586
  elements.refresh,
570
587
  elements.enableNotifications,
588
+ elements.enableThermostatModeControl,
571
589
  elements.debug,
572
590
  ].forEach((element) => {
573
591
  element.addEventListener("input", () => {
package/index.js CHANGED
@@ -192,7 +192,7 @@ class NuHeatPlatform {
192
192
  entry = { uuid };
193
193
  this.accessories.push(entry);
194
194
  }
195
- entry.accessory = new NuHeatThermostat(this.log, deviceData, this.config.holdLength || 1440, getPlatformAccessory(deviceAccessory), this.NuHeatAPI, Homebridge, this.account?.temperatureScale);
195
+ entry.accessory = new NuHeatThermostat(this.log, deviceData, this.config.holdLength || 1440, getPlatformAccessory(deviceAccessory), this.NuHeatAPI, Homebridge, this.account?.temperatureScale, this.config.enableThermostatModeControl === true);
196
196
  entry.existsInConfig = true;
197
197
  this.log.info("Loaded thermostat " +
198
198
  deviceData.serialNumber +
package/lib/NuHeatAPI.js CHANGED
@@ -163,6 +163,42 @@ class NuHeatAPI {
163
163
  scheduleMode: NuHeatModels_1.SCHEDULE_MODE.AUTO,
164
164
  });
165
165
  }
166
+ /**
167
+ * The v1 thermostat update model has no operating mode, so it cannot request
168
+ * standby. The v2 mode endpoints can, and they share the same account,
169
+ * identity provider, and `openapi` scope as v1.
170
+ */
171
+ async setThermostatMode(serialNumber, mode, options = {}) {
172
+ let callURL;
173
+ const callBody = { serialNumber };
174
+ if (mode === NuHeatModels_1.HEATING_MODE.AUTO) {
175
+ callURL = (0, settings_1.buildNuHeatApiUrl)("/api/v2/Mode/Auto");
176
+ }
177
+ else if (mode === NuHeatModels_1.HEATING_MODE.HOLD) {
178
+ callURL = (0, settings_1.buildNuHeatApiUrl)("/api/v2/Mode/Hold");
179
+ if (options.holdLength && options.holdLength > 0) {
180
+ callBody.holdUntil = new Date(Date.now() + options.holdLength * 60 * 1000).toISOString();
181
+ }
182
+ }
183
+ else {
184
+ // NuHeat has no off; standby is a permanent manual setpoint.
185
+ callURL = (0, settings_1.buildNuHeatApiUrl)("/api/v2/Mode/Manual");
186
+ }
187
+ if (options.setPointTemp !== undefined) {
188
+ callBody.temperature = Number(options.setPointTemp);
189
+ callBody.temperatureType = NuHeatModels_1.TEMPERATURE_TYPE.ABSOLUTE;
190
+ }
191
+ this.log.debug("NuHeatAPI: Setting thermostat " +
192
+ serialNumber +
193
+ " to " +
194
+ mode +
195
+ " mode.");
196
+ const callOptions = {
197
+ body: JSON.stringify(callBody),
198
+ method: "PUT",
199
+ };
200
+ return await this.makeAPICall(callURL, callOptions);
201
+ }
166
202
  async updateThermostat(thermostatUpdate) {
167
203
  const callURL = (0, settings_1.buildNuHeatApiUrl)("/api/v1/Thermostat");
168
204
  const callOptions = {
@@ -254,7 +290,17 @@ class NuHeatAPI {
254
290
  if (response.status === 204) {
255
291
  return true;
256
292
  }
257
- const returnedData = await response.json();
293
+ let returnedData;
294
+ try {
295
+ returnedData = await response.json();
296
+ }
297
+ catch (error) {
298
+ this.log.warn("NuHeatAPI: " +
299
+ this.describeRequest(callURL, callOptions) +
300
+ " returned a response that could not be parsed as JSON: " +
301
+ error);
302
+ return false;
303
+ }
258
304
  if (options.normalize) {
259
305
  if (options.normalizeArray && Array.isArray(returnedData)) {
260
306
  return returnedData.map(options.normalize);
@@ -589,7 +635,15 @@ class NuHeatAPI {
589
635
  return null;
590
636
  }
591
637
  this.tokenScope = redirectUrl.searchParams.get("scope") ?? "";
592
- const token = (await response.json());
638
+ let token;
639
+ try {
640
+ token = (await response.json());
641
+ }
642
+ catch (error) {
643
+ this.log.warn("NuHeatAPI: The OAuth token response could not be parsed as JSON: " +
644
+ error);
645
+ return null;
646
+ }
593
647
  this.log.debug("NuHeatAPI: OAuth access token received. Granted scopes: " +
594
648
  (token.scope || this.tokenScope || "unknown") +
595
649
  ". Refresh token present: " +
@@ -610,7 +664,15 @@ class NuHeatAPI {
610
664
  if (!response) {
611
665
  return false;
612
666
  }
613
- const token = (await response.json());
667
+ let token;
668
+ try {
669
+ token = (await response.json());
670
+ }
671
+ catch (error) {
672
+ this.log.warn("NuHeatAPI: The refreshed OAuth token response could not be parsed as JSON: " +
673
+ error);
674
+ return false;
675
+ }
614
676
  const previousRefreshToken = this.refreshToken;
615
677
  this.accessToken = token.access_token;
616
678
  this.accessTokenTimestamp = Date.now();
@@ -31,7 +31,10 @@ class NuHeatGroup {
31
31
  const response = await this.NuHeatAPI.setAwayMode(this.deviceData.groupId ?? "", value);
32
32
  if (!response) {
33
33
  this.log.error("Error setting away mode", this.deviceData.groupName);
34
- callback(new Error("Error: setAwayMode"));
34
+ // Acknowledge the write so HomeKit does not wedge the switch, then
35
+ // re-read the group to reconcile the tile with the real state.
36
+ callback(null);
37
+ await this.updateAccessory();
35
38
  }
36
39
  else {
37
40
  this.updateValues({ ...this.deviceData, awayMode: value });
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OPERATING_MODE = exports.SCHEDULE_MODE = void 0;
3
+ exports.STANDBY_SETPOINT_C = exports.TEMPERATURE_TYPE = exports.HEATING_MODE = exports.OPERATING_MODE = exports.SCHEDULE_MODE = void 0;
4
+ exports.isThermostatOnline = isThermostatOnline;
5
+ exports.fromNuHeatTemperature = fromNuHeatTemperature;
6
+ exports.isStandbySetpoint = isStandbySetpoint;
7
+ exports.deriveHeatingMode = deriveHeatingMode;
4
8
  exports.normalizeAccount = normalizeAccount;
5
9
  exports.normalizeGroup = normalizeGroup;
6
10
  exports.normalizeSchedule = normalizeSchedule;
@@ -15,9 +19,80 @@ exports.OPERATING_MODE = {
15
19
  AUTO: 1,
16
20
  MANUAL: 2,
17
21
  };
22
+ exports.HEATING_MODE = {
23
+ OFF: "off",
24
+ MANUAL: "manual",
25
+ HOLD: "hold",
26
+ AUTO: "auto",
27
+ };
28
+ /** v2 `eTemperatureType`: 0 = absolute setpoint, 1 = relative to current. */
29
+ exports.TEMPERATURE_TYPE = {
30
+ ABSOLUTE: 0,
31
+ RELATIVE: 1,
32
+ };
18
33
  function coalesce(...values) {
19
34
  return values.find((value) => value !== undefined && value !== null);
20
35
  }
36
+ function isThermostatOnline(thermostat) {
37
+ const onlineValue = coalesce(thermostat.Online, thermostat.online, thermostat.isOnline);
38
+ if (typeof onlineValue === "boolean") {
39
+ return onlineValue;
40
+ }
41
+ if (typeof onlineValue === "string") {
42
+ return onlineValue.replace(/'/g, "").trim().toLowerCase() === "true";
43
+ }
44
+ return true;
45
+ }
46
+ /** The minimum NuHeat target temperature, which the plugin uses for standby. */
47
+ exports.STANDBY_SETPOINT_C = 10;
48
+ /** Converts a raw NuHeat temperature (e.g. `setPointTemp`) to Celsius. */
49
+ function fromNuHeatTemperature(raw) {
50
+ return (((raw - 33) / 56 + 33 - 32) * 5) / 9;
51
+ }
52
+ /**
53
+ * Standby is a manual or hold mode parked at the minimum target temperature,
54
+ * which is how this plugin requests it. Away mode reads back even lower, so
55
+ * anything at or below the standby setpoint counts.
56
+ */
57
+ function isStandbySetpoint(setPointTemp) {
58
+ if (setPointTemp === undefined || setPointTemp === null) {
59
+ return false;
60
+ }
61
+ const celsius = fromNuHeatTemperature(Number(setPointTemp));
62
+ return Number.isFinite(celsius) && celsius <= exports.STANDBY_SETPOINT_C;
63
+ }
64
+ /**
65
+ * NuHeat has no "off". Standby is a manual or hold mode parked at the minimum
66
+ * target temperature; a genuine manual setpoint change always reads back
67
+ * warmer, and an auto schedule is a scheduled run regardless of the
68
+ * thermostat's operating mode.
69
+ */
70
+ function deriveHeatingMode(thermostat) {
71
+ if (!isThermostatOnline(thermostat)) {
72
+ return exports.HEATING_MODE.OFF;
73
+ }
74
+ const scheduleMode = Number(thermostat.scheduleMode);
75
+ if (scheduleMode === exports.SCHEDULE_MODE.HOLD) {
76
+ return isStandbySetpoint(thermostat.setPointTemp)
77
+ ? exports.HEATING_MODE.OFF
78
+ : exports.HEATING_MODE.HOLD;
79
+ }
80
+ if (scheduleMode === exports.SCHEDULE_MODE.PERMANENT_HOLD) {
81
+ return isStandbySetpoint(thermostat.setPointTemp)
82
+ ? exports.HEATING_MODE.OFF
83
+ : exports.HEATING_MODE.MANUAL;
84
+ }
85
+ if (scheduleMode === exports.SCHEDULE_MODE.AUTO) {
86
+ return exports.HEATING_MODE.AUTO;
87
+ }
88
+ // Unknown schedule mode: report whatever the hardware tells us.
89
+ if (Number(thermostat.operatingMode) === exports.OPERATING_MODE.MANUAL) {
90
+ return isStandbySetpoint(thermostat.setPointTemp)
91
+ ? exports.HEATING_MODE.OFF
92
+ : exports.HEATING_MODE.MANUAL;
93
+ }
94
+ return exports.HEATING_MODE.AUTO;
95
+ }
21
96
  function normalizeAccount(account = {}) {
22
97
  return {
23
98
  firstName: coalesce(account.firstName, account.FirstName),
@@ -2,7 +2,6 @@
2
2
  const NuHeatModels_1 = require("./NuHeatModels");
3
3
  let Characteristic;
4
4
  let ThermostatService;
5
- const MIN_TARGET_TEMPERATURE_C = 10;
6
5
  const MAX_TARGET_TEMPERATURE_C = 38;
7
6
  class NuHeatThermostat {
8
7
  log;
@@ -11,7 +10,8 @@ class NuHeatThermostat {
11
10
  accessory;
12
11
  NuHeatAPI;
13
12
  temperatureScale;
14
- constructor(log, deviceData, holdLength, accessory, NuHeatAPI, homebridge, temperatureScale) {
13
+ enableThermostatModeControl;
14
+ constructor(log, deviceData, holdLength, accessory, NuHeatAPI, homebridge, temperatureScale, enableThermostatModeControl = false) {
15
15
  Characteristic = homebridge.hap.Characteristic;
16
16
  ThermostatService = homebridge.hap.Service.Thermostat;
17
17
  this.log = log;
@@ -20,6 +20,7 @@ class NuHeatThermostat {
20
20
  this.accessory = accessory;
21
21
  this.NuHeatAPI = NuHeatAPI;
22
22
  this.temperatureScale = temperatureScale;
23
+ this.enableThermostatModeControl = enableThermostatModeControl;
23
24
  this.accessory
24
25
  .getService(homebridge.hap.Service.AccessoryInformation)
25
26
  .setCharacteristic(Characteristic.Manufacturer, "NuHeat")
@@ -31,10 +32,17 @@ class NuHeatThermostat {
31
32
  setupListeners() {
32
33
  const thermostatService = this.accessory.getService(ThermostatService);
33
34
  this.log.info("holdLength: " + this.holdLength, this.deviceData.name);
35
+ const targetHeatingCoolingStates = this.enableThermostatModeControl
36
+ ? [
37
+ Characteristic.TargetHeatingCoolingState.OFF,
38
+ Characteristic.TargetHeatingCoolingState.HEAT,
39
+ Characteristic.TargetHeatingCoolingState.AUTO,
40
+ ]
41
+ : [Characteristic.TargetHeatingCoolingState.HEAT];
34
42
  thermostatService
35
43
  .getCharacteristic(Characteristic.TargetHeatingCoolingState)
36
44
  .setProps({
37
- validValues: [Characteristic.TargetHeatingCoolingState.HEAT],
45
+ validValues: targetHeatingCoolingStates,
38
46
  })
39
47
  .on("set", this.setTargetHeatingCooling.bind(this));
40
48
  thermostatService.getCharacteristic(Characteristic.CurrentTemperature).setProps({
@@ -50,25 +58,79 @@ class NuHeatThermostat {
50
58
  this.updateTemperatureDisplayUnits();
51
59
  }
52
60
  async setTargetHeatingCooling(value, callback) {
61
+ if (this.enableThermostatModeControl) {
62
+ await this.setThermostatModeFromHomeKit(value, callback);
63
+ return;
64
+ }
53
65
  if (value !== Characteristic.TargetHeatingCoolingState.OFF) {
54
66
  callback(null);
55
67
  void this.updateAccessory();
56
68
  return;
57
69
  }
58
70
  this.log.info("NuHeat does not support an off mode. Setting the thermostat to its minimum target temperature instead.", this.deviceData.name);
59
- const response = await this.NuHeatAPI.setHeatSetpoint(this.deviceData.serialNumber ?? "", this.toNuHeatTemperature(MIN_TARGET_TEMPERATURE_C), this.holdLength);
71
+ const response = await this.NuHeatAPI.setHeatSetpoint(this.deviceData.serialNumber ?? "", this.toNuHeatTemperature(NuHeatModels_1.STANDBY_SETPOINT_C), this.holdLength);
60
72
  if (!response) {
61
73
  this.log.error("Error setting minimum target temperature for off request", this.deviceData.name);
62
74
  callback(new Error("Error: setTargetHeatingCooling"));
63
75
  return;
64
76
  }
65
- this.updateValues(response);
77
+ // The v1 write returns 204 with no content, so re-read the authoritative
78
+ // state instead of pushing the empty response into HomeKit.
79
+ await this.updateAccessory();
80
+ callback(null);
81
+ }
82
+ /**
83
+ * The v1 update model cannot request standby or an indefinite auto schedule,
84
+ * so mode changes go through the v2 mode endpoints. Those writes return no
85
+ * content, so the authoritative state is re-read through v1 afterwards.
86
+ */
87
+ async setThermostatModeFromHomeKit(value, callback) {
88
+ const { OFF, HEAT, AUTO } = Characteristic.TargetHeatingCoolingState;
89
+ let mode;
90
+ const options = {};
91
+ if (value === OFF) {
92
+ mode = NuHeatModels_1.HEATING_MODE.OFF;
93
+ options.setPointTemp = this.toNuHeatTemperature(NuHeatModels_1.STANDBY_SETPOINT_C);
94
+ this.log.info("Setting the thermostat to standby (minimum target temperature).", this.deviceData.name);
95
+ }
96
+ else if (value === AUTO) {
97
+ mode = NuHeatModels_1.HEATING_MODE.AUTO;
98
+ this.log.info("Resuming the NuHeat schedule.", this.deviceData.name);
99
+ }
100
+ else if (value === HEAT) {
101
+ // Deliberately omit the setpoint: the v2 mode body's temperature field
102
+ // is optional, and HomeKit sends mode and temperature as separate
103
+ // concurrent writes. Sending a stale setpoint here can clobber the
104
+ // setpoint write that lands right after this one.
105
+ if (this.holdLength >= 1440) {
106
+ mode = NuHeatModels_1.HEATING_MODE.MANUAL;
107
+ }
108
+ else {
109
+ mode = NuHeatModels_1.HEATING_MODE.HOLD;
110
+ if (this.holdLength > 0) {
111
+ options.holdLength = this.holdLength;
112
+ }
113
+ }
114
+ this.log.info("Holding the thermostat target temperature.", this.deviceData.name);
115
+ }
116
+ else {
117
+ this.log.error("Unsupported target heating state " + value, this.deviceData.name);
118
+ callback(new Error("Error: setTargetHeatingCooling"));
119
+ return;
120
+ }
121
+ const response = await this.NuHeatAPI.setThermostatMode(this.deviceData.serialNumber ?? "", mode, options);
122
+ if (!response) {
123
+ this.log.error("Error setting thermostat mode", this.deviceData.name);
124
+ callback(new Error("Error: setTargetHeatingCooling"));
125
+ return;
126
+ }
127
+ await this.updateAccessory();
66
128
  callback(null);
67
129
  }
68
130
  async setTargetTemperature(value, callback) {
69
131
  this.log.info("Setting target temperature to " + value + " C", this.deviceData.name);
70
- if (value < MIN_TARGET_TEMPERATURE_C)
71
- value = MIN_TARGET_TEMPERATURE_C;
132
+ if (value < NuHeatModels_1.STANDBY_SETPOINT_C)
133
+ value = NuHeatModels_1.STANDBY_SETPOINT_C;
72
134
  if (value > MAX_TARGET_TEMPERATURE_C)
73
135
  value = MAX_TARGET_TEMPERATURE_C;
74
136
  const heatSetPoint = this.toNuHeatTemperature(value);
@@ -79,7 +141,9 @@ class NuHeatThermostat {
79
141
  callback(new Error("Error: setTargetTemperature"));
80
142
  }
81
143
  else {
82
- this.updateValues(response);
144
+ // The v1 write returns 204 with no content, so re-read the authoritative
145
+ // state instead of pushing the empty response into HomeKit.
146
+ await this.updateAccessory();
83
147
  callback(null);
84
148
  }
85
149
  }
@@ -93,6 +157,13 @@ class NuHeatThermostat {
93
157
  }
94
158
  }
95
159
  updateValues(newValues) {
160
+ // Write calls that return 204 No Content resolve to a literal `true`.
161
+ // That is not thermostat data; pushing it would flash garbage temperatures
162
+ // and blank the cached device data until the next poll.
163
+ if (!newValues || typeof newValues !== "object") {
164
+ this.log.debug("Ignoring a thermostat update without payload data", this.deviceData.name);
165
+ return;
166
+ }
96
167
  if (this.isOnline(newValues)) {
97
168
  this.updateTemperatureDisplayUnits();
98
169
  let currentTemperature = Number(this.toHBTemperature(newValues.currentTemperature ?? 0));
@@ -102,8 +173,8 @@ class NuHeatThermostat {
102
173
  .getCharacteristic(Characteristic.CurrentTemperature)
103
174
  .updateValue(currentTemperature);
104
175
  let setPointTemperature = Number(this.toHBTemperature(newValues.setPointTemp ?? 0));
105
- if (setPointTemperature < MIN_TARGET_TEMPERATURE_C) {
106
- setPointTemperature = MIN_TARGET_TEMPERATURE_C;
176
+ if (setPointTemperature < NuHeatModels_1.STANDBY_SETPOINT_C) {
177
+ setPointTemperature = NuHeatModels_1.STANDBY_SETPOINT_C;
107
178
  }
108
179
  if (setPointTemperature > MAX_TARGET_TEMPERATURE_C) {
109
180
  setPointTemperature = MAX_TARGET_TEMPERATURE_C;
@@ -139,16 +210,25 @@ class NuHeatThermostat {
139
210
  return (((temperature * 9) / 5 + 32 - 33) * 56 + 33).toFixed(0);
140
211
  }
141
212
  toHBTemperature(temperature) {
142
- return ((((temperature - 33) / 56 + 33 - 32) * 5) / 9).toFixed(1);
213
+ return (0, NuHeatModels_1.fromNuHeatTemperature)(temperature).toFixed(1);
143
214
  }
144
215
  toHomeBridgeHeatingCoolingState(newValues) {
145
- if (!this.isOnline(newValues)) {
146
- return Characteristic.TargetHeatingCoolingState.OFF;
216
+ if (!this.enableThermostatModeControl) {
217
+ if (!this.isOnline(newValues)) {
218
+ return Characteristic.TargetHeatingCoolingState.OFF;
219
+ }
220
+ // The v1 read model cannot distinguish standby from a scheduled run, and
221
+ // the characteristic only advertises heat, so heat is the only safe
222
+ // report.
223
+ return Characteristic.TargetHeatingCoolingState.HEAT;
147
224
  }
148
- switch (newValues.operatingMode) {
149
- case NuHeatModels_1.OPERATING_MODE.AUTO:
150
- case NuHeatModels_1.OPERATING_MODE.MANUAL:
151
- return Characteristic.TargetHeatingCoolingState.HEAT;
225
+ switch ((0, NuHeatModels_1.deriveHeatingMode)(newValues)) {
226
+ case NuHeatModels_1.HEATING_MODE.OFF:
227
+ return Characteristic.TargetHeatingCoolingState.OFF;
228
+ case NuHeatModels_1.HEATING_MODE.AUTO:
229
+ return Characteristic.TargetHeatingCoolingState.AUTO;
230
+ case NuHeatModels_1.HEATING_MODE.MANUAL:
231
+ case NuHeatModels_1.HEATING_MODE.HOLD:
152
232
  default:
153
233
  return Characteristic.TargetHeatingCoolingState.HEAT;
154
234
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-nuheat2",
3
- "version": "1.2.28",
3
+ "version": "1.4.0",
4
4
  "description": "Homebridge Platform for NuHeat Signature Thermostats",
5
5
  "main": "index.js",
6
6
  "scripts": {