Skip to content

Commit

Permalink
fix(away): allowing rooms to be set to away
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed Sep 20, 2021
1 parent f0b93a2 commit 756bc0f
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
"required": false,
"default": false
},
"hidePrimaryStructure": {
"title": "Hide primary structure",
"description": "Hides the primary structure thermostat",
"type": "boolean",
"required": false,
"default": true
},
"hideVentTemperatureSensors": {
"title": "Hide vent temperature sensors",
"description": "Hides the Vent Temperature Sensors",
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],
"dependencies": {
"class-transformer": "^0.4.0",
"flair-api-ts": "^1.0.26",
"flair-api-ts": "^1.0.28",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
Expand Down
23 changes: 22 additions & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from 'flair-api-ts';
import { plainToClass } from 'class-transformer';
import { getRandomIntInclusive } from './utils';
import {FlairStructurePlatformAccessory} from './structurePlatformAccessory';

/**
* HomebridgePlatform
Expand All @@ -38,10 +39,12 @@ export class FlairPlatform implements DynamicPlatformPlugin {

private client?: Client;

private structure?: Structure;
public structure?: Structure;

private rooms: [FlairRoomPlatformAccessory?] = [];

private primaryStructureAccessory?: FlairStructurePlatformAccessory;

private _hasValidConfig?: boolean;

private _hasValidCredentials?: boolean;
Expand Down Expand Up @@ -151,6 +154,9 @@ export class FlairPlatform implements DynamicPlatformPlugin {
room.updateFromStructure(this.structure);
}
}
if (this.primaryStructureAccessory) {
this.primaryStructureAccessory.updateFromStructure(this.structure);
}
return this.structure;
}

Expand Down Expand Up @@ -234,6 +240,10 @@ export class FlairPlatform implements DynamicPlatformPlugin {
),
);
});
} else if (accessory.context.type === Structure.type) {
this.log.info('Restoring structure from cache:', accessory.displayName);
accessory.context.device = plainToClass(Structure, accessory.context.device);
this.primaryStructureAccessory = new FlairStructurePlatformAccessory(this, accessory, this.client!);
}

// add the restored accessory to the accessories cache so we can track if it has already been registered
Expand All @@ -254,6 +264,10 @@ export class FlairPlatform implements DynamicPlatformPlugin {
promisesToResolve.push(this.addDevices(await this.client!.getVents()));
}

if (!this.config.hidePrimaryStructure) {
promisesToResolve.push(this.addDevices([await this.client!.getPrimaryStructure()]));
}

if (!this.config.hidePuckRooms) {
promisesToResolve.push(
this.addDevices(
Expand Down Expand Up @@ -332,6 +346,13 @@ export class FlairPlatform implements DynamicPlatformPlugin {
),
);
});
} else if (device instanceof Structure) {
accessory.context.type = Structure.type;
this.primaryStructureAccessory = new FlairStructurePlatformAccessory(
this,
accessory,
this.client!,
);
} else {
continue;
}
Expand Down
81 changes: 49 additions & 32 deletions src/roomPlatformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export class FlairRoomPlatformAccessory {
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.room.id!);

this.thermostatService = this.accessory.getService(this.platform.Service.Thermostat)
?? this.accessory.addService(this.platform.Service.Thermostat);
?? this.accessory.addService(this.platform.Service.Thermostat);
this.thermostatService.setPrimaryService(true);
this.thermostatService
.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name)
.setCharacteristic(this.platform.Characteristic.CurrentTemperature, this.room.currentTemperatureC!)
.setCharacteristic(this.platform.Characteristic.TargetTemperature, this.room.setPointC!)
.setCharacteristic(
this.platform.Characteristic.TargetHeatingCoolingState,
this.getTargetHeatingCoolingStateFromStructure(this.structure)!,
this.getTargetHeatingCoolingStateFromStructureAndRoom(this.structure)!,
)
.setCharacteristic(
this.platform.Characteristic.CurrentHeatingCoolingState,
this.getCurrentHeatingCoolingStateFromStructure(this.structure)!,
this.getCurrentHeatingCoolingStateFromStructureAndRoom(this.structure)!,
)
.setCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.room.currentHumidity!);

Expand All @@ -63,41 +63,53 @@ export class FlairRoomPlatformAccessory {

this.thermostatService.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
.on(CharacteristicEventTypes.SET, this.setTargetHeatingCoolingState.bind(this));
// .on(CharacteristicEventTypes.GET, this.getTargetTemperature.bind(this))

setInterval(async () => {
await this.getNewRoomReadings();
}, (platform.config.pollInterval+ getRandomIntInclusive(1,20)) * 1000);
}, (platform.config.pollInterval + getRandomIntInclusive(1, 20)) * 1000);
this.getNewRoomReadings();
}

setTargetHeatingCoolingState(value: CharacteristicValue, callback: CharacteristicSetCallback):void {
setTargetHeatingCoolingState(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
if (value === this.platform.Characteristic.TargetHeatingCoolingState.OFF) {
this.platform.setStructureMode(FlairMode.AUTO, StructureHeatCoolMode.OFF).then((structure: Structure) => {
this.client.setRoomAway(this.room, true).then((room: Room) => {
this.updateRoomReadingsFromRoom(room);
this.platform.log.debug('Set Room to away', value);
// you must call the callback function
callback(null);
this.updateFromStructure(structure);
});
} else if(value === this.platform.Characteristic.TargetHeatingCoolingState.COOL) {
} else if (value === this.platform.Characteristic.TargetHeatingCoolingState.COOL) {
this.setRoomActive();
this.platform.setStructureMode(FlairMode.AUTO, StructureHeatCoolMode.COOL).then((structure: Structure) => {
callback(null);
this.updateFromStructure(structure);
});
} else if(value === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
} else if (value === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
this.setRoomActive();
this.platform.setStructureMode(FlairMode.AUTO, StructureHeatCoolMode.HEAT).then((structure: Structure) => {
callback(null);
this.updateFromStructure(structure);
});
} else if(value === this.platform.Characteristic.TargetHeatingCoolingState.AUTO) {
} else if (value === this.platform.Characteristic.TargetHeatingCoolingState.AUTO) {
this.setRoomActive();
this.platform.setStructureMode(FlairMode.AUTO, StructureHeatCoolMode.AUTO).then((structure: Structure) => {
callback(null);
this.updateFromStructure(structure);
});
}
}

setRoomActive(): void {
if (this.room.active) {
return;
}
this.client.setRoomAway(this.room, false).then((room: Room) => {
this.platform.log.debug('Set Room to active');
});
}


setTargetTemperature(value: CharacteristicValue, callback: CharacteristicSetCallback):void {
setTargetTemperature(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
this.client.setRoomSetPoint(this.room, value as number).then((room: Room) => {
this.updateRoomReadingsFromRoom(room);
this.platform.log.debug('Set Characteristic Temperature -> ', value);
Expand All @@ -107,7 +119,7 @@ export class FlairRoomPlatformAccessory {

}

getTargetTemperature(callback: CharacteristicGetCallback):void {
getTargetTemperature(callback: CharacteristicGetCallback): void {
this.getNewRoomReadings().then((room: Room) => {
callback(null, room.setPointC);
});
Expand All @@ -126,47 +138,46 @@ export class FlairRoomPlatformAccessory {
return this.room;
}

public updateFromStructure(structure: Structure):void {
public updateFromStructure(structure: Structure): void {
this.structure = structure;

// push the new value to HomeKit
this.thermostatService
.updateCharacteristic(
this.platform.Characteristic.TargetHeatingCoolingState,
this.getTargetHeatingCoolingStateFromStructure(this.structure)!,
)
.updateCharacteristic(
this.platform.Characteristic.CurrentHeatingCoolingState,
this.getCurrentHeatingCoolingStateFromStructure(this.structure)!,
);
this.updateRoomReadingsFromRoom(this.room);

this.platform.log.debug(
`Pushed updated current structure state for ${this.room.name!} to HomeKit:`,
this.structure.structureHeatCoolMode!,
this.structure.structureHeatCoolMode!,
);
}

updateRoomReadingsFromRoom(room: Room):void {
updateRoomReadingsFromRoom(room: Room): void {
this.accessory.context.device = room;
this.room = room;

// push the new value to HomeKit
this.thermostatService
.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.room.currentTemperatureC!)
.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.room.setPointC!)
.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.room.currentHumidity!);
.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.room.currentHumidity!)
.updateCharacteristic(
this.platform.Characteristic.TargetHeatingCoolingState,
this.getTargetHeatingCoolingStateFromStructureAndRoom(this.structure)!,
)
.updateCharacteristic(
this.platform.Characteristic.CurrentHeatingCoolingState,
this.getCurrentHeatingCoolingStateFromStructureAndRoom(this.structure)!,
);
this.platform.log.debug(
`Pushed updated current temperature state for ${this.room.name!} to HomeKit:`,
this.room.currentTemperatureC!,
this.room.currentTemperatureC!,
);
}

private getCurrentHeatingCoolingStateFromStructure(structure: Structure) {
if (structure.structureHeatCoolMode === StructureHeatCoolMode.OFF) {
private getCurrentHeatingCoolingStateFromStructureAndRoom(structure: Structure) {
if (!this.room.active) {
return this.platform.Characteristic.CurrentHeatingCoolingState.OFF;
}


if (structure.structureHeatCoolMode === StructureHeatCoolMode.COOL) {
return this.platform.Characteristic.CurrentHeatingCoolingState.COOL;
}
Expand All @@ -176,13 +187,17 @@ export class FlairRoomPlatformAccessory {
}

if (structure.structureHeatCoolMode === StructureHeatCoolMode.AUTO) {
//TODO: When the structure api shows the current thermostat mode change this to that.
// For now active always means cool.
return this.platform.Characteristic.CurrentHeatingCoolingState.COOL;
}

return this.platform.Characteristic.CurrentHeatingCoolingState.OFF;
}


private getTargetHeatingCoolingStateFromStructure(structure: Structure) {
if (structure.structureHeatCoolMode === StructureHeatCoolMode.OFF) {
private getTargetHeatingCoolingStateFromStructureAndRoom(structure: Structure) {
if (!this.room.active) {
return this.platform.Characteristic.TargetHeatingCoolingState.OFF;
}

Expand All @@ -197,6 +212,8 @@ export class FlairRoomPlatformAccessory {
if (structure.structureHeatCoolMode === StructureHeatCoolMode.AUTO) {
return this.platform.Characteristic.TargetHeatingCoolingState.AUTO;
}

return this.platform.Characteristic.TargetHeatingCoolingState.AUTO;
}

}
Loading

0 comments on commit 756bc0f

Please sign in to comment.