-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { CharacteristicValue, Logger, PlatformAccessory, Service } from 'homebridge'; | ||
import fetch from 'node-fetch'; // I am, in fact, trying to make fetch happen. | ||
|
||
import { BlaQHomebridgePluginPlatform } from '../platform.js'; | ||
import { | ||
BlaQButtonEvent, | ||
BlaQTextSensorEvent, | ||
} from '../types.js'; | ||
import { LogMessageEvent, StateUpdateMessageEvent, StateUpdateRecord } from '../utils/eventsource.js'; | ||
import { BaseBlaQAccessory } from './base.js'; | ||
|
||
const correctAPIBaseURL = (inputURL: string) => { | ||
let correctedAPIBaseURL = inputURL; | ||
if(!correctedAPIBaseURL.includes('://')){ | ||
correctedAPIBaseURL = `http://${correctedAPIBaseURL}`; | ||
} | ||
if(correctedAPIBaseURL.endsWith('/')){ | ||
correctedAPIBaseURL = correctedAPIBaseURL.slice(0, -1); | ||
} | ||
return correctedAPIBaseURL; | ||
}; | ||
|
||
type BlaQGarageLearnModeAccessoryConstructorParams = { | ||
platform: BlaQHomebridgePluginPlatform; | ||
accessory: PlatformAccessory; | ||
model: string; | ||
serialNumber: string; | ||
apiBaseURL: string; | ||
}; | ||
|
||
/** | ||
* Platform Accessory | ||
* An instance of this class is created for each accessory your platform registers | ||
* Each accessory may expose multiple services of different service types. | ||
*/ | ||
export class BlaQGarageLearnModeAccessory implements BaseBlaQAccessory { | ||
private logger: Logger; | ||
private accessoryInformationService: Service; | ||
private switchService: Service; | ||
private apiBaseURL: string; | ||
private firmwareVersion?: string; | ||
private isOn?: boolean; | ||
private readonly platform: BlaQHomebridgePluginPlatform; | ||
private readonly accessory: PlatformAccessory; | ||
private readonly model: string; | ||
private readonly serialNumber: string; | ||
|
||
constructor({ | ||
platform, | ||
accessory, | ||
model, | ||
serialNumber, | ||
apiBaseURL, | ||
}: BlaQGarageLearnModeAccessoryConstructorParams) { | ||
this.platform = platform; | ||
this.logger = this.platform.logger; | ||
this.logger.debug('Initializing BlaQGarageLearnModeAccessory...'); | ||
this.accessory = accessory; | ||
this.model = model; | ||
this.serialNumber = serialNumber; | ||
this.apiBaseURL = correctAPIBaseURL(apiBaseURL); | ||
this.switchService = this.accessory.getService(this.platform.service.Switch) | ||
|| this.accessory.addService(this.platform.service.Switch); | ||
|
||
this.accessoryInformationService = this.accessory.getService(this.platform.service.AccessoryInformation) | ||
|| this.accessory.addService(this.platform.service.AccessoryInformation); | ||
|
||
// set accessory information | ||
this.accessoryInformationService | ||
.setCharacteristic(this.platform.characteristic.Manufacturer, 'Konnected') | ||
.setCharacteristic(this.platform.characteristic.Model, this.model) | ||
.setCharacteristic(this.platform.characteristic.SerialNumber, this.serialNumber); | ||
|
||
// Set the service name. This is what is displayed as the name on the Home | ||
// app. We use what we stored in `accessory.context` in `discoverDevices`. | ||
this.switchService.setCharacteristic(this.platform.characteristic.Name, accessory.context.device.displayName); | ||
|
||
this.switchService.getCharacteristic(this.platform.characteristic.On) | ||
.onGet(this.getIsOn.bind(this)) | ||
.onSet(this.changeIsOn.bind(this)); | ||
|
||
// Publish firmware version; this may not be initialized yet, so we set a getter. | ||
// Note that this is against the AccessoryInformation service, not the GDO service. | ||
this.accessoryInformationService | ||
.getCharacteristic(this.platform.characteristic.FirmwareRevision) | ||
.onGet(this.getFirmwareVersion.bind(this)); | ||
this.logger.debug('Initialized BlaQGarageLearnModeAccessory!'); | ||
} | ||
|
||
getFirmwareVersion(): CharacteristicValue { | ||
return this.firmwareVersion || ''; | ||
} | ||
|
||
private setFirmwareVersion(version: string) { | ||
this.firmwareVersion = version; | ||
this.accessoryInformationService.setCharacteristic( | ||
this.platform.characteristic.FirmwareRevision, | ||
version, | ||
); | ||
} | ||
|
||
getIsOn(): CharacteristicValue { | ||
return this.isOn || false; | ||
} | ||
|
||
setIsOn(isOn: boolean) { | ||
this.isOn = isOn; | ||
this.switchService.setCharacteristic( | ||
this.platform.characteristic.On, | ||
this.isOn, | ||
); | ||
} | ||
|
||
private async changeIsOn(target: CharacteristicValue){ | ||
const apiTarget: string = target ? 'turn_on' : 'turn_off'; | ||
if(target !== this.isOn){ // only call the API when target = true (button on) | ||
await fetch(`${this.apiBaseURL}/switch/learn/${apiTarget}`, {method: 'POST'}); | ||
} | ||
} | ||
|
||
setAPIBaseURL(url: string){ | ||
this.apiBaseURL = correctAPIBaseURL(url); | ||
} | ||
|
||
handleStateEvent(stateEvent: StateUpdateMessageEvent){ | ||
this.logger.debug('Processing state event:', stateEvent.data); | ||
try { | ||
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; | ||
if (['switch-learn'].includes(stateInfo.id)) { | ||
const buttonEvent = stateInfo as BlaQButtonEvent & { state: 'ON' | 'OFF' }; | ||
if(['OFF', 'ON'].includes(buttonEvent.state.toUpperCase())){ | ||
this.setIsOn(buttonEvent.state.toUpperCase() === 'ON'); | ||
} | ||
} else if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) { | ||
const b = stateInfo as BlaQTextSensorEvent; | ||
if (b.value === b.state && b.value !== '' && b.value !== null && b.value !== undefined) { | ||
this.logger.info('Firmware version:', b.value); | ||
this.setFirmwareVersion(b.value); | ||
} else { | ||
this.logger.error('Mismatched firmware versions in value/state:', b.value, b.state); | ||
this.firmwareVersion = undefined; | ||
} | ||
} | ||
} catch(e) { | ||
this.logger.error('Cannot deserialize message:', stateEvent); | ||
this.logger.error('Deserialization yielded:', e); | ||
} | ||
} | ||
|
||
handleLogEvent(logEvent: LogMessageEvent){ | ||
this.logger.debug('BlaQ log:', logEvent.data); | ||
try { | ||
const logStr = logEvent.data; | ||
const lowercaseLogStr = logStr.toLowerCase(); | ||
const learnTurningOn = | ||
lowercaseLogStr.includes('learn') && | ||
lowercaseLogStr.includes('turning') && | ||
lowercaseLogStr.includes('on'); | ||
const learnStateOn = | ||
lowercaseLogStr.includes('learn') && | ||
lowercaseLogStr.includes('state') && | ||
lowercaseLogStr.includes('on'); | ||
const learnTurningOff = | ||
lowercaseLogStr.includes('learn') && | ||
lowercaseLogStr.includes('turning') && | ||
lowercaseLogStr.includes('off'); | ||
const learnStateOff = | ||
lowercaseLogStr.includes('learn') && | ||
lowercaseLogStr.includes('state') && | ||
lowercaseLogStr.includes('off'); | ||
if (learnTurningOn || learnStateOn) { | ||
this.setIsOn(true); | ||
} else if (learnTurningOff || learnStateOff) { | ||
this.setIsOn(false); | ||
} | ||
} catch(e) { | ||
this.logger.error('Log parsing error:', e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters