UNPKG

12.1 kBTypeScriptView Raw
1import { EventEmitter } from "events";
2import * as hapNodeJs from "hap-nodejs";
3import { Controller, Service } from "hap-nodejs";
4import { AccessoryConfig, PlatformConfig } from "./bridgeService";
5import { Logging } from "./logger";
6import { PlatformAccessory } from "./platformAccessory";
7import { User } from "./user";
8export type HAP = typeof hapNodeJs;
9export type HAPLegacyTypes = typeof hapNodeJs.LegacyTypes;
10export type PluginIdentifier = PluginName | ScopedPluginName;
11export type PluginName = string;
12export type ScopedPluginName = string;
13export type AccessoryName = string;
14export type PlatformName = string;
15export type AccessoryIdentifier = string;
16export type PlatformIdentifier = string;
17export declare const enum PluginType {
18 ACCESSORY = "accessory",
19 PLATFORM = "platform"
20}
21/**
22 * The {PluginInitializer} is a method which must be the default export for every homebridge plugin.
23 * It is called once the plugin is loaded from disk.
24 */
25export interface PluginInitializer {
26 /**
27 * When the initializer is called the plugin must use the provided api instance and call the appropriate
28 * register methods - {@link API.registerAccessory} or {@link API.registerPlatform} - in order to
29 * correctly register for the following startup sequence.
30 *
31 * @param {API} api
32 */
33 (api: API): void | Promise<void>;
34}
35export interface AccessoryPluginConstructor {
36 new (logger: Logging, config: AccessoryConfig, api: API): AccessoryPlugin;
37}
38export interface AccessoryPlugin {
39 /**
40 * Optional method which will be called if a 'identify' of an Accessory is requested by HomeKit.
41 */
42 identify?(): void;
43 /**
44 * This method will be called once on startup, to query all services to be exposed by the Accessory.
45 * All event handlers for characteristics should be set up before the array is returned.
46 *
47 * @returns {Service[]} services - returned services will be added to the Accessory
48 */
49 getServices(): Service[];
50 /**
51 * This method will be called once on startup, to query all controllers to be exposed by the Accessory.
52 * It is optional to implement.
53 *
54 * This includes controllers like the RemoteController or the CameraController.
55 * Any necessary controller specific setup should have been done when returning the array.
56 * In most cases the plugin will only return an array of the size 1.
57 *
58 * In the case that the Plugin does not add any additional services (returned by {@link getServices}) the
59 * method {@link getServices} must be defined in any way and should just return an empty array.
60 *
61 * @returns {Controller[]} controllers - returned controllers will be configured for the Accessory
62 */
63 getControllers?(): Controller[];
64}
65export interface PlatformPluginConstructor<Config extends PlatformConfig = PlatformConfig> {
66 new (logger: Logging, config: Config, api: API): DynamicPlatformPlugin | StaticPlatformPlugin | IndependentPlatformPlugin;
67}
68export interface PlatformPlugin {
69}
70/**
71 * Platform that is able to dynamically add or remove accessories.
72 * All configured accessories are stored to disk and recreated on startup.
73 * Accessories can be added or removed by using {@link API.registerPlatformAccessories} or {@link API.unregisterPlatformAccessories}.
74 */
75export interface DynamicPlatformPlugin extends PlatformPlugin {
76 /**
77 * This method is called for every PlatformAccessory, which is recreated from disk on startup.
78 * It should be used to properly initialize the Accessory and setup all event handlers for
79 * all services and their characteristics.
80 *
81 * @param {PlatformAccessory} accessory which needs to be configured
82 */
83 configureAccessory(accessory: PlatformAccessory): void;
84}
85/**
86 * Platform that exposes all available characteristics at the start of the plugin.
87 * The set of accessories can not change at runtime.
88 * The bridge waits for all callbacks to return before it is published and accessible by HomeKit controllers.
89 */
90export interface StaticPlatformPlugin extends PlatformPlugin {
91 /**
92 * This method is called once at startup. The Platform should pass all accessories which need to be created
93 * to the callback in form of a {@link AccessoryPlugin}.
94 * The Platform must respond in a timely manner as otherwise the startup of the bridge would be unnecessarily delayed.
95 *
96 * @param {(foundAccessories: AccessoryPlugin[]) => void} callback
97 */
98 accessories(callback: (foundAccessories: AccessoryPlugin[]) => void): void;
99}
100/**
101 * Platform that does not aim to add any accessories to the main bridge accessory.
102 * This platform should be used if for example a plugin aims to only expose external accessories.
103 * It should also be used when the platform doesn't intend to expose any accessories at all, like plugins
104 * providing a UI for homebridge.
105 */
106export interface IndependentPlatformPlugin extends PlatformPlugin {
107}
108export declare const enum APIEvent {
109 /**
110 * Event is fired once homebridge has finished with booting up and initializing all components and plugins.
111 * When this event is fired it is possible that the Bridge accessory isn't published yet, if homebridge still needs
112 * to wait for some {@see StaticPlatformPlugin | StaticPlatformPlugins} to finish accessory creation.
113 */
114 DID_FINISH_LAUNCHING = "didFinishLaunching",
115 /**
116 * This event is fired when homebridge gets shutdown. This could be a regular shutdown or an unexpected crash.
117 * At this stage all Accessories are already unpublished and all PlatformAccessories are already saved to disk!
118 */
119 SHUTDOWN = "shutdown"
120}
121export declare const enum InternalAPIEvent {
122 REGISTER_ACCESSORY = "registerAccessory",
123 REGISTER_PLATFORM = "registerPlatform",
124 PUBLISH_EXTERNAL_ACCESSORIES = "publishExternalAccessories",
125 REGISTER_PLATFORM_ACCESSORIES = "registerPlatformAccessories",
126 UPDATE_PLATFORM_ACCESSORIES = "updatePlatformAccessories",
127 UNREGISTER_PLATFORM_ACCESSORIES = "unregisterPlatformAccessories"
128}
129export interface API {
130 /**
131 * The homebridge API version as a floating point number.
132 */
133 readonly version: number;
134 /**
135 * The current homebridge semver version.
136 */
137 readonly serverVersion: string;
138 readonly user: typeof User;
139 readonly hap: HAP;
140 readonly hapLegacyTypes: HAPLegacyTypes;
141 readonly platformAccessory: typeof PlatformAccessory;
142 /**
143 * Returns true if the current running homebridge version is greater or equal to the
144 * passed version string.
145 *
146 * Example:
147 *
148 * We assume the homebridge version 1.3.0-beta.12 ({@link serverVersion}) and the following example calls below
149 * ```
150 * versionGreaterOrEqual("1.2.0"); // will return true
151 * versionGreaterOrEqual("1.3.0"); // will return false (the RELEASE version 1.3.0 is bigger than the BETA version 1.3.0-beta.12)
152 * versionGreaterOrEqual("1.3.0-beta.8); // will return true
153 * ```
154 *
155 * @param version
156 */
157 versionGreaterOrEqual(version: string): boolean;
158 registerAccessory(accessoryName: AccessoryName, constructor: AccessoryPluginConstructor): void;
159 registerAccessory(pluginIdentifier: PluginIdentifier, accessoryName: AccessoryName, constructor: AccessoryPluginConstructor): void;
160 registerPlatform<Config extends PlatformConfig>(platformName: PlatformName, constructor: PlatformPluginConstructor<Config>): void;
161 registerPlatform<Config extends PlatformConfig>(pluginIdentifier: PluginIdentifier, platformName: PlatformName, constructor: PlatformPluginConstructor<Config>): void;
162 registerPlatformAccessories(pluginIdentifier: PluginIdentifier, platformName: PlatformName, accessories: PlatformAccessory[]): void;
163 updatePlatformAccessories(accessories: PlatformAccessory[]): void;
164 unregisterPlatformAccessories(pluginIdentifier: PluginIdentifier, platformName: PlatformName, accessories: PlatformAccessory[]): void;
165 /**
166 * @deprecated use {@link publishExternalAccessories} directly to publish a standalone Accessory
167 */
168 publishCameraAccessories(pluginIdentifier: PluginIdentifier, accessories: PlatformAccessory[]): void;
169 publishExternalAccessories(pluginIdentifier: PluginIdentifier, accessories: PlatformAccessory[]): void;
170 on(event: "didFinishLaunching", listener: () => void): this;
171 on(event: "shutdown", listener: () => void): this;
172}
173export declare interface HomebridgeAPI {
174 on(event: "didFinishLaunching", listener: () => void): this;
175 on(event: "shutdown", listener: () => void): this;
176 on(event: InternalAPIEvent.REGISTER_ACCESSORY, listener: (accessoryName: AccessoryName, accessoryConstructor: AccessoryPluginConstructor, pluginIdentifier?: PluginIdentifier) => void): this;
177 on(event: InternalAPIEvent.REGISTER_PLATFORM, listener: (platformName: PlatformName, platformConstructor: PlatformPluginConstructor, pluginIdentifier?: PluginIdentifier) => void): this;
178 on(event: InternalAPIEvent.PUBLISH_EXTERNAL_ACCESSORIES, listener: (accessories: PlatformAccessory[]) => void): this;
179 on(event: InternalAPIEvent.REGISTER_PLATFORM_ACCESSORIES, listener: (accessories: PlatformAccessory[]) => void): this;
180 on(event: InternalAPIEvent.UPDATE_PLATFORM_ACCESSORIES, listener: (accessories: PlatformAccessory[]) => void): this;
181 on(event: InternalAPIEvent.UNREGISTER_PLATFORM_ACCESSORIES, listener: (accessories: PlatformAccessory[]) => void): this;
182 emit(event: "didFinishLaunching"): boolean;
183 emit(event: "shutdown"): boolean;
184 emit(event: InternalAPIEvent.REGISTER_ACCESSORY, accessoryName: AccessoryName, accessoryConstructor: AccessoryPluginConstructor, pluginIdentifier?: PluginIdentifier): boolean;
185 emit(event: InternalAPIEvent.REGISTER_PLATFORM, platformName: PlatformName, platformConstructor: PlatformPluginConstructor, pluginIdentifier?: PluginIdentifier): boolean;
186 emit(event: InternalAPIEvent.PUBLISH_EXTERNAL_ACCESSORIES, accessories: PlatformAccessory[]): boolean;
187 emit(event: InternalAPIEvent.REGISTER_PLATFORM_ACCESSORIES, accessories: PlatformAccessory[]): boolean;
188 emit(event: InternalAPIEvent.UPDATE_PLATFORM_ACCESSORIES, accessories: PlatformAccessory[]): boolean;
189 emit(event: InternalAPIEvent.UNREGISTER_PLATFORM_ACCESSORIES, accessories: PlatformAccessory[]): boolean;
190}
191export declare class HomebridgeAPI extends EventEmitter implements API {
192 readonly version = 2.7;
193 readonly serverVersion: string;
194 readonly user: typeof User;
195 readonly hap: typeof hapNodeJs;
196 readonly hapLegacyTypes: typeof hapNodeJs.LegacyTypes;
197 readonly platformAccessory: typeof PlatformAccessory;
198 constructor();
199 versionGreaterOrEqual(version: string): boolean;
200 static isDynamicPlatformPlugin(platformPlugin: PlatformPlugin): platformPlugin is DynamicPlatformPlugin;
201 static isStaticPlatformPlugin(platformPlugin: PlatformPlugin): platformPlugin is StaticPlatformPlugin;
202 signalFinished(): void;
203 signalShutdown(): void;
204 registerAccessory(accessoryName: AccessoryName, constructor: AccessoryPluginConstructor): void;
205 registerAccessory(pluginIdentifier: PluginIdentifier, accessoryName: AccessoryName, constructor: AccessoryPluginConstructor): void;
206 registerPlatform(platformName: PlatformName, constructor: PlatformPluginConstructor): void;
207 registerPlatform(pluginIdentifier: PluginIdentifier, platformName: PlatformName, constructor: PlatformPluginConstructor): void;
208 publishCameraAccessories(pluginIdentifier: PluginIdentifier, accessories: PlatformAccessory[]): void;
209 publishExternalAccessories(pluginIdentifier: PluginIdentifier, accessories: PlatformAccessory[]): void;
210 registerPlatformAccessories(pluginIdentifier: PluginIdentifier, platformName: PlatformName, accessories: PlatformAccessory[]): void;
211 updatePlatformAccessories(accessories: PlatformAccessory[]): void;
212 unregisterPlatformAccessories(pluginIdentifier: PluginIdentifier, platformName: PlatformName, accessories: PlatformAccessory[]): void;
213}
214//# sourceMappingURL=api.d.ts.map
\No newline at end of file