-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(AcControl): add air conditioner component
- Loading branch information
Showing
9 changed files
with
303 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Axios } from 'axios-observable'; | ||
import nock from 'nock'; | ||
import { map } from 'rxjs/operators'; | ||
import { Server } from '../src/server'; | ||
|
||
const config = require('./support/config_test.json'); | ||
const loxoneDiscoverResponse = require('./responses/loxone-discover.json'); | ||
let app; | ||
|
||
describe('Air Conditionner', () => { | ||
beforeAll((done: DoneFn) => { | ||
const url = `${config.loxone.protocol}://${config.loxone.url}`; | ||
|
||
nock(url) | ||
.post('/data/LoxApp3.json') | ||
.reply(200, loxoneDiscoverResponse); | ||
|
||
app = new Server({ | ||
jwt: __dirname + '/support/jwt.json', | ||
config: __dirname + '/support/config_test.json', | ||
port: 3000, | ||
}, done); | ||
}); | ||
|
||
afterAll((done) => { | ||
app?.server?.close(done); | ||
}); | ||
|
||
it('should request the status of a temperature', (done: DoneFn) => { | ||
Axios.post(`http://localhost:3000/smarthome`, { | ||
requestId: 'ff36a3cc-ec34-11e6-b1a0-64510650abcf', | ||
inputs: [{ | ||
intent: 'action.devices.QUERY', | ||
payload: { | ||
devices: [ | ||
{ | ||
id: '10f4ff00-0155-692f-ffff6322d0f91670' | ||
} | ||
] | ||
} | ||
}] | ||
}, { | ||
headers: { | ||
Authorization: 'Bearer access-token-from-skill', | ||
} | ||
}) | ||
.pipe(map((resp: any) => resp.data)) | ||
.subscribe((resp) => { | ||
expect(resp.payload.devices['10f4ff00-0155-692f-ffff6322d0f91670'].online).toBeTruthy(); | ||
// TODO : Test status | ||
done(); | ||
}); | ||
}); | ||
|
||
// TODO : Mock Loxone Socket | ||
// it('should update the target temperature', (done: DoneFn) => { | ||
// Axios.post(`http://localhost:3000/smarthome`, { | ||
// requestId: 'ff36a3cc-ec34-11e6-b1a0-64510650abcf', | ||
// inputs: [{ | ||
// intent: 'action.devices.QUERY', | ||
// payload: { | ||
// "commands": [{ | ||
// "devices": [{ | ||
// "id": "1b70c016-0396-1f6d-ffff0be037a23d47" | ||
// }], | ||
// "execution": [{ | ||
// "command": "action.devices.commands.ThermostatTemperatureSetpoint", | ||
// "params": { | ||
// "thermostatTemperatureSetpoint": 24 | ||
// } | ||
// }] | ||
// }] | ||
// } | ||
// }] | ||
// }, { | ||
// headers: { | ||
// Authorization: 'Bearer access-token-from-skill', | ||
// } | ||
// }) | ||
// .pipe(map((resp: any) => resp.data)) | ||
// .subscribe((resp) => { | ||
// expect(resp.payload.devices['10f4ff00-0155-692f-ffff6322d0f91670'].online).toBeTruthy(); | ||
// expect(resp.payload.devices['10f4ff00-0155-692f-ffff6322d0f91670'].thermostatTemperatureSetpoint).toBe('24'); | ||
// // TODO : Test status | ||
// done(); | ||
// }); | ||
// }); | ||
|
||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { map, Observable, of, Subject } from 'rxjs'; | ||
import { CapabilityHandler } from '../capabilities/capability-handler'; | ||
import { TemperatureSetting, TemperatureSettingHandler, TemperatureState } from '../capabilities/temperature-setting'; | ||
import { ComponentRaw } from '../config'; | ||
import { LoxoneRequest } from '../loxone-request'; | ||
import { Component } from './component'; | ||
import { OnOff, OnOffHandler } from '../capabilities/on-off'; | ||
import { ErrorType } from '../error'; | ||
|
||
export class AirCoolerComponent extends Component implements OnOff, TemperatureSetting { | ||
private on: boolean; | ||
protected temperatureState: TemperatureState = new TemperatureState(); | ||
|
||
constructor(rawComponent: ComponentRaw, loxoneRequest: LoxoneRequest, statesEvents: Subject<Component>) { | ||
super(rawComponent, loxoneRequest, statesEvents); | ||
|
||
this.loxoneRequest.getControlInformation(this.loxoneId).subscribe(temperature => { | ||
this.loxoneRequest.watchComponent(temperature.states.temperature).subscribe((event) => { | ||
if(event === 0) { | ||
console.log('Ambient temperature not managed by the airconditionner, try to find a a sensor in the same room.'); | ||
// TODO : unsubscribe the current event | ||
// TODO : find in the same room a temperature sensor | ||
} | ||
|
||
this.temperatureState.thermostatTemperatureAmbient = parseInt(event); // Current temp is not returned by Loxone | ||
this.statesEvents.next(this); | ||
}); | ||
this.loxoneRequest.watchComponent(temperature.states.status).subscribe((event) => { | ||
switch (parseInt(event)) { | ||
case 0: this.on = false; break; | ||
case 1: this.on = true; break; | ||
} | ||
this.statesEvents.next(this); | ||
}); | ||
this.loxoneRequest.watchComponent(temperature.states.targetTemperature).subscribe((event) => { | ||
this.temperatureState.thermostatTemperatureSetpoint = parseInt(event, 10); | ||
this.statesEvents.next(this); | ||
}); | ||
this.loxoneRequest.watchComponent(temperature.states.mode).subscribe((event) => { | ||
switch (parseInt(event)) { | ||
case 1: this.temperatureState.thermostatMode = 'auto'; break; | ||
case 2: this.temperatureState.thermostatMode = 'heat'; break; | ||
case 3: this.temperatureState.thermostatMode = 'cool'; break; | ||
case 4: this.temperatureState.thermostatMode = 'dry'; break; | ||
case 5: this.temperatureState.thermostatMode = 'fan-only'; break; | ||
} | ||
this.statesEvents.next(this); | ||
}); | ||
}); | ||
} | ||
|
||
turnOn(): Observable<boolean> { | ||
return this.loxoneRequest.sendCmd(this.loxoneId, 'on').pipe(map((result) => { | ||
if (result.code === '200') { | ||
this.on = true; | ||
this.statesEvents.next(this); | ||
return true; | ||
} | ||
throw new Error(ErrorType.ENDPOINT_UNREACHABLE) | ||
})) | ||
} | ||
|
||
turnOff(): Observable<boolean> { | ||
return this.loxoneRequest.sendCmd(this.loxoneId, 'off').pipe(map((result) => { | ||
if (result.code === '200') { | ||
this.on = false; | ||
this.statesEvents.next(this); | ||
return true; | ||
} | ||
throw new Error(ErrorType.ENDPOINT_UNREACHABLE); | ||
})); | ||
} | ||
|
||
getPowerState(): Observable<boolean> { | ||
return of(this.on); | ||
} | ||
|
||
getCapabilities(): CapabilityHandler<any>[] { | ||
return [ | ||
OnOffHandler.INSTANCE, | ||
TemperatureSettingHandler.INSTANCE | ||
]; | ||
} | ||
|
||
getTemperature(): Observable<TemperatureState> { | ||
return of(this.temperatureState) | ||
} | ||
|
||
setTemperature(temp: number): Observable<boolean> { | ||
return this.loxoneRequest.sendCmd(this.loxoneId, `setTarget/${temp}`).pipe(map((result) => { | ||
if (result.code === '200') { | ||
this.temperatureState.thermostatTemperatureSetpoint = temp; | ||
this.statesEvents.next(this); | ||
return true; | ||
} | ||
throw new Error(ErrorType.ENDPOINT_UNREACHABLE); | ||
})); | ||
} | ||
|
||
setMode(mode: string): Observable<boolean> { | ||
let loxoneMode: number; | ||
switch (mode) { | ||
case 'auto': loxoneMode = 1; break; | ||
case 'heat': loxoneMode = 2; break; | ||
case 'cool': loxoneMode = 3; break; | ||
case 'dry': loxoneMode = 4; break; | ||
case 'fan-only': loxoneMode = 5; break; | ||
} | ||
|
||
return this.loxoneRequest.sendCmd(this.loxoneId, `setMode/${loxoneMode}`).pipe(map((result) => { | ||
if (result.code === '200') { | ||
this.temperatureState.thermostatMode = mode; | ||
this.statesEvents.next(this); | ||
return true; | ||
} | ||
throw new Error(ErrorType.ENDPOINT_UNREACHABLE); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.