Skip to content

Commit

Permalink
More robustness for dynamic IP
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Apr 24, 2024
1 parent 014de50 commit 8760c18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
7 changes: 5 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@
"label": {
"en": "IP Address"
},
"type": "text",
"required": true
"type": "label",
"required": false,
"hint": {
"en": "The IP address of the device."
}
}
]
}
Expand Down
31 changes: 19 additions & 12 deletions drivers/uponor/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,46 @@ class UponorThermostatDevice extends Device {

async onAdded(): Promise<void> {
const { address } = this.getSettings()
await this._updateAddress(address)
await this._init(address)
}

async onUninit(): Promise<void> {
await this._uninit()
}

onDiscoveryResult(discoveryResult: DiscoveryResult) {
return this.getData().id.includes(discoveryResult.id)
}

async onDiscoveryAvailable(discoveryResult: DiscoveryResultMAC) {
await this._updateAddress(discoveryResult.address)
await this._init(discoveryResult.address)
}

async onDiscoveryAddressChanged(discoveryResult: DiscoveryResultMAC): Promise<void> {
await this._updateAddress(discoveryResult.address)
await this._init(discoveryResult.address)
}

async onDiscoveryLastSeenChanged(discoveryResult: DiscoveryResultMAC): Promise<void> {
await this._updateAddress(discoveryResult.address)
await this._init(discoveryResult.address)
}

async onDeleted(): Promise<void> {
clearInterval(this._syncInterval as NodeJS.Timeout)
this._syncInterval = undefined
this._client = undefined
await this._uninit()
}

async _updateAddress(newAddress: string): Promise<void> {
async _init(newAddress: string): Promise<void> {
// TODO: validate new IP address is correct before updating everything (ARP is not always reliable)
clearInterval(this._syncInterval as NodeJS.Timeout)
await this._uninit()
await this.setSettings({ address: newAddress })
const { address } = this.getSettings()
this._client = new UponorHTTPClient(address)
await this._syncAttributes()
this._client = new UponorHTTPClient(newAddress)
this._syncInterval = setInterval(this._syncAttributes.bind(this), POLL_INTERVAL_MS)
await this._syncAttributes()
}

async _uninit() {
clearInterval(this._syncInterval as NodeJS.Timeout)
this._syncInterval = undefined
this._client = undefined
}

private async _syncAttributes() {
Expand Down
7 changes: 5 additions & 2 deletions drivers/uponor/driver.compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
"label": {
"en": "IP Address"
},
"type": "text",
"required": true
"type": "label",
"required": false,
"hint": {
"en": "The IP address of the device."
}
}
]
}

0 comments on commit 8760c18

Please sign in to comment.