homebridge-bomgovau-v2 0.2.5 → 0.2.6

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/index.js CHANGED
@@ -66,7 +66,7 @@ class BOMgovauAccessory {
66
66
  minStep: 1,
67
67
  perms: [Perms.PAIRED_READ, Perms.NOTIFY]
68
68
  });
69
- this.value = this.getDefaultValue();
69
+ this.value = 1013;
70
70
  }
71
71
  }
72
72
 
@@ -179,6 +179,24 @@ class BOMgovauAccessory {
179
179
  : undefined;
180
180
  }
181
181
 
182
+ /**
183
+ * Returns the last valid HomeKit value, or a valid initial fallback.
184
+ *
185
+ * @param {Service} service - Sensor service
186
+ * @param {Characteristic} characteristic - Sensor characteristic
187
+ * @param {number} min - Inclusive minimum
188
+ * @param {number} max - Inclusive maximum
189
+ * @param {number} fallback - Valid value to use before a reading is available
190
+ * @returns {number}
191
+ * @private
192
+ */
193
+ _getCharacteristicFallback(service, characteristic, min, max, fallback) {
194
+ const value = service.getCharacteristic(characteristic).value;
195
+ return typeof value === "number" && Number.isFinite(value) && value >= min && value <= max
196
+ ? value
197
+ : fallback;
198
+ }
199
+
182
200
  /**
183
201
  * Sets up HomeKit services based on enabled sensors.
184
202
  *
@@ -353,12 +371,15 @@ class BOMgovauAccessory {
353
371
  const now = Date.now();
354
372
 
355
373
  if (now < this.BOMdataExpires) {
356
- this.log("[BOM gov au] BOM data will expire at " + new Date(this.BOMdataExpires).toLocaleString() + " which is in " +
374
+ this.log.debug("[BOM gov au] BOM data will expire at " + new Date(this.BOMdataExpires).toLocaleString() + " which is in " +
357
375
  ((this.BOMdataExpires - now) / 1000 / 60).toFixed(1) + "min");
358
376
  return;
359
377
  }
360
378
 
361
- this.log("[BOM gov au] Current Time is " + now + ", BOM data expires at " + this.BOMdataExpires);
379
+ this.log("[BOM gov au] Current Time is " + new Date(now).toLocaleString() +
380
+ ", BOM data expires at " + (this.BOMdataExpires > 0
381
+ ? new Date(this.BOMdataExpires).toLocaleString()
382
+ : "not set"));
362
383
  this.log("[BOM gov au] Updating observations from BOM.");
363
384
 
364
385
  try {
@@ -368,13 +389,31 @@ class BOMgovauAccessory {
368
389
  this.log("[BOM gov au] Observations retrieved for " + this.header.name + ", " + this.header.state);
369
390
 
370
391
  this.BOMdataExpires = expiresAt;
371
- this.log("[BOM gov au] BOM data will expire at " + new Date(this.BOMdataExpires).toLocaleString() + " which is in " +
392
+ this.log.debug("[BOM gov au] BOM data will expire at " + new Date(this.BOMdataExpires).toLocaleString() + " which is in " +
372
393
  ((this.BOMdataExpires - fetchedAt) / 1000 / 60).toFixed(1) + "min");
373
394
 
374
395
  const temperature = this._getObservationValue("air_temp", -100, 100);
375
396
  const apparentTemp = this._getObservationValue("apparent_t", -100, 100);
376
397
  const humidity = this._getObservationValue("rel_hum", 0, 100);
377
398
  const pressure = this._getObservationValue("press", 700, 1100);
399
+ const readings = [];
400
+
401
+ if (this.sensors.temperature) {
402
+ readings.push("temperature: " + (temperature === undefined ? "unavailable" : temperature + " °C"));
403
+ }
404
+ if (this.sensors.apparentTemp) {
405
+ readings.push("apparent temperature: " +
406
+ (apparentTemp === undefined ? "unavailable" : apparentTemp + " °C"));
407
+ }
408
+ if (this.sensors.humidity) {
409
+ readings.push("humidity: " + (humidity === undefined ? "unavailable" : humidity + "%"));
410
+ }
411
+ if (this.sensors.pressure) {
412
+ readings.push("pressure: " + (pressure === undefined ? "unavailable" : pressure + " hPa"));
413
+ }
414
+ if (readings.length) {
415
+ this.log("[BOM gov au] Current readings: " + readings.join(", "));
416
+ }
378
417
 
379
418
  if (this.sensors.temperature && this.temperatureService) {
380
419
  this.temperatureService.setCharacteristic(Characteristic.StatusActive, temperature !== undefined);
@@ -442,61 +481,77 @@ class BOMgovauAccessory {
442
481
  /**
443
482
  * Gets the current air temperature.
444
483
  *
484
+ * Returns the last valid characteristic value while the sensor is offline.
485
+ *
445
486
  * @returns {Promise<number>} Temperature in Celsius
446
- * @throws {Error} If temperature is unavailable or outside the supported range
447
487
  */
448
488
  async getStateTemperature() {
449
489
  await this.updateObservations();
450
- const value = this._getObservationValue("air_temp", -100, 100);
451
- if (value === undefined) {
452
- throw new Error("BOM temperature is unavailable or outside the supported range");
453
- }
454
- return value;
490
+ return this._getObservationValue("air_temp", -100, 100) ??
491
+ this._getCharacteristicFallback(
492
+ this.temperatureService,
493
+ this.hap.Characteristic.CurrentTemperature,
494
+ -100,
495
+ 100,
496
+ 0
497
+ );
455
498
  }
456
499
 
457
500
  /**
458
501
  * Gets the current air pressure.
459
502
  *
503
+ * Returns the last valid characteristic value while the sensor is offline.
504
+ *
460
505
  * @returns {Promise<number>} Pressure in hPa
461
- * @throws {Error} If pressure is unavailable or outside the supported range
462
506
  */
463
507
  async getStatePressure() {
464
508
  await this.updateObservations();
465
- const value = this._getObservationValue("press", 700, 1100);
466
- if (value === undefined) {
467
- throw new Error("BOM pressure is unavailable or outside the supported range");
468
- }
469
- return value;
509
+ return this._getObservationValue("press", 700, 1100) ??
510
+ this._getCharacteristicFallback(
511
+ this.airPressureService,
512
+ this.AirPressureCharacteristic,
513
+ 700,
514
+ 1100,
515
+ 1013
516
+ );
470
517
  }
471
518
 
472
519
  /**
473
520
  * Gets the current apparent temperature.
474
521
  *
522
+ * Returns the last valid characteristic value while the sensor is offline.
523
+ *
475
524
  * @returns {Promise<number>} Apparent temperature in Celsius
476
- * @throws {Error} If apparent temperature is unavailable or outside the supported range
477
525
  */
478
526
  async getStateApparentTemperature() {
479
527
  await this.updateObservations();
480
- const value = this._getObservationValue("apparent_t", -100, 100);
481
- if (value === undefined) {
482
- throw new Error("BOM apparent temperature is unavailable or outside the supported range");
483
- }
484
- return value;
528
+ return this._getObservationValue("apparent_t", -100, 100) ??
529
+ this._getCharacteristicFallback(
530
+ this.apparentTempService,
531
+ this.hap.Characteristic.CurrentTemperature,
532
+ -100,
533
+ 100,
534
+ 0
535
+ );
485
536
  }
486
537
 
487
538
  /**
488
539
  * Gets the current relative humidity.
489
540
  *
541
+ * Returns the last valid characteristic value while the sensor is offline.
542
+ *
490
543
  * @returns {Promise<number>} Relative humidity percentage
491
- * @throws {Error} If humidity is unavailable or outside the supported range
492
544
  */
493
545
  async getStateHumidity() {
494
546
  await this.updateObservations();
495
- const value = this._getObservationValue("rel_hum", 0, 100);
496
- if (value === undefined) {
497
- throw new Error("BOM humidity is unavailable or outside the supported range");
498
- }
499
- return value;
547
+ return this._getObservationValue("rel_hum", 0, 100) ??
548
+ this._getCharacteristicFallback(
549
+ this.humidityService,
550
+ this.hap.Characteristic.CurrentRelativeHumidity,
551
+ 0,
552
+ 100,
553
+ 0
554
+ );
500
555
  }
501
556
  }
502
557
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-bomgovau-v2",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Makes BOM.gov.au data available in your Homebridge(Homekit) setup",
5
5
  "homepage": "https://github.com/pcoombe/homebridge-BOMgovau",
6
6
  "license": "Apache-2",
@@ -13,6 +13,7 @@ class CharacteristicMock {
13
13
  this.displayName = displayName;
14
14
  this.UUID = UUID;
15
15
  this.props = props;
16
+ this.value = this.getDefaultValue();
16
17
  }
17
18
 
18
19
  setProps(props) {
@@ -127,7 +128,9 @@ async function main() {
127
128
  }
128
129
  };
129
130
  const logErrors = [];
130
- const logger = () => {};
131
+ const infoLogs = [];
132
+ const logger = (...args) => infoLogs.push(args.join(" "));
133
+ logger.debug = () => {};
131
134
  logger.warn = () => {};
132
135
  logger.error = (...args) => logErrors.push(args.join(" "));
133
136
 
@@ -168,12 +171,22 @@ async function main() {
168
171
  accessory.bomClient.fetchObservations = async () => {
169
172
  throw new Error("Simulated initial BOM request failure");
170
173
  };
171
- await assert.rejects(accessory.getStateTemperature(), /temperature is unavailable/,
172
- "Initial temperature should be unavailable when the first BOM request fails");
173
- await assert.rejects(accessory.getStateHumidity(), /humidity is unavailable/,
174
- "Initial humidity should be unavailable when the first BOM request fails");
175
- await assert.rejects(accessory.getStateApparentTemperature(), /apparent temperature is unavailable/,
176
- "Initial apparent temperature should be unavailable when the first BOM request fails");
174
+ const [initialTemperature, initialHumidity, initialApparentTemperature, initialPressure] =
175
+ await Promise.all([
176
+ accessory.getStateTemperature(),
177
+ accessory.getStateHumidity(),
178
+ accessory.getStateApparentTemperature(),
179
+ accessory.getStatePressure()
180
+ ]);
181
+ assert.ok(initialTemperature >= -100 && initialTemperature <= 100,
182
+ "Initial temperature getter should return a valid fallback");
183
+ assert.ok(initialHumidity >= 0 && initialHumidity <= 100,
184
+ "Initial humidity getter should return a valid fallback");
185
+ assert.ok(initialApparentTemperature >= -100 && initialApparentTemperature <= 100,
186
+ "Initial apparent temperature getter should return a valid fallback");
187
+ assert.ok(initialPressure >= 700 && initialPressure <= 1100,
188
+ "Initial pressure getter should return a valid fallback");
189
+ assert.equal(initialPressure, 1013, "Initial pressure should use the in-range fallback");
177
190
  assert.equal(accessory.obs.air_temp, undefined,
178
191
  "Temperature should not start with a plausible zero reading");
179
192
  assert.equal(accessory.obs.rel_hum, undefined,
@@ -186,6 +199,8 @@ async function main() {
186
199
  assert.equal(characteristicValue(accessory.humidityService, "StatusFault"), true);
187
200
  assert.equal(characteristicValue(accessory.apparentTempService, "StatusActive"), false);
188
201
  assert.equal(characteristicValue(accessory.apparentTempService, "StatusFault"), true);
202
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusActive"), false);
203
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusFault"), true);
189
204
  logErrors.length = 0;
190
205
 
191
206
  let fetchCount = 0;
@@ -203,6 +218,15 @@ async function main() {
203
218
  await new Promise(resolve => setImmediate(resolve));
204
219
 
205
220
  assert.equal(logErrors.length, 0, `Plugin logged errors:\n${logErrors.join("\n")}`);
221
+ const readingsLog = infoLogs.find(message => message.includes("[BOM gov au] Current readings:"));
222
+ assert.ok(readingsLog, "Successful observation retrieval should log current readings");
223
+ assert.ok(readingsLog.includes(`temperature: ${accessory.obs.air_temp} °C`));
224
+ assert.ok(readingsLog.includes(`apparent temperature: ${accessory.obs.apparent_t} °C`));
225
+ assert.ok(readingsLog.includes(`humidity: ${accessory.obs.rel_hum}%`));
226
+ assert.ok(readingsLog.includes(typeof accessory.obs.press === "number" &&
227
+ accessory.obs.press >= 700 && accessory.obs.press <= 1100
228
+ ? `pressure: ${accessory.obs.press} hPa`
229
+ : "pressure: unavailable"));
206
230
  assert.ok(accessory.header?.name, "BOM station name was not loaded");
207
231
  assert.ok(accessory.header?.state, "BOM station state was not loaded");
208
232
  assert.ok(/^\d{14}$/.test(accessory.obs?.aifstime_utc), "Observation timestamp is invalid");
@@ -248,7 +272,11 @@ async function main() {
248
272
  "Pressure service should be faulted when the feed omits pressure");
249
273
  assert.equal(characteristicValue(accessory.airPressureService, "StatusActive"), false,
250
274
  "Pressure service should be offline when the feed omits pressure");
251
- await assert.rejects(accessory.getStatePressure(), /pressure is unavailable/);
275
+ const pressureReading = await accessory.getStatePressure();
276
+ assert.ok(pressureReading >= 700 && pressureReading <= 1100,
277
+ "Missing pressure should return a valid fallback value");
278
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusActive"), false);
279
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusFault"), true);
252
280
  }
253
281
 
254
282
  assert.equal(historyEntries.length, 1, "A complete weather history entry should be recorded");
@@ -259,6 +287,29 @@ async function main() {
259
287
 
260
288
  assert.ok(accessory.BOMdataExpires > Date.now(), "Observation cache expiry is not in the future");
261
289
 
290
+ const lastPressureValue = characteristicValue(
291
+ accessory.airPressureService,
292
+ accessory.AirPressureCharacteristic
293
+ );
294
+ const missingPressureAt = Date.now();
295
+ accessory.BOMdataExpires = 0;
296
+ accessory.bomClient.fetchObservations = async () => ({
297
+ observation: {
298
+ ...accessory.obs,
299
+ press: undefined
300
+ },
301
+ header: accessory.header,
302
+ fetchedAt: missingPressureAt,
303
+ expiresAt: missingPressureAt + 60_000
304
+ });
305
+ await accessory.updateObservations();
306
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusActive"), false,
307
+ "A missing pressure reading should mark the pressure sensor offline");
308
+ assert.equal(characteristicValue(accessory.airPressureService, "StatusFault"), true,
309
+ "A missing pressure reading should mark the pressure sensor faulted");
310
+ assert.equal(await accessory.getStatePressure(), lastPressureValue,
311
+ "A missing pressure reading should return the last valid HomeKit pressure value");
312
+
262
313
  const cachedTemperature = accessory.obs.air_temp;
263
314
  const cachedExpiry = accessory.BOMdataExpires;
264
315
  accessory.BOMdataExpires = 0;