Skip to content

Commit

Permalink
Fix: request errors (#4)
Browse files Browse the repository at this point in the history
* Fix edge case when setting override IP back to empty

* Wrap all fetch calls in try/catch
  • Loading branch information
ChrisTerBeke committed May 2, 2024
1 parent 4084be0 commit 2851278
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
9 changes: 0 additions & 9 deletions drivers/uponor/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class UponorThermostatDevice extends Device {

async onInit(): Promise<void> {
this.registerCapabilityListener('target_temperature', this._setTargetTemperature.bind(this))
// this.registerCapabilityListener('thermostat_mode', this._setThermostatMode.bind(this))
this._init()
}

Expand Down Expand Up @@ -99,7 +98,6 @@ class UponorThermostatDevice extends Device {
this.setAvailable()
this.setCapabilityValue('measure_temperature', data.temperature)
this.setCapabilityValue('target_temperature', data.setPoint)
// this.setCapabilityValue('thermostat_mode', data.mode)
}

private async _setTargetTemperature(value: number): Promise<void> {
Expand All @@ -108,13 +106,6 @@ class UponorThermostatDevice extends Device {
await this._client.setTargetTemperature(controllerID, thermostatID, value)
await this._syncAttributes()
}

private async _setThermostatMode(value: Mode): Promise<void> {
if (!this._client) return
const { controllerID, thermostatID } = this.getData()
await this._client.setMode(controllerID, thermostatID, value)
await this._syncAttributes()
}
}

module.exports = UponorThermostatDevice
46 changes: 22 additions & 24 deletions lib/UponorHTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,18 @@ export class UponorHTTPClient {
}

private async _syncAttributes(): Promise<Map<string, string>> {
const request = await fetch(this._url, {
method: 'POST',
headers: { 'x-jnap-action': 'http://phyn.com/jnap/uponorsky/GetAttributes' },
body: '{}'
})
const data: AttributesResponse = await request.json() as AttributesResponse
if (data.result != 'OK') {
return Promise.reject(data.result)
}

const result = new Map<string, string>()
for (const v of data.output.vars) {
result.set(v.waspVarName, v.waspVarValue)
try {
const request = await fetch(this._url, {
method: 'POST',
headers: { 'x-jnap-action': 'http://phyn.com/jnap/uponorsky/GetAttributes' },
body: '{}'
})
const data: AttributesResponse = await request.json() as AttributesResponse
if (data.result != 'OK') return Promise.reject(data.result)
return new Map(data.output.vars.map(v => [v.waspVarName, v.waspVarValue]))
} catch (error) {
return Promise.reject(error)
}

return result
}

private _syncThermostats(): Map<string, Thermostat> {
Expand Down Expand Up @@ -168,15 +164,17 @@ export class UponorHTTPClient {
}

private async _setAttributes(attributes: Map<string, string>): Promise<void> {
const vars = Array.from(attributes, ([key, value]) => [{ "waspVarName": key, "waspVarValue": value }]).flat()
const request = await fetch(this._url, {
method: 'POST',
headers: { 'x-jnap-action': 'http://phyn.com/jnap/uponorsky/SetAttributes' },
body: JSON.stringify({ "vars": vars }),
})
const data: AttributesResponse = await request.json() as AttributesResponse
if (data.result != 'OK') {
return Promise.reject(data.result)
try {
const vars = Array.from(attributes, ([key, value]) => [{ "waspVarName": key, "waspVarValue": value }]).flat()
const request = await fetch(this._url, {
method: 'POST',
headers: { 'x-jnap-action': 'http://phyn.com/jnap/uponorsky/SetAttributes' },
body: JSON.stringify({ "vars": vars }),
})
const data: AttributesResponse = await request.json() as AttributesResponse
if (data.result != 'OK') return Promise.reject(data.result)
} catch (error) {
return Promise.reject(error)
}
}

Expand Down

0 comments on commit 2851278

Please sign in to comment.