Skip to content

Commit

Permalink
Let accessory values initially be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
hannseman committed Mar 27, 2018
1 parent ac0d883 commit 88622b3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
42 changes: 27 additions & 15 deletions lib/accessory.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -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 });
Expand All @@ -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;
}
Expand Down
48 changes: 42 additions & 6 deletions test/test-accessory.js
Expand Up @@ -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', () => {
Expand All @@ -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, {});
Expand All @@ -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, {});
Expand All @@ -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, {});
Expand All @@ -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();
Expand Down

0 comments on commit 88622b3

Please sign in to comment.