UNPKG

@homebridge-plugins/homebridge-govee

Version:

Homebridge plugin to integrate Govee devices into HomeKit.

188 lines (161 loc) 6.32 kB
import { base64ToHex, getTwoItemPosition, hexToTwoItems, parseError, } from '../utils/functions.js' import platformLang from '../utils/lang-en.js' export default class { constructor(platform, accessory) { // Set up variables from the platform this.hapChar = platform.api.hap.Characteristic this.hapErr = platform.api.hap.HapStatusError this.hapServ = platform.api.hap.Service this.platform = platform // Set up variables from the accessory this.accessory = accessory // Speed codes this.value2Code = { 1: 'MwUBAQAAAAAAAAAAAAAAAAAAADY=', 2: 'MwUBAgAAAAAAAAAAAAAAAAAAADU=', 3: 'MwUBAwAAAAAAAAAAAAAAAAAAADQ=', 4: 'MwUBBAAAAAAAAAAAAAAAAAAAADM=', 5: 'MwUBBQAAAAAAAAAAAAAAAAAAADI=', 6: 'MwUBBgAAAAAAAAAAAAAAAAAAADE=', 7: 'MwUBBwAAAAAAAAAAAAAAAAAAADA=', 8: 'MwUBCAAAAAAAAAAAAAAAAAAAAD8=', } // Migrate old %-rotation speed to unitless const existingFanService = this.accessory.getService(this.hapServ.Fan) if (existingFanService) { if (existingFanService.getCharacteristic(this.hapChar.RotationSpeed).props.unit === 'percentage') { this.accessory.removeService(existingFanService) } } // Add the fan service if it doesn't already exist this.service = this.accessory.getService(this.hapServ.Fan) || this.accessory.addService(this.hapServ.Fan) // Add the set handler to the fan on/off characteristic this.service .getCharacteristic(this.hapChar.On) .onSet(async value => this.internalStateUpdate(value)) this.cacheState = this.service.getCharacteristic(this.hapChar.On).value ? 'on' : 'off' // Add the set handler to the fan rotation speed characteristic this.service .getCharacteristic(this.hapChar.RotationSpeed) .setProps({ maxValue: 8, minStep: 1, minValue: 0, unit: 'unitless', }) .onSet(async value => this.internalSpeedUpdate(value)) this.cacheSpeed = this.service.getCharacteristic(this.hapChar.RotationSpeed).value // Output the customised options to the log const opts = JSON.stringify({}) platform.log('[%s] %s %s.', accessory.displayName, platformLang.devInitOpts, opts) } async internalStateUpdate(value) { try { const newValue = value ? 'on' : 'off' // Don't continue if the new value is the same as before if (this.cacheState === newValue) { return } // Send the request to the platform sender function await this.platform.sendDeviceUpdate(this.accessory, { cmd: 'stateHumi', value: value ? 1 : 0, }) // Cache the new state and log if appropriate this.cacheState = newValue this.accessory.log(`${platformLang.curState} [${this.cacheState}]`) } catch (err) { // Catch any errors during the process this.accessory.logWarn(`${platformLang.devNotUpdated} ${parseError(err)}`) // Throw a 'no response' error and set a timeout to revert this after 2 seconds setTimeout(() => { this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on') }, 2000) throw new this.hapErr(-70402) } } async internalSpeedUpdate(value) { try { // Don't continue if the speed is 0 if (value === 0) { return } // Don't continue if the speed value won't have effect if (value === this.cacheSpeed) { return } // Get the scene code for this value const newCode = this.value2Code[value] // Send the request to the platform sender function await this.platform.sendDeviceUpdate(this.accessory, { cmd: 'ptReal', value: newCode, openApi: this.accessory.context.openApiCapabilities?.workMode ? { instance: 'workMode', capabilityType: 'devices.capabilities.work_mode', value: { workMode: 1, modeValue: value } } : undefined, }) // Cache the new state and log if appropriate this.cacheSpeed = value this.accessory.log(`${platformLang.curSpeed} [${value}]`) } catch (err) { // Catch any errors during the process this.accessory.logWarn(`${platformLang.devNotUpdated} ${parseError(err)}`) // Throw a 'no response' error and set a timeout to revert this after 2 seconds setTimeout(() => { this.service.updateCharacteristic(this.hapChar.RotationSpeed, this.cacheSpeed) }, 2000) throw new this.hapErr(-70402) } } externalUpdate(params) { // Check for an ON/OFF change if (params.state && params.state !== this.cacheState) { this.cacheState = params.state this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on') // Log the change this.accessory.log(`${platformLang.curState} [${this.cacheState}]`) } // Handle OpenAPI workMode (speed) if (params.workMode) { const speed = params.workMode.modeValue if (speed >= 1 && speed <= 8) { this.cacheSpeed = speed this.service.updateCharacteristic(this.hapChar.RotationSpeed, this.cacheSpeed) } } // Check for some other scene/mode change (params.commands || []).forEach((command) => { const hexString = base64ToHex(command) const hexParts = hexToTwoItems(hexString) // Return now if not a device query update code if (getTwoItemPosition(hexParts, 1) !== 'aa') { return } const deviceFunction = `${getTwoItemPosition(hexParts, 2)}${getTwoItemPosition(hexParts, 3)}` switch (deviceFunction) { case '0501': case '0502': { // Speed update const newSpeedRaw = getTwoItemPosition(hexParts, 4) const newSpeedValue = Number.parseInt(newSpeedRaw, 16) if (newSpeedValue >= 1 && newSpeedValue <= 8) { if (newSpeedValue !== this.cacheSpeed) { this.cacheSpeed = newSpeedValue this.service.updateCharacteristic(this.hapChar.RotationSpeed, this.cacheSpeed) this.accessory.log(`${platformLang.curSpeed} [${this.cacheSpeed}]`) } } break } default: this.accessory.logDebugWarn(`${platformLang.newScene}: [${command}] [${hexString}]`) break } }) } }