From 3fffb15ac70b848176f4564f5bea8985da18491b Mon Sep 17 00:00:00 2001 From: ChrisTerBeke <1134120+ChrisTerBeke@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:50:46 +0100 Subject: [PATCH] Clean up some code --- drivers/uponor/device.ts | 41 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/drivers/uponor/device.ts b/drivers/uponor/device.ts index 210f775..2c8c972 100644 --- a/drivers/uponor/device.ts +++ b/drivers/uponor/device.ts @@ -26,13 +26,13 @@ class UponorThermostatDevice extends Device { } async onDiscoveryAvailable(discoveryResult: DiscoveryResultMAC): Promise { - await this.setSettings({ 'discovered_address': discoveryResult.address }) - this._updateAddress(discoveryResult.address, true) + await this._updateDiscoveredAddress(discoveryResult.address) + await this._updateAddress(discoveryResult.address, true) } async onDiscoveryAddressChanged(discoveryResult: DiscoveryResultMAC): Promise { - await this.setSettings({ 'discovered_address': discoveryResult.address }) - this._updateAddress(discoveryResult.address, true) + await this._updateDiscoveredAddress(discoveryResult.address) + await this._updateAddress(discoveryResult.address, true) } async onSettings({ newSettings }: { newSettings: { [key: string]: any } }): Promise { @@ -66,23 +66,17 @@ class UponorThermostatDevice extends Device { return success } - async _init(): Promise { - // TODO: implement dynamic capability system like the Remeha app - this.registerCapabilityListener('target_temperature', this._setTargetTemperature.bind(this)) + private async _updateDiscoveredAddress(newAddress: string): Promise { + if (newAddress.length === 0) return + await this.setSettings({ 'discovered_address': newAddress }) + } + async _init(): Promise { const address = this._getAddress() if (!address) return this.setUnavailable('No IP address configured') - - try { - const client = new UponorHTTPClient(address) - const canConnect = await client.testConnection() - if (!canConnect) return this.setUnavailable(`Could not connect to Uponor controller on IP address ${address}`) - this._client = client - this._syncInterval = setInterval(this._syncAttributes.bind(this), POLL_INTERVAL_MS) - setTimeout(this._syncAttributes.bind(this), 2000) - } catch (error) { - this.setUnavailable(`Error connecting to Uponor controller: ${error}`) - } + this._client = new UponorHTTPClient(address) + this._syncInterval = setInterval(this._sync.bind(this), POLL_INTERVAL_MS) + setTimeout(this._sync.bind(this), 2000) } async _uninit(): Promise { @@ -91,8 +85,19 @@ class UponorThermostatDevice extends Device { this._client = undefined } + private async _sync(): Promise { + await this._syncCapabilities() + await this._syncAttributes() + } + + private async _syncCapabilities(): Promise { + this.registerCapabilityListener('target_temperature', this._setTargetTemperature.bind(this)) + } + private async _syncAttributes(): Promise { if (!this._client) return this.setUnavailable('No Uponor client') + const canConnect = await this._client.testConnection() + if (!canConnect) return this.setUnavailable('Could not connect to Uponor controller on local network') try { await this._client.syncAttributes()