From 88622b3c8f3ad2f7dc1b98b6912a98d0702598fd Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Mon, 26 Mar 2018 16:31:37 +0200 Subject: [PATCH] Let accessory values initially be undefined --- lib/accessory.js | 42 +++++++++++++++++++++++------------- test/test-accessory.js | 48 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/accessory.js b/lib/accessory.js index 79ab221..c4a3293 100644 --- a/lib/accessory.js +++ b/lib/accessory.js @@ -8,9 +8,9 @@ class HygrothermographAccessory { this.log = log; this.config = config; - this.currentTemperature = 0.0; - this.currentHumidity = 0.0; - this.currentBatteryLevel = 100; + this.latestTemperature = undefined; + this.latestHumidity = undefined; + this.latestBatteryLevel = undefined; this.informationService = this.getInformationService(); this.temperatureService = this.getTemperatureService(); @@ -40,36 +40,36 @@ class HygrothermographAccessory { this.log.debug('Initialized accessory'); } set temperature(newValue) { - this.currentTemperature = newValue; + this.latestTemperature = newValue; this.temperatureService .getCharacteristic(Characteristic.CurrentTemperature) .updateValue(newValue); } get temperature() { - return this.currentTemperature; + return this.latestTemperature; } set humidity(newValue) { - this.currentHumidity = newValue; + this.latestHumidity = newValue; this.humidityService .getCharacteristic(Characteristic.CurrentRelativeHumidity) .updateValue(newValue); } get humidity() { - return this.currentHumidity; + return this.latestHumidity; } set batteryLevel(newValue) { - this.currentBatteryLevel = newValue; + this.latestBatteryLevel = newValue; this.batteryService .getCharacteristic(Characteristic.BatteryLevel) .updateValue(newValue); } get batteryLevel() { - return this.currentBatteryLevel; + return this.latestBatteryLevel; } getInformationService() { @@ -79,11 +79,19 @@ class HygrothermographAccessory { .setCharacteristic(Characteristic.SerialNumber, 'LYWSDCGQ/01ZM'); } + onCharacteristicGetValue(callback, value) { + if (value === undefined) { + callback(new Error('Undefined characteristic value')); + } else { + callback(null, value); + } + } + getTemperatureService() { const temperatureService = new Service.TemperatureSensor('Temperature'); temperatureService .getCharacteristic(Characteristic.CurrentTemperature) - .on('get', callback => callback(null, this.temperature)); + .on('get', callback => this.onCharacteristicGetValue(callback, this.temperature)); temperatureService .getCharacteristic(Characteristic.CurrentTemperature) .setProps({ minValue: -10 }); @@ -97,25 +105,29 @@ class HygrothermographAccessory { const humidityService = new Service.HumiditySensor('Humidity'); humidityService .getCharacteristic(Characteristic.CurrentRelativeHumidity) - .on('get', callback => callback(null, this.humidity)); + .on('get', callback => this.onCharacteristicGetValue(callback, this.humidity)); return humidityService; } getBatteryService() { const batteryService = new Service.BatteryService('Battery'); batteryService.getCharacteristic(Characteristic.BatteryLevel) - .on('get', callback => callback(null, this.batteryLevel)); + .on('get', callback => this.onCharacteristicGetValue(callback, this.batteryLevel)); batteryService.setCharacteristic( Characteristic.ChargingState, Characteristic.ChargingState.NOT_CHARGEABLE, ); batteryService.getCharacteristic(Characteristic.StatusLowBattery) .on('get', (callback) => { - if (this.batteryLevel > 10) { - callback(null, Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); + let batteryStatus; + if (this.batteryLevel === undefined) { + batteryStatus = undefined; + } else if (this.batteryLevel > 10) { + batteryStatus = Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL; } else { - callback(null, Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW); + batteryStatus = Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW; } + this.onCharacteristicGetValue(callback, batteryStatus); }); return batteryService; } diff --git a/test/test-accessory.js b/test/test-accessory.js index 438d63b..13d3bfe 100644 --- a/test/test-accessory.js +++ b/test/test-accessory.js @@ -45,25 +45,25 @@ describe('accessory', () => { it('should update current temperature', () => { const accessory = new this.HygrothermographAccessory(mockLogger, {}); accessory.scanner.emit('temperatureChange', 20.5, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentTemperature, 20.5); + assert.strictEqual(accessory.latestTemperature, 20.5); accessory.scanner.emit('temperatureChange', 25.5, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentTemperature, 25.5); + assert.strictEqual(accessory.latestTemperature, 25.5); }); it('should update current humidity', () => { const accessory = new this.HygrothermographAccessory(mockLogger, {}); accessory.scanner.emit('humidityChange', 30.5, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentHumidity, 30.5); + assert.strictEqual(accessory.latestHumidity, 30.5); accessory.scanner.emit('humidityChange', 35.5, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentHumidity, 35.5); + assert.strictEqual(accessory.latestHumidity, 35.5); }); it('should update current battery level', () => { const accessory = new this.HygrothermographAccessory(mockLogger, {}); accessory.scanner.emit('batteryChange', 90, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentBatteryLevel, 90); + assert.strictEqual(accessory.latestBatteryLevel, 90); accessory.scanner.emit('batteryChange', 9, { address: '123', id: '123' }); - assert.strictEqual(accessory.currentBatteryLevel, 9); + assert.strictEqual(accessory.latestBatteryLevel, 9); }); it('should receive error', () => { @@ -82,6 +82,15 @@ describe('accessory', () => { assert(temperatureSpy.calledWith(null, 23)); }); + it('should error on undefined temperature characteristic get value ', () => { + const temperatureSpy = sinon.spy(); + const accessory = new this.HygrothermographAccessory(mockLogger, {}); + const characteristic = this.characteristics.CurrentTemperature; + assert.strictEqual(accessory.temperature, undefined); + characteristic.emit('get', temperatureSpy); + assert(temperatureSpy.calledWith(sinon.match.instanceOf(Error))); + }); + it('should answer humidity characteristic get value', () => { const humiditySpy = sinon.spy(); const accessory = new this.HygrothermographAccessory(mockLogger, {}); @@ -91,6 +100,15 @@ describe('accessory', () => { assert(humiditySpy.calledWith(null, 30)); }); + it('should error on undefined humidity characteristic get value ', () => { + const humiditySpy = sinon.spy(); + const accessory = new this.HygrothermographAccessory(mockLogger, {}); + const characteristic = this.characteristics.CurrentRelativeHumidity; + assert.strictEqual(accessory.humidity, undefined); + characteristic.emit('get', humiditySpy); + assert(humiditySpy.calledWith(sinon.match.instanceOf(Error))); + }); + it('should answer low battery characteristic get value', () => { const lowBatterySpy = sinon.spy(); const accessory = new this.HygrothermographAccessory(mockLogger, {}); @@ -105,6 +123,15 @@ describe('accessory', () => { assert(lowBatterySpy.calledWith(null, characteristic.BATTERY_LEVEL_NORMAL)); }); + it('should error on undefined low battery characteristic get value', () => { + const lowBatterySpy = sinon.spy(); + const accessory = new this.HygrothermographAccessory(mockLogger, {}); + const characteristic = this.characteristics.StatusLowBattery; + assert.strictEqual(accessory.batteryLevel, undefined); + characteristic.emit('get', lowBatterySpy); + assert(lowBatterySpy.calledWith(sinon.match.instanceOf(Error))); + }); + it('should answer battery level characteristic get value', () => { const batterySpy = sinon.spy(); const accessory = new this.HygrothermographAccessory(mockLogger, {}); @@ -115,6 +142,15 @@ describe('accessory', () => { assert(batterySpy.calledWith(null, 99)); }); + it('should error on undefined battery level characteristic get value ', () => { + const batterySpy = sinon.spy(); + const accessory = new this.HygrothermographAccessory(mockLogger, {}); + const characteristic = this.characteristics.BatteryLevel; + assert.strictEqual(accessory.batteryLevel, undefined); + characteristic.emit('get', batterySpy); + assert(batterySpy.calledWith(sinon.match.instanceOf(Error))); + }); + it('should return all services', () => { const accessory = new this.HygrothermographAccessory(mockLogger, {}); const services = accessory.getServices();