Skip to content

Commit 2f1a8a3

Browse files
committed
Lazy load history and eve characteristics
1 parent 3878d8a commit 2f1a8a3

5 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/accessory/abstract/mqtt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ export abstract class MQTTAccessory<C extends MQTTAccessoryConfig> extends Commo
160160
}
161161

162162
protected recordHistory(type: HistoryType, entry: HistoryEntry, updateLastActivation: boolean = false): boolean {
163-
return this.dependency.history.record(this, this.config.history, type, entry, updateLastActivation);
163+
return this.dependency.history?.record(this, this.config.history, type, entry, updateLastActivation) === true;
164164
}
165165
}

src/homebridge/platform.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export class HomebridgeEasyMQTT implements DynamicPlatformPlugin {
4848
api.isMatterEnabled?.() === true ? 'Enabled' : 'Disabled',
4949
);
5050

51-
initEveCharacteristics(api);
52-
5351
this.api.on('didFinishLaunching', () => {
5452
this.setup();
5553
});
@@ -88,12 +86,6 @@ export class HomebridgeEasyMQTT implements DynamicPlatformPlugin {
8886

8987
const groups = new Map<string, BaseAccessoryConfig[]>();
9088

91-
const Service = this.api.hap.Service;
92-
const Characteristic = this.api.hap.Characteristic;
93-
const HapStatusError = this.api.hap.HapStatusError;
94-
95-
const history = new History(this.api, this.log);
96-
9789
for (const accessoryConfig of this.config.accessories) {
9890

9991
if (!assert(this.log, PLATFORM_NAME, accessoryConfig, 'info') ||
@@ -107,6 +99,8 @@ export class HomebridgeEasyMQTT implements DynamicPlatformPlugin {
10799

108100
if (accessoryConfig.info.protocol === Protocol.HomeKit) {
109101

102+
initEveCharacteristics(this.api);
103+
110104
const groupName = accessoryConfig.info.group;
111105
if (groupName !== undefined) {
112106

@@ -121,12 +115,17 @@ export class HomebridgeEasyMQTT implements DynamicPlatformPlugin {
121115
const uuid = this.api.hap.uuid.generate(id);
122116

123117
const homekitAccessory = this.createHomeKitAccessory(accessoryConfig.info.name, uuid);
118+
119+
const Service = this.api.hap.Service;
120+
const Characteristic = this.api.hap.Characteristic;
121+
const HapStatusError = this.api.hap.HapStatusError;
122+
124123
const dependency: AccessoryDependency = {
125-
protocol: Protocol.HomeKit,
124+
protocol: accessoryConfig.info.protocol,
126125
getHomeKit: () => ({ Service, Characteristic, HapStatusError, accessory: homekitAccessory }),
127126
getMatter: () => undefined,
128127
log: this.log,
129-
history: history,
128+
history: History.instance(this.api, this.log),
130129
};
131130

132131
const accessory = createAccessory(dependency, accessoryConfig);
@@ -165,12 +164,17 @@ export class HomebridgeEasyMQTT implements DynamicPlatformPlugin {
165164

166165
const uuid = this.api.hap.uuid.generate(groupName);
167166
const homekitAccessory = this.createHomeKitAccessory(groupName, uuid);
167+
168+
const Service = this.api.hap.Service;
169+
const Characteristic = this.api.hap.Characteristic;
170+
const HapStatusError = this.api.hap.HapStatusError;
171+
168172
const dependency: AccessoryDependency = {
169173
protocol: Protocol.HomeKit,
170174
getHomeKit: () => ({ Service, Characteristic, HapStatusError, accessory: homekitAccessory }),
171175
getMatter: () => undefined,
172176
log: this.log,
173-
history: history,
177+
history: History.instance(this.api, this.log),
174178
};
175179

176180
const configs = groups.get(groupName)!;

src/model/eve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let _EveHomeKitTypes: EveHomeKitTypes | undefined;
2222
export function initEveCharacteristics(api: API) {
2323

2424
if (_EveHomeKitTypes) {
25-
throw new Error('EveHomeKitTypes already initialized');
25+
return;
2626
}
2727

2828
_EveHomeKitTypes = new EveHomeKitTypes(api);

src/model/history.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ function HistoryService(type: HistoryType, accessory: HomeKitAccessory, options?
5555

5656
export class History {
5757

58+
private static _instance: History | undefined;
59+
public static instance(api: API, log: Log) : History {
60+
if (History._instance === undefined) {
61+
History._instance = new History(api, log);
62+
}
63+
return History._instance;
64+
}
65+
5866
private readonly historyServices = new Map<string, HistoryService>();
5967
private readonly persistPath: string;
6068

6169
private readonly cleanedUp = new Set<string>();
6270

63-
constructor(private readonly api: API, private readonly log: Log) {
71+
private constructor(private readonly api: API, private readonly log: Log) {
6472

6573
if (ServiceProvider) {
6674
throw new Error('HistoryServiceProvider already initialized');

src/model/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type AccessoryDependency = {
2424
getHomeKit: GetHomeKit;
2525
getMatter: GetMatter,
2626
log: Log,
27-
history: History,
27+
history?: History,
2828
}
2929

3030
export type MQTTAccessoryDependency<C extends MQTTAccessoryConfig> = AccessoryDependency & {

0 commit comments

Comments
 (0)