Skip to content

Commit

Permalink
Update Gateway.js
Browse files Browse the repository at this point in the history
Change initialisation logic, see #36.
  • Loading branch information
ebaauw committed Apr 18, 2022
1 parent fcebe07 commit 11e7b52
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions lib/DeconzAccessory/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Gateway extends homebridgeLib.AccessoryDelegate {
this.heartbeatEnabled = true
this
.on('identify', this.identify)
.once('heartbeat', this.init)
.once('heartbeat', (beat) => { this.initialBeat = beat })
.on('heartbeat', this.heartbeat)
.on('shutdown', this.shutdown)
}
Expand Down Expand Up @@ -216,28 +216,24 @@ class Gateway extends homebridgeLib.AccessoryDelegate {
}
}

/** Initialise the gateway delegate.
*/
async init (beat) {
try {
this.debug('initialising...')
this.initialBeat = beat
await this.connect()
this.initialised = true
this.debug('initialised')
this.emit('initialised')
} catch (error) { this.error(error) }
}

/** Update properties from gateway announcement.
* @param {string} host - The gateway hostname or IP address and port.
* @param {Object} config - The response body of an unauthenticated
* GET `/config` (from {@link DeconzDiscovery#config config()}.
*/
found (host, config) {
this.values.host = host
this.context.config = config
this.values.software = config.swversion
async found (host, config) {
try {
this.context.host = host
this.values.host = host
this.context.config = config
this.values.software = config.swversion
if (!this.initialised) {
this.debug('initialising...')
await this.connect()
}
} catch (error) {
this.error(error)
}
}

async shutdown () {
Expand Down Expand Up @@ -418,8 +414,8 @@ class Gateway extends homebridgeLib.AccessoryDelegate {
for (const id in this.exposeErrorById) {
this.resetExposeError(id)
}
this.context.fullState = null
this.pollNext = true
this.pollFullState = true
} catch (error) {
if (
error instanceof Deconz.ApiError && error.type === 101 && retry < 8
Expand Down Expand Up @@ -684,9 +680,11 @@ class Gateway extends homebridgeLib.AccessoryDelegate {
try {
this.polling = true
this.vdebug('%spolling...', this.pollNext ? 'priority ' : '')
if (this.context.fullState == null) {
this.context.fullState = await this.client.get('/')
this.context.fullState.groups[0] = await this.client.get('/groups/0')
if (this.context.fullState == null || this.pollFullState) {
const fullState = await this.client.get('/')
fullState.groups[0] = await this.client.get('/groups/0')
this.context.fullState = fullState
this.pollFullState = false
} else {
const config = await this.client.get('/config')
if (config.bridgeid === this.id && config.UTC == null) {
Expand Down Expand Up @@ -718,6 +716,11 @@ class Gateway extends homebridgeLib.AccessoryDelegate {
this.vdebug('polling done')
this.pollNext = false
this.polling = false
if (!this.initialised) {
this.initialised = true
this.debug('initialised')
this.emit('initialised')
}
}
}

Expand Down

0 comments on commit 11e7b52

Please sign in to comment.