Skip to content

Commit

Permalink
Merge pull request #24 from danieljtribe/hotfix/config
Browse files Browse the repository at this point in the history
Handling for invalid Octopus API response
  • Loading branch information
danieljtribe authored Sep 22, 2024
2 parents b04faca + 531d654 commit 22bddab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"displayName": "Homebridge Agile Octopus Rates",
"name": "homebridge-agile-octopus-rates",
"version": "1.2.8",
"version": "1.2.9",
"description": "A simple plugin to publish switches to Homebridge, which can toggle on during the cleapest 30 minute, 60 minute etc... time periods.",
"license": "Apache-2.0",
"homepage": "https://github.com/danieljtribe/homebridge-agile-octopus",
Expand Down
43 changes: 25 additions & 18 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AgileOctopusAccessory {
private swCheapCustom: any;
private customLowPriceThreshold: number = 0.00;

private data = {} as OctopusAPITimeslotResponse[];
private data = [] as OctopusAPITimeslotResponse[];
private periodDefinitions = [] as SearchPeriod[];
private customDevices = [] as CustomDevice[];

Expand Down Expand Up @@ -107,29 +107,35 @@ export class AgileOctopusAccessory {
if(!this.config.disableSwitches) {
this.switches.forEach(async sw => {
let state = false;
sw.cheapestPeriods.forEach(cheapestPeriod => {
if(moment().isAfter(cheapestPeriod.startTime) && moment().isBefore(cheapestPeriod.endTime)) {
state = true;
if(sw.cheapestPeriods) {
sw.cheapestPeriods.forEach(cheapestPeriod => {
if(moment().isAfter(cheapestPeriod.startTime) && moment().isBefore(cheapestPeriod.endTime)) {
state = true;
}
});

if(state) {
if(!sw.state) this.platform.log.info(`Triggering switch for cheapest ${sw.blocks * 30} minute block (${sw.title})`);
sw.accessory.updateCharacteristic(this.platform.Characteristic.On, true);
sw.state = true;
} else {
if(sw.state) this.platform.log.debug(`Turning off ${sw.title}, end time passed`);
sw.accessory.updateCharacteristic(this.platform.Characteristic.On, false);
sw.state = false;
}
});

if(state) {
if(!sw.state) this.platform.log.info(`Triggering switch for cheapest ${sw.blocks * 30} minute block (${sw.title})`);
sw.accessory.updateCharacteristic(this.platform.Characteristic.On, true);
sw.state = true;
} else {
if(sw.state) this.platform.log.debug(`Turning off ${sw.title}, end time passed`);
sw.accessory.updateCharacteristic(this.platform.Characteristic.On, false);
sw.state = false;
this.platform.log.warn(`No suitable time periods configured for switch "${sw.title}"`);
}
});
}

let currentSlot = this.data.filter(slot => moment().isAfter(slot.startMoment) && moment().isBefore(slot.endMoment));
if(currentSlot) {
this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, currentSlot[0].value_inc_vat.toFixed(2));
if(!this.config.disableSwitches) this.swNegative.updateCharacteristic(this.platform.Characteristic.On, Number(currentSlot[0].value_inc_vat) <= Number(0.00));
if(!this.config.disableSwitches) this.swCheapCustom.updateCharacteristic(this.platform.Characteristic.On, Number(currentSlot[0].value_inc_vat) <= Number(this.customLowPriceThreshold));
if(this.data.length > 0) {
let currentSlot = this.data.filter(slot => moment().isAfter(slot.startMoment) && moment().isBefore(slot.endMoment));
if(currentSlot) {
this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, currentSlot[0].value_inc_vat.toFixed(2));
if(!this.config.disableSwitches) this.swNegative.updateCharacteristic(this.platform.Characteristic.On, Number(currentSlot[0].value_inc_vat) <= Number(0.00));
if(!this.config.disableSwitches) this.swCheapCustom.updateCharacteristic(this.platform.Characteristic.On, Number(currentSlot[0].value_inc_vat) <= Number(this.customLowPriceThreshold));
}
}
}

Expand Down Expand Up @@ -221,6 +227,7 @@ export class AgileOctopusAccessory {
return;
} catch(e) {
this.platform.log.warn("Unable to fetch current rates, will retry shortly.");
this.data = [];
}
}

Expand Down

0 comments on commit 22bddab

Please sign in to comment.