Skip to content

Commit

Permalink
Update govee.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 committed Oct 11, 2020
1 parent f6c46bf commit c4e5a12
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions lib/govee.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,14 @@ class Govee {
}
this.log('Plugin has finished initialising. Synching with Govee.')
this.httpClient = new GoveeHTTP(this.config, this.log)
await this.goveeDeviceSync(true)
await this.goveeSync(true)
this.log('[%s] devices loaded from your Govee account.', this.devicesInGV.size)
await this.goveeStatusSync()
await this.goveeSync()
this.refreshDevice = promInterval(
async () => {
if (this.refreshFlag) {
try {
await this.goveeDeviceSync(false)
} catch (err) {
if (this.debug) {
this.log.warn(err.message)
}
}
}
},
cns.refreshTime * 1000,
{ stopOnError: false }
)
this.refreshStatus = promInterval(
async () => {
if (this.refreshFlag) {
try {
await this.goveeStatusSync()
await this.goveeSync()
} catch (err) {
if (this.debug) {
this.log.warn(err.message)
Expand All @@ -79,7 +64,9 @@ class Govee {
this.log.warn('Invalid Govee API key - please double check it.')
this.log.warn('**********************************************')
} else {
this.log.warn('********** Cannot load homebridge-govee *********')
this.log.warn(err.message)
this.log.warn('*************************************************')
}
}
}
Expand All @@ -88,7 +75,7 @@ class Govee {
this.refreshFlag = false
}

async goveeDeviceSync () {
async goveeSync () {
const res = await this.httpClient.getDevices()
res.forEach(device => this.devicesInGV.set(device.device, device))
this.devicesInHB.forEach(a => {
Expand All @@ -98,9 +85,6 @@ class Govee {
}
})
this.devicesInGV.forEach(d => this.refreshDevice(d))
}

async goveeStatusSync () {
this.devicesInHB.forEach(async accessory => {
try {
if (!accessory.context.retrievable) throw new Error('current status could not be retrieved')
Expand All @@ -112,7 +96,7 @@ class Govee {
})
}

async refreshDevice (device) {
refreshDevice (device) {
try {
let accessory
if (cns.modelsLED.includes(device.model)) {
Expand All @@ -128,7 +112,7 @@ class Govee {
/**********
UNSUPPORTED
**********/
throw new Error("it isn't supported")
return
/*********/
}
accessory.context.controllable = device.controllable
Expand Down Expand Up @@ -157,7 +141,6 @@ class Govee {
}
this.devicesInHB.set(device.device, accessory)
this.api.registerPlatformAccessories('homebridge-govee', 'Govee', [accessory])
this.log(' → [%s] has been added to Homebridge.', device.deviceName)
return accessory
} catch (err) {
this.log.warn(' → [%s] could not be added to Homebridge as %s.', device.deviceName, err)
Expand All @@ -173,7 +156,6 @@ class Govee {
try {
this.devicesInHB.delete(accessory.context.gvDeviceId)
this.api.unregisterPlatformAccessories('homebridge-govee', 'Govee', [accessory])
this.log('[%s] has been removed from Homebridge.', accessory.displayName)
} catch (err) {
this.log.warn("[%s] needed to be removed but couldn't as %s.", accessory.displayName, err)
}
Expand Down

0 comments on commit c4e5a12

Please sign in to comment.