From da131e5ce9b02c3c8e867feb5a073b3a012192c5 Mon Sep 17 00:00:00 2001 From: Chris ter Beke <1134120+ChrisTerBeke@users.noreply.github.com> Date: Sat, 10 Aug 2024 20:30:26 +0200 Subject: [PATCH] Add repair flow for authentication (#15) --- app.json | 5 ++ drivers/remeha/driver.compose.json | 125 +++++++++++++++-------------- drivers/remeha/driver.ts | 97 ++++++++++++---------- drivers/remeha/repair/login.html | 66 +++++++++++++++ 4 files changed, 190 insertions(+), 103 deletions(-) create mode 100644 drivers/remeha/repair/login.html diff --git a/app.json b/app.json index ba07fcd..d402db1 100644 --- a/app.json +++ b/app.json @@ -154,6 +154,11 @@ "template": "add_devices" } ], + "repair": [ + { + "id": "login" + } + ], "settings": [ { "type": "group", diff --git a/drivers/remeha/driver.compose.json b/drivers/remeha/driver.compose.json index 27ffab0..a6981e9 100644 --- a/drivers/remeha/driver.compose.json +++ b/drivers/remeha/driver.compose.json @@ -1,70 +1,71 @@ { - "name": { - "en": "Remeha eTwist" + "name": { + "en": "Remeha eTwist" + }, + "class": "thermostat", + "capabilities": [ + "target_temperature", + "measure_temperature", + "measure_temperature_water", + "measure_temperature_outside", + "measure_pressure", + "alarm_water" + ], + "platforms": ["local"], + "connectivity": ["cloud"], + "images": { + "small": "{{driverAssetsPath}}/images/small.png", + "large": "{{driverAssetsPath}}/images/large.png", + "xlarge": "{{driverAssetsPath}}/images/xlarge.png" + }, + "pair": [ + { + "id": "login" }, - "class": "thermostat", - "capabilities": [ - "target_temperature", - "measure_temperature", - "measure_temperature_water", - "measure_temperature_outside", - "measure_pressure", - "alarm_water" - ], - "platforms": [ - "local" - ], - "connectivity": [ - "cloud" - ], - "images": { - "small": "{{driverAssetsPath}}/images/small.png", - "large": "{{driverAssetsPath}}/images/large.png", - "xlarge": "{{driverAssetsPath}}/images/xlarge.png" + { + "id": "list_devices", + "template": "list_devices", + "navigation": { + "next": "add_devices" + } }, - "pair": [ + { + "id": "add_devices", + "template": "add_devices" + } + ], + "repair": [ + { + "id": "login" + } + ], + "settings": [ + { + "type": "group", + "label": { + "en": "Troubleshooting", + "nl": "Probleemoplossing" + }, + "children": [ { - "id": "login" + "id": "debugEnabled", + "type": "checkbox", + "label": { + "en": "Enable debug data", + "nl": "Schakel probleemverhelping in" + }, + "value": false }, { - "id": "list_devices", - "template": "list_devices", - "navigation": { - "next": "add_devices" - } - }, - { - "id": "add_devices", - "template": "add_devices" - } - ], - "settings": [ - { - "type": "group", - "label": { - "en": "Troubleshooting", - "nl": "Probleemoplossing" - }, - "children": [ - { - "id": "debugEnabled", - "type": "checkbox", - "label": { - "en": "Enable debug data", - "nl": "Schakel probleemverhelping in" - }, - "value": false - }, - { - "id": "apiData", - "type": "textarea", - "label": { - "en": "Last API response", - "nl": "Laatste API-reactie" - }, - "value": "{}" - } - ] + "id": "apiData", + "type": "textarea", + "label": { + "en": "Last API response", + "nl": "Laatste API-reactie" + }, + "value": "{}" } - ] + ] + } + ] } diff --git a/drivers/remeha/driver.ts b/drivers/remeha/driver.ts index 8211981..869ba52 100644 --- a/drivers/remeha/driver.ts +++ b/drivers/remeha/driver.ts @@ -1,46 +1,61 @@ -import { Driver } from 'homey' -import { PairSession } from 'homey/lib/Driver' -import { RemehaAuth, TokenData } from '../../lib/RemehaAuth' -import { DeviceData, RemehaMobileApi } from '../../lib/RemehaMobileApi' +import { Device, Driver } from "homey"; +import { PairSession } from "homey/lib/Driver"; +import { RemehaAuth, TokenData } from "../../lib/RemehaAuth"; +import { DeviceData, RemehaMobileApi } from "../../lib/RemehaMobileApi"; class RemehaDriver extends Driver { + private _tokenData: TokenData | null = null; - private _tokenData: TokenData | null = null - - async onPair(session: PairSession) { - session.setHandler('login', this._login.bind(this)) - session.setHandler('list_devices', this._listDevices.bind(this)) - } - - private async _login(credentials: string): Promise { - const authorizer = new RemehaAuth() - const [email, password] = credentials.split('|') - if (!email || !password) throw new Error('Invalid credentials') - this._tokenData = await authorizer.login(email, password) - this.homey.settings.set('debug_token', JSON.stringify(this._tokenData)) - } - - private async _listDevices(): Promise { - if (!this._tokenData || !this._tokenData.accessToken) return [] - const api = new RemehaMobileApi(this._tokenData.accessToken) - const debug = await api.debug() - this.homey.settings.set('debug_devices', JSON.stringify(debug)) - const devices = await api.devices() - return devices.map(this._mapDevice.bind(this)) - } - - private _mapDevice(device: DeviceData): any { - return { - name: device.name, - data: { - id: device.id, - }, - store: { - accessToken: this._tokenData?.accessToken, - refreshToken: this._tokenData?.refreshToken, - } - } - } + async onPair(session: PairSession) { + this.homey.settings.set("debug_token", ""); + session.setHandler("login", this._login.bind(this)); + session.setHandler("list_devices", this._listDevices.bind(this)); + } + + async onRepair(session: PairSession, device: Device) { + this.homey.settings.set("debug_token", ""); + session.setHandler("repair", this._updateTokenData.bind(this, device)); + } + + private async _login(credentials: string): Promise { + this.homey.settings.set("debug_token", ""); + const authorizer = new RemehaAuth(); + const [email, password] = credentials.split("|"); + if (!email || !password) throw new Error("Invalid credentials"); + this._tokenData = await authorizer.login(email, password); + this.homey.settings.set("debug_token", JSON.stringify(this._tokenData)); + } + + private async _updateTokenData( + device: Device, + credentials: string, + ): Promise { + await this._login(credentials); + await device.setStoreValue("accessToken", this._tokenData?.accessToken); + await device.setStoreValue("refreshToken", this._tokenData?.refreshToken); + } + + private async _listDevices(): Promise { + if (!this._tokenData || !this._tokenData.accessToken) return []; + const api = new RemehaMobileApi(this._tokenData.accessToken); + const debug = await api.debug(); + this.homey.settings.set("debug_devices", JSON.stringify(debug)); + const devices = await api.devices(); + return devices.map(this._mapDevice.bind(this)); + } + + private _mapDevice(device: DeviceData): any { + return { + name: device.name, + data: { + id: device.id, + }, + store: { + accessToken: this._tokenData?.accessToken, + refreshToken: this._tokenData?.refreshToken, + }, + }; + } } -module.exports = RemehaDriver +module.exports = RemehaDriver; diff --git a/drivers/remeha/repair/login.html b/drivers/remeha/repair/login.html new file mode 100644 index 0000000..f43f9be --- /dev/null +++ b/drivers/remeha/repair/login.html @@ -0,0 +1,66 @@ + + +
+

+
+ +

+ +
+
+
+ + +
+
+ + +
+
+
+ +
+ +