homebridge-blaq
Version:
Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware
137 lines • 5.18 kB
JavaScript
import fetch from 'node-fetch'; // I am, in fact, trying to make fetch happen.
export const correctAPIBaseURL = (inputURL) => {
let correctedAPIBaseURL = inputURL;
if (!correctedAPIBaseURL.includes('://')) {
correctedAPIBaseURL = `http://${correctedAPIBaseURL}`;
}
if (correctedAPIBaseURL.endsWith('/')) {
correctedAPIBaseURL = correctedAPIBaseURL.slice(0, -1);
}
return correctedAPIBaseURL;
};
export class BaseBlaQAccessory {
apiBaseURL;
user;
pass;
firmwareVersion;
synced;
queuedEvents = [];
accessory;
accessoryInformationService;
logger;
friendlyName;
platform;
serialNumber;
constructor({ accessory, apiBaseURL, apiUser, apiPass, friendlyName, platform, serialNumber, }) {
this.platform = platform;
this.logger = this.platform.logger;
this.logger.debug(`Initializing ${this.getSelfClassName()}...`);
this.accessory = accessory;
this.friendlyName = friendlyName;
this.serialNumber = serialNumber;
this.apiBaseURL = correctAPIBaseURL(apiBaseURL);
this.user = apiUser;
this.pass = apiPass;
this.accessoryInformationService = this.getOrAddService(this.platform.service.AccessoryInformation);
// set accessory information
this.accessoryInformationService
.setCharacteristic(this.platform.characteristic.Manufacturer, 'Konnected')
.setCharacteristic(this.platform.characteristic.Model, 'GDO blaQ')
.setCharacteristic(this.platform.characteristic.SerialNumber, this.serialNumber)
.setCharacteristic(this.platform.characteristic.Name, this.friendlyName);
// 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));
}
getSelfClassName() {
return this.constructor.name;
}
getOrAddService(service) {
return this.accessory.getService(service) ||
this.accessory.addService(service);
}
removeService(service) {
const foundService = this.accessory.getService(service);
if (foundService) {
this.accessory.removeService(foundService);
}
}
processQueuedEvents() {
while (this.queuedEvents.length) {
const event = this.queuedEvents.shift();
const funcToCall = {
'ping': this.handlePingEvent?.bind(this),
'log': this.handleLogEvent?.bind(this),
'state': this.handleStateEvent?.bind(this),
}[event.type];
if (funcToCall) {
funcToCall(event.event);
}
}
}
resetSyncState() {
this.synced = false;
}
handlePingEvent(pingEvent) {
if (!this.synced) {
this.queuedEvents.push({ type: 'ping', event: pingEvent });
}
}
handleLogEvent(logEvent) {
if (!this.synced) {
this.queuedEvents.push({ type: 'log', event: logEvent });
}
}
handleStateEvent(stateEvent) {
try {
const stateInfo = JSON.parse(stateEvent.data);
if (['binary_sensor-synced'].includes(stateInfo.id)) {
this.synced = stateInfo.value;
if (this.synced) {
this.processQueuedEvents();
}
}
else if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) {
const b = stateInfo;
if (b.value && b.value === b.state) {
this.setFirmwareVersion(b.value);
}
else {
this.logger.error('Mismatched firmware versions in value/state:', b.value, b.state);
this.firmwareVersion = undefined;
}
}
else if (!this.synced) {
this.queuedEvents.push({ type: 'state', event: stateEvent });
}
}
catch (e) {
this.logger.error('Cannot deserialize message:', stateEvent);
this.logger.error('Deserialization yielded:', e);
}
}
getFirmwareVersion() {
return this.firmwareVersion || '';
}
setFirmwareVersion(version) {
this.firmwareVersion = version;
this.accessoryInformationService.setCharacteristic(this.platform.characteristic.FirmwareRevision, version);
}
setAPIBaseURL(url) {
this.apiBaseURL = correctAPIBaseURL(url);
}
authFetch(url, init) {
const newInit = init || {};
const basicCreds = `${this.user}:${this.pass}`;
newInit['headers'] = {
...newInit['headers'],
...(this.user && this.pass ? {
'Authorization': `Basic ${Buffer.from(basicCreds).toString('base64')}`,
} : {}),
};
return fetch(url, newInit);
}
}
//# sourceMappingURL=base.js.map