@chazepps/homebridge-hejhome
Version:
The Hejhome plugin allows you to access your Hejhome device(s) from HomeKit.
136 lines • 5.51 kB
JavaScript
import { LedStripRgbw2, LightRgbw5, RelayController, SensorMo, SmartButton, ZigbeeSwitch1, ZigbeeSwitch2 } from './accessories/index.js';
import { getToken, hejAccessories, hejDevices, hejEvent, startRealtime } from './requests/index.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
export class HejhomePlatform {
log;
config;
api;
Service;
Characteristic;
accessories = new Map();
discoveredCacheUUIDs = [];
token = null;
tokenExpiry = null;
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.log.info('Initializing platform...');
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
this.api.on('didFinishLaunching', async () => {
await this.initializeToken();
await this.refreshToken();
await new Promise((r) => {
const interval = setInterval(() => {
if (this.token) {
clearInterval(interval);
r();
}
}, 100);
});
// Start real-time updates
await startRealtime(this);
// Discover and register devices
await this.discoverDevices();
// Device state update event handler
hejEvent.on('deviceUpdated', (device) => {
const uuid = this.api.hap.uuid.generate(device.id);
const accessory = this.accessories.get(uuid);
if (accessory) {
accessory.context.device = device;
hejAccessories[device.id]?.updateCharacteristics();
}
});
});
}
// Initialize token and set refresh interval
async initializeToken() {
setInterval(() => this.refreshToken(), 24 * 60 * 60 * 1000); // Refresh token every 24 hours
}
// Token refresh logic
async refreshToken() {
const token = await getToken(this);
if (!token) {
this.log.error('Failed to refresh token');
return;
}
try {
this.token = token;
this.tokenExpiry = Date.now() + 24 * 60 * 60 * 1000; // Set token expiry time (24 hours later)
this.log.info('Token refreshed successfully');
}
catch (error) {
this.log.error('Failed to refresh token:', error);
}
}
// Validate and refresh token if necessary
async getToken() {
if (!this.token || (this.tokenExpiry && Date.now() >= this.tokenExpiry)) {
await this.refreshToken();
}
return this.token;
}
// Load accessory from cache
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.set(accessory.UUID, accessory);
}
// Discover and register devices
async discoverDevices() {
for (const id in hejDevices) {
const device = hejDevices[id];
const uuid = this.api.hap.uuid.generate(device.id);
let existingAccessory = this.accessories.get(uuid);
if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
this.initHejAccessory(this, existingAccessory, device);
}
else {
this.log.info('Adding new accessory:', device.name);
const accessory = new this.api.platformAccessory(device.name, uuid);
this.initHejAccessory(this, accessory, device);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
this.discoveredCacheUUIDs.push(uuid);
}
// Remove accessories that are in cache but no longer exist
for (const [uuid, accessory] of this.accessories) {
if (!this.discoveredCacheUUIDs.includes(uuid)) {
this.log.info('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
// Initialize accessory based on device type
initHejAccessory(platform, accessory, device) {
let hejAccessory = null;
switch (device.deviceType) {
case 'ZigbeeSwitch1':
hejAccessory = new ZigbeeSwitch1(platform, accessory, device);
break;
case 'ZigbeeSwitch2':
hejAccessory = new ZigbeeSwitch2(platform, accessory, device);
break;
case 'LightRgbw5':
hejAccessory = new LightRgbw5(platform, accessory, device);
break;
case 'LedStripRgbw2':
hejAccessory = new LedStripRgbw2(platform, accessory, device);
break;
case 'SensorMo':
hejAccessory = new SensorMo(platform, accessory, device);
break;
case 'SmartButton':
hejAccessory = new SmartButton(platform, accessory, device);
break;
case 'RelayController':
hejAccessory = new RelayController(platform, accessory, device);
break;
}
if (hejAccessory) {
hejAccessories[device.id] = hejAccessory;
}
}
}
//# sourceMappingURL=platform.js.map