Skip to content

Commit 31eb14b

Browse files
committed
checks for supported commands
1 parent 0ef261f commit 31eb14b

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/govee.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,20 @@ class Govee {
105105
}
106106
if ((accessory = this.devicesInHB.get(device.device))) {
107107
accessory.context.controllable = device.controllable
108+
accessory.context.retrievable = device.retrievable
108109
accessory.context.supportedCmds = device.supportCmds
109110
const res = await this.httpClient.getDevice(accessory.context)
110-
this.refreshAccessory(accessory, Object.assign({}, ...res))
111+
if (accessory.context.retrievable) {
112+
this.refreshAccessory(accessory, Object.assign({}, ...res))
113+
} else {
114+
throw new Error('requesting status is unsupported for the ' + device.model)
115+
}
111116
} else {
112-
this.log.warn("[%s] could not be initialised as it wasn't found in Homebridge.", device.deviceName)
117+
throw new Error("it wasn't found in Homebridge.")
113118
}
114119
} catch (err) {
115120
if (this.debug) {
116-
let str = err.message
117-
if (str === 'Request failed with status code 400') str += '. This error can often be safely ignored if device control is working as expected'
118-
this.log.warn('[%s] could not be updated as %s.', device.deviceName, str)
121+
this.log.warn('[%s] could not be updated as %s.', device.deviceName, err.message)
119122
}
120123
}
121124
}
@@ -209,6 +212,12 @@ class Govee {
209212
async internalOnOffUpdate (accessory, value, callback) {
210213
callback()
211214
try {
215+
if (!(
216+
accessory.context.controllable &&
217+
accessory.context.supportedCmds.includes('turn')
218+
)) {
219+
throw new Error('model [' + accessory.context.gvModel + '] does not support command [turn]')
220+
}
212221
await this.httpClient.updateDevice(accessory.context, {
213222
name: 'turn',
214223
value: value ? 'on' : 'off'
@@ -218,13 +227,19 @@ class Govee {
218227
}
219228
this.ignoreNextSync = true
220229
} catch (err) {
221-
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: \n%s.', accessory.displayName, err)
230+
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: %s.', accessory.displayName, err.message)
222231
}
223232
}
224233

225234
async internalBrightnessUpdate (accessory, value, callback) {
226235
callback()
227236
try {
237+
if (!(
238+
accessory.context.controllable &&
239+
accessory.context.supportedCmds.includes('brightness')
240+
)) {
241+
throw new Error('model [' + accessory.context.gvModel + '] does not support command [brightness]')
242+
}
228243
await this.httpClient.updateDevice(accessory.context, {
229244
name: 'brightness',
230245
value: value
@@ -234,13 +249,19 @@ class Govee {
234249
}
235250
this.ignoreNextSync = true
236251
} catch (err) {
237-
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: \n%s.', accessory.displayName, err)
252+
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: %s.', accessory.displayName, err.message)
238253
}
239254
}
240255

241256
async internalColourUpdate (accessory, value, callback) {
242257
callback()
243258
try {
259+
if (!(
260+
accessory.context.controllable &&
261+
accessory.context.supportedCmds.includes('color')
262+
)) {
263+
throw new Error('model [' + accessory.context.gvModel + '] does not support command [color]')
264+
}
244265
const lightService = accessory.getService(Service.Lightbulb)
245266
const curSat = lightService.getCharacteristic(Characteristic.Saturation).value
246267
const newRGB = cConvert.hsv.rgb(value, curSat, 100)
@@ -258,7 +279,7 @@ class Govee {
258279
}
259280
this.ignoreNextSync = true
260281
} catch (err) {
261-
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: \n%s.', accessory.displayName, err)
282+
this.log.warn('[%s] could not be updated and its status will be reverted soon. Error: %s.', accessory.displayName, err.message)
262283
}
263284
}
264285
}

0 commit comments

Comments
 (0)