@homebridge/plugin-ui-utils 1.0.3 → 1.0.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/DEVELOPMENT.md +10 -11
- package/README.md +128 -127
- package/eslint.config.js +27 -0
- package/package.json +27 -26
- package/tsconfig.ui.json +2 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -7
- package/dist/index.js.map +0 -1
- package/dist/server.d.ts +0 -96
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -184
- package/dist/server.js.map +0 -1
- package/dist/ui.d.ts +0 -62
- package/dist/ui.d.ts.map +0 -1
- package/dist/ui.interface.d.ts +0 -307
- package/dist/ui.interface.d.ts.map +0 -1
- package/dist/ui.interface.js +0 -3
- package/dist/ui.interface.js.map +0 -1
- package/dist/ui.js +0 -299
- package/dist/ui.js.map +0 -1
- package/dist/ui.mock.d.ts +0 -37
- package/dist/ui.mock.d.ts.map +0 -1
- package/dist/ui.mock.js +0 -101
- package/dist/ui.mock.js.map +0 -1
package/CHANGELOG.md
CHANGED
package/DEVELOPMENT.md
CHANGED
|
@@ -104,12 +104,12 @@ Here is a simple example using Jest:
|
|
|
104
104
|
import { MockHomebridgePluginUi } from '@homebridge/plugin-ui-utils/dist/ui.mock'
|
|
105
105
|
|
|
106
106
|
describe('TestCustomUi', () => {
|
|
107
|
-
let homebridge: MockHomebridgePluginUi
|
|
107
|
+
let homebridge: MockHomebridgePluginUi
|
|
108
108
|
|
|
109
109
|
beforeEach(() => {
|
|
110
|
-
homebridge = new MockHomebridgePluginUi()
|
|
111
|
-
window.homebridge = homebridge
|
|
112
|
-
})
|
|
110
|
+
homebridge = new MockHomebridgePluginUi()
|
|
111
|
+
window.homebridge = homebridge
|
|
112
|
+
})
|
|
113
113
|
|
|
114
114
|
it('should return the plugin config and schema when called', async () => {
|
|
115
115
|
// mock the config
|
|
@@ -117,17 +117,16 @@ describe('TestCustomUi', () => {
|
|
|
117
117
|
{
|
|
118
118
|
platform: 'homebridge-example'
|
|
119
119
|
}
|
|
120
|
-
]
|
|
120
|
+
]
|
|
121
121
|
|
|
122
122
|
// mock the schema
|
|
123
123
|
homebridge.mockPluginSchema = {
|
|
124
124
|
pluginAlias: 'homebridge-example',
|
|
125
125
|
pluginType: 'platform'
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
expect(await window.homebridge.getPluginConfig()).toHaveLength(1);
|
|
129
|
-
expect(await window.homebridge.getPluginConfigSchema()).toHaveProperty('pluginAlias');
|
|
130
|
-
});
|
|
126
|
+
}
|
|
131
127
|
|
|
132
|
-
|
|
128
|
+
expect(await window.homebridge.getPluginConfig()).toHaveLength(1)
|
|
129
|
+
expect(await window.homebridge.getPluginConfigSchema()).toHaveProperty('pluginAlias')
|
|
130
|
+
})
|
|
131
|
+
})
|
|
133
132
|
```
|
package/README.md
CHANGED
|
@@ -14,21 +14,21 @@
|
|
|
14
14
|
The package assists plugin developers creating fully customisable configuration user interfaces for their plugins.
|
|
15
15
|
|
|
16
16
|
- [Implementation](#implementation)
|
|
17
|
-
|
|
17
|
+
- [Project Layout](#project-layout)
|
|
18
18
|
- [User Interface API](#user-interface-api)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
- [Config](#config)
|
|
20
|
+
- [Requests](#requests)
|
|
21
|
+
- [Toast Notifications](#toast-notifications)
|
|
22
|
+
- [Modal](#modal)
|
|
23
|
+
- [Forms](#forms)
|
|
24
|
+
- [Events](#events)
|
|
25
|
+
- [Plugin / Server Information](#plugin--server-information)
|
|
26
26
|
- [Server API](#server-api)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
- [Setup](#setup)
|
|
28
|
+
- [Request Handling](#request-handling)
|
|
29
|
+
- [Request Error Handling](#request-error-handling)
|
|
30
|
+
- [Push Events](#push-events)
|
|
31
|
+
- [Server Information](#server-information)
|
|
32
32
|
- [Examples](#examples)
|
|
33
33
|
- [Development](#development)
|
|
34
34
|
|
|
@@ -36,17 +36,17 @@ The package assists plugin developers creating fully customisable configuration
|
|
|
36
36
|
|
|
37
37
|
A plugin's custom user interface has two main components:
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
- [User Interface](#user-interface-api) - this is the HTML / CSS / JavaScript code the users interact with
|
|
40
|
+
- [Server](#server-api) - this is an optional server side script that provides endpoints the UI can call
|
|
41
41
|
|
|
42
42
|
### Project Layout
|
|
43
43
|
|
|
44
44
|
A custom UI should be published under a directory named `homebridge-ui`:
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
- `homebridge-ui/public/index.html` - required - this is the plugin UI entry point.
|
|
47
|
+
- `homebridge-ui/public/` - you can store any other assets (`.css`, `.js`, images etc.) in the public folder.
|
|
48
|
+
- `homebridge-ui/server.js` - optional - this is the server side script containing API endpoints for your plugin UI.
|
|
49
|
+
- `config.schema.json` - required - set `customUi` to `true` in the schema to enable custom UI.
|
|
50
50
|
|
|
51
51
|
Basic structure example:
|
|
52
52
|
|
|
@@ -64,7 +64,7 @@ You may customise the location of the `homebridge-ui` by setting the `customUiPa
|
|
|
64
64
|
|
|
65
65
|
## User Interface API
|
|
66
66
|
|
|
67
|
-
A plugin's custom user interface is displayed inside an iframe in the settings modal, in place of the schema-generated form.
|
|
67
|
+
A plugin's custom user interface is displayed inside an iframe in the settings modal, in place of the schema-generated form.
|
|
68
68
|
|
|
69
69
|
The user interface API is provided to the plugin's custom UI via the `window.homebridge` object. This is injected into the plugin's custom UI during render.
|
|
70
70
|
|
|
@@ -73,11 +73,12 @@ The user interface API is provided to the plugin's custom UI via the `window.hom
|
|
|
73
73
|
</p>
|
|
74
74
|
|
|
75
75
|
Note:
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
|
|
77
|
+
- Developers are free to use front end frameworks such as Angular, Vue, or React to create the plugin's custom user interface.
|
|
78
|
+
- Developers should make use [Bootstrap 4](https://getbootstrap.com/docs) CSS classes, as these will automatically be styled and themed correctly. There is no need to include the boostrap css yourself, this will be injected by the Homebridge UI during render.
|
|
79
|
+
- As the user interface is displayed in an isolated iframe, you can safely use any custom JavaScript and CSS.
|
|
80
|
+
- The `index.html` file should not include `<html>`, `<head>`, or `<body>` tags, as these are added by the Homebridge UI during the render process.
|
|
81
|
+
- You may include external assets in your HTML.
|
|
81
82
|
|
|
82
83
|
Example `index.html`:
|
|
83
84
|
|
|
@@ -114,7 +115,7 @@ Returns a promise that resolves an array of accessory or platform config blocks
|
|
|
114
115
|
An empty array will be returned if the plugin is not currently configured.
|
|
115
116
|
|
|
116
117
|
```ts
|
|
117
|
-
const pluginConfigBlocks = await homebridge.getPluginConfig()
|
|
118
|
+
const pluginConfigBlocks = await homebridge.getPluginConfig()
|
|
118
119
|
// [{ platform: 'ExamplePlatform', name: 'example' }]
|
|
119
120
|
```
|
|
120
121
|
|
|
@@ -124,7 +125,7 @@ const pluginConfigBlocks = await homebridge.getPluginConfig();
|
|
|
124
125
|
|
|
125
126
|
Update the plugin config.
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
- `pluginConfig`: A full array of platform and accessory config blocks.
|
|
128
129
|
|
|
129
130
|
This should be called whenever a change to the config is made.
|
|
130
131
|
|
|
@@ -144,14 +145,14 @@ const pluginConfig = [
|
|
|
144
145
|
}
|
|
145
146
|
]
|
|
146
147
|
|
|
147
|
-
await homebridge.updatePluginConfig(pluginConfig)
|
|
148
|
+
await homebridge.updatePluginConfig(pluginConfig)
|
|
148
149
|
```
|
|
149
150
|
|
|
150
151
|
#### `homebridge.savePluginConfig`
|
|
151
152
|
|
|
152
153
|
> `homebridge.savePluginConfig(): Promise<void>`
|
|
153
154
|
|
|
154
|
-
Saves the plugin config changes to the Homebridge `config.json`. This is the equivalent of clicking the
|
|
155
|
+
Saves the plugin config changes to the Homebridge `config.json`. This is the equivalent of clicking the _Save_ button.
|
|
155
156
|
|
|
156
157
|
This should be used sparingly, for example, after a access token is generated.
|
|
157
158
|
|
|
@@ -159,10 +160,10 @@ You must call `await homebridge.updatePluginConfig()` first.
|
|
|
159
160
|
|
|
160
161
|
```ts
|
|
161
162
|
// update config first!
|
|
162
|
-
await homebridge.updatePluginConfig(pluginConfig)
|
|
163
|
+
await homebridge.updatePluginConfig(pluginConfig)
|
|
163
164
|
|
|
164
165
|
// save config
|
|
165
|
-
await homebridge.savePluginConfig()
|
|
166
|
+
await homebridge.savePluginConfig()
|
|
166
167
|
```
|
|
167
168
|
|
|
168
169
|
#### `homebridge.getPluginConfigSchema`
|
|
@@ -172,7 +173,7 @@ await homebridge.savePluginConfig();
|
|
|
172
173
|
Returns the plugin's config.schema.json.
|
|
173
174
|
|
|
174
175
|
```ts
|
|
175
|
-
const schema = await homebridge.getPluginConfigSchema()
|
|
176
|
+
const schema = await homebridge.getPluginConfigSchema()
|
|
176
177
|
```
|
|
177
178
|
|
|
178
179
|
#### `homebridge.getCachedAccessories`
|
|
@@ -182,7 +183,7 @@ const schema = await homebridge.getPluginConfigSchema();
|
|
|
182
183
|
Returns the any cached accessories for the plugin
|
|
183
184
|
|
|
184
185
|
```ts
|
|
185
|
-
const cachedAccessories = await homebridge.getCachedAccessories()
|
|
186
|
+
const cachedAccessories = await homebridge.getCachedAccessories()
|
|
186
187
|
```
|
|
187
188
|
|
|
188
189
|
### Requests
|
|
@@ -195,16 +196,16 @@ This allows the custom UI to make API requests to their `server.js` script.
|
|
|
195
196
|
|
|
196
197
|
Make a request to the plugin's server side script.
|
|
197
198
|
|
|
198
|
-
|
|
199
|
-
|
|
199
|
+
- `path`: the path handler on the server that the request should be sent to
|
|
200
|
+
- `body`: an optional payload
|
|
200
201
|
|
|
201
202
|
Returns a promise with the response from the server.
|
|
202
203
|
|
|
203
204
|
User Interface Example:
|
|
204
205
|
|
|
205
206
|
```ts
|
|
206
|
-
const response = await homebridge.request('/hello', { who: 'world' })
|
|
207
|
-
console.log(response)
|
|
207
|
+
const response = await homebridge.request('/hello', { who: 'world' })
|
|
208
|
+
console.log(response) // the response from the server
|
|
208
209
|
```
|
|
209
210
|
|
|
210
211
|
The corresponding code in the `server.js` file would look like this:
|
|
@@ -213,8 +214,8 @@ The corresponding code in the `server.js` file would look like this:
|
|
|
213
214
|
// server side request handler
|
|
214
215
|
this.onRequest('/hello', async (payload) => {
|
|
215
216
|
console.log(payload) // the payload sent from the UI
|
|
216
|
-
return { hello: 'user' }
|
|
217
|
-
})
|
|
217
|
+
return { hello: 'user' }
|
|
218
|
+
})
|
|
218
219
|
```
|
|
219
220
|
|
|
220
221
|
### Toast Notifications
|
|
@@ -231,8 +232,8 @@ Toast notifications are the pop-up notifications displayed in the bottom right c
|
|
|
231
232
|
|
|
232
233
|
Shows a green "success" notification.
|
|
233
234
|
|
|
234
|
-
|
|
235
|
-
|
|
235
|
+
- `message`: the toast content
|
|
236
|
+
- `title`: an optional title
|
|
236
237
|
|
|
237
238
|
#### `homebridge.toast.error`
|
|
238
239
|
|
|
@@ -240,8 +241,8 @@ Shows a green "success" notification.
|
|
|
240
241
|
|
|
241
242
|
Shows a red "error" notification.
|
|
242
243
|
|
|
243
|
-
|
|
244
|
-
|
|
244
|
+
- `message`: the toast content
|
|
245
|
+
- `title`: an optional title
|
|
245
246
|
|
|
246
247
|
#### `homebridge.toast.warning`
|
|
247
248
|
|
|
@@ -249,8 +250,8 @@ Shows a red "error" notification.
|
|
|
249
250
|
|
|
250
251
|
Shows an amber "warning" notification.
|
|
251
252
|
|
|
252
|
-
|
|
253
|
-
|
|
253
|
+
- `message`: the toast content
|
|
254
|
+
- `title`: an optional title
|
|
254
255
|
|
|
255
256
|
#### `homebridge.toast.info`
|
|
256
257
|
|
|
@@ -258,8 +259,8 @@ Shows an amber "warning" notification.
|
|
|
258
259
|
|
|
259
260
|
Shows a blue "info" notification.
|
|
260
261
|
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
- `message`: the toast content
|
|
263
|
+
- `title`: an optional title
|
|
263
264
|
|
|
264
265
|
### Modal
|
|
265
266
|
|
|
@@ -272,7 +273,7 @@ Close the settings modal.
|
|
|
272
273
|
This action does not save any config changes.
|
|
273
274
|
|
|
274
275
|
```ts
|
|
275
|
-
homebridge.closeSettings()
|
|
276
|
+
homebridge.closeSettings()
|
|
276
277
|
```
|
|
277
278
|
|
|
278
279
|
#### `homebridge.showSpinner`
|
|
@@ -283,13 +284,13 @@ Displays a spinner / loading overlay, preventing user input until cleared with `
|
|
|
283
284
|
|
|
284
285
|
```ts
|
|
285
286
|
// show the spinner overlay
|
|
286
|
-
homebridge.showSpinner()
|
|
287
|
+
homebridge.showSpinner()
|
|
287
288
|
|
|
288
289
|
// wait for the request to process
|
|
289
|
-
await homebridge.request('/hello')
|
|
290
|
+
await homebridge.request('/hello')
|
|
290
291
|
|
|
291
292
|
// hide the spinner overlay
|
|
292
|
-
homebridge.hideSpinner()
|
|
293
|
+
homebridge.hideSpinner()
|
|
293
294
|
```
|
|
294
295
|
|
|
295
296
|
#### `homebridge.hideSpinner`
|
|
@@ -299,7 +300,7 @@ homebridge.hideSpinner();
|
|
|
299
300
|
Hide the spinner / loading overlay.
|
|
300
301
|
|
|
301
302
|
```ts
|
|
302
|
-
homebridge.hideSpinner()
|
|
303
|
+
homebridge.hideSpinner()
|
|
303
304
|
```
|
|
304
305
|
|
|
305
306
|
### Forms
|
|
@@ -307,13 +308,13 @@ homebridge.hideSpinner();
|
|
|
307
308
|
The custom user interface allows you to create two types of forms:
|
|
308
309
|
|
|
309
310
|
1. A form based on your plugin's `config.schema.json` file
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
311
|
+
- User input is automatically mapped to the plugin config object
|
|
312
|
+
- You can listen for change events from your custom user interface
|
|
313
|
+
- The schema must contain all config options
|
|
313
314
|
2. A standalone form
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
315
|
+
- Not linked to your `config.schema.json` form in any way
|
|
316
|
+
- You must listen for change events, process the event, and update the plugin config
|
|
317
|
+
- The form does not need to include all config options
|
|
317
318
|
|
|
318
319
|
Developers are also able to create their own forms using HTML.
|
|
319
320
|
|
|
@@ -325,15 +326,15 @@ Show the schema-generated form below the custom user interface.
|
|
|
325
326
|
This feature only works for platform plugins that have set `singular` = `true` in their config.schema.json file.
|
|
326
327
|
|
|
327
328
|
```ts
|
|
328
|
-
homebridge.showSchemaForm()
|
|
329
|
+
homebridge.showSchemaForm()
|
|
329
330
|
```
|
|
330
331
|
|
|
331
332
|
When enabling the schema form, you should listen for the `configChanged` event to keep your config in sync. This event is triggered whenever the user makes a change in the schema-generated form (250ms debounce).
|
|
332
333
|
|
|
333
334
|
```ts
|
|
334
335
|
window.homebridge.addEventListener('configChanged', (event: MessageEvent) => {
|
|
335
|
-
console.log('Updated config:', event.data)
|
|
336
|
-
})
|
|
336
|
+
console.log('Updated config:', event.data)
|
|
337
|
+
})
|
|
337
338
|
```
|
|
338
339
|
|
|
339
340
|
#### `homebridge.hideSchemaForm`
|
|
@@ -343,7 +344,7 @@ window.homebridge.addEventListener('configChanged', (event: MessageEvent) => {
|
|
|
343
344
|
Hides the schema-generated form.
|
|
344
345
|
|
|
345
346
|
```ts
|
|
346
|
-
homebridge.hideSchemaForm()
|
|
347
|
+
homebridge.hideSchemaForm()
|
|
347
348
|
```
|
|
348
349
|
|
|
349
350
|
#### `homebridge.createForm`
|
|
@@ -354,52 +355,52 @@ Create a new standalone form. You may pass in an arbitrary schema using the same
|
|
|
354
355
|
|
|
355
356
|
Only one standalone form can be displayed at a time. The main config-schema based form cannot be shown while a standalone form is being displayed.
|
|
356
357
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
358
|
+
- `schema`: The [form schema object](https://developers.homebridge.io/#/config-schema), may also contain layout metadata
|
|
359
|
+
- `data`: The initial form data
|
|
360
|
+
- `submitButton`: String. Optional label for a submit button, if not provided, no submit button will be displayed
|
|
361
|
+
- `cancelButton`: String. Optional label for a cancel button, if not provided, no cancel button will be displayed
|
|
361
362
|
|
|
362
363
|
Example:
|
|
363
364
|
|
|
364
365
|
```ts
|
|
365
366
|
// create the form
|
|
366
367
|
const myForm = homebridge.createForm(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
368
|
+
{
|
|
369
|
+
schema: {
|
|
370
|
+
type: 'object',
|
|
371
|
+
properties: {
|
|
372
|
+
name: {
|
|
373
|
+
title: 'Name',
|
|
374
|
+
type: 'string',
|
|
375
|
+
required: true,
|
|
376
376
|
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
layout: null,
|
|
380
|
+
form: null,
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
name: 'initial name value'
|
|
384
|
+
}
|
|
385
|
+
)
|
|
385
386
|
|
|
386
387
|
// watch for change events
|
|
387
388
|
myForm.onChange((change) => {
|
|
388
|
-
console.log(change)
|
|
389
|
-
})
|
|
389
|
+
console.log(change)
|
|
390
|
+
})
|
|
390
391
|
|
|
391
392
|
// watch for submit button click events
|
|
392
393
|
myForm.onSubmit((form) => {
|
|
393
|
-
console.log(form)
|
|
394
|
-
})
|
|
394
|
+
console.log(form)
|
|
395
|
+
})
|
|
395
396
|
|
|
396
397
|
// watch for cancel button click events
|
|
397
398
|
myForm.onCancel((form) => {
|
|
398
|
-
console.log(form)
|
|
399
|
-
})
|
|
399
|
+
console.log(form)
|
|
400
|
+
})
|
|
400
401
|
|
|
401
402
|
// stop listening to change events and hide the form
|
|
402
|
-
myForm.end()
|
|
403
|
+
myForm.end()
|
|
403
404
|
```
|
|
404
405
|
|
|
405
406
|
### Events
|
|
@@ -413,7 +414,7 @@ Called when the Homebridge UI has completed rendering the plugin's custom UI.
|
|
|
413
414
|
```ts
|
|
414
415
|
homebridge.addEventListener('ready', () => {
|
|
415
416
|
// do something with event
|
|
416
|
-
})
|
|
417
|
+
})
|
|
417
418
|
```
|
|
418
419
|
|
|
419
420
|
#### Custom Events
|
|
@@ -424,14 +425,14 @@ UI Example:
|
|
|
424
425
|
|
|
425
426
|
```ts
|
|
426
427
|
homebridge.addEventListener('my-event', (event) => {
|
|
427
|
-
console.log(event.data)
|
|
428
|
-
})
|
|
428
|
+
console.log(event.data) // the event payload from the server
|
|
429
|
+
})
|
|
429
430
|
```
|
|
430
431
|
|
|
431
432
|
The corresponding code in the `server.js` file would look like this:
|
|
432
433
|
|
|
433
434
|
```ts
|
|
434
|
-
this.pushEvent('my-event', { some: 'data' })
|
|
435
|
+
this.pushEvent('my-event', { some: 'data' })
|
|
435
436
|
```
|
|
436
437
|
|
|
437
438
|
### Plugin / Server Information
|
|
@@ -440,7 +441,7 @@ this.pushEvent('my-event', { some: 'data' });
|
|
|
440
441
|
|
|
441
442
|
> `homebridge.plugin`
|
|
442
443
|
|
|
443
|
-
Is an object that contains plugin metadata.
|
|
444
|
+
Is an object that contains plugin metadata.
|
|
444
445
|
|
|
445
446
|
```ts
|
|
446
447
|
{
|
|
@@ -467,8 +468,8 @@ Is an object containing some server metadata
|
|
|
467
468
|
```ts
|
|
468
469
|
{
|
|
469
470
|
env: {
|
|
470
|
-
platform: string
|
|
471
|
-
nodeVersion: string
|
|
471
|
+
platform: string // darwin, win32, linux, freebsd etc.
|
|
472
|
+
nodeVersion: string // Node.js version
|
|
472
473
|
}
|
|
473
474
|
}
|
|
474
475
|
```
|
|
@@ -477,17 +478,18 @@ Is an object containing some server metadata
|
|
|
477
478
|
|
|
478
479
|
To provide server API endpoints that can be called from the custom UI, a plugin must place a `server.js` file in the `homebridge-ui` directory.
|
|
479
480
|
|
|
480
|
-
You will need to include the
|
|
481
|
+
You will need to include the `@homebridge/plugin-ui-utils` library as a prod dependency:
|
|
481
482
|
|
|
482
483
|
```
|
|
483
484
|
npm install --save @homebridge/plugin-ui-utils
|
|
484
485
|
```
|
|
485
486
|
|
|
486
487
|
Note:
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
488
|
+
|
|
489
|
+
- This `server.js` script will be spawned as a child process when the plugin's settings modal is opened, and is terminated when the settings modal is closed.
|
|
490
|
+
- The `server.js` script must create a new instance of a class that extends `HomebridgePluginUiServer` from the `@homebridge/plugin-ui-utils` library.
|
|
491
|
+
- This file will be spawned as a child process when the plugin's settings modal is opened, and is terminated when the settings modal is closed.
|
|
492
|
+
- The server side script must extend the class provided by the `@homebridge/plugin-ui-utils` library.
|
|
491
493
|
|
|
492
494
|
Example `server.js`:
|
|
493
495
|
|
|
@@ -496,7 +498,7 @@ const { HomebridgePluginUiServer } = require('@homebridge/plugin-ui-utils');
|
|
|
496
498
|
|
|
497
499
|
// your class MUST extend the HomebridgePluginUiServer
|
|
498
500
|
class UiServer extends HomebridgePluginUiServer {
|
|
499
|
-
constructor () {
|
|
501
|
+
constructor () {
|
|
500
502
|
// super must be called first
|
|
501
503
|
super();
|
|
502
504
|
|
|
@@ -531,7 +533,7 @@ class UiServer extends HomebridgePluginUiServer {
|
|
|
531
533
|
Let the UI know the server is ready to accept requests.
|
|
532
534
|
|
|
533
535
|
```ts
|
|
534
|
-
this.ready()
|
|
536
|
+
this.ready()
|
|
535
537
|
```
|
|
536
538
|
|
|
537
539
|
### Request Handling
|
|
@@ -542,8 +544,8 @@ this.ready();
|
|
|
542
544
|
|
|
543
545
|
Handle requests sent from the UI to the given path.
|
|
544
546
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
+
- `path`: the request path name
|
|
548
|
+
- `fn`: a function to handle the incoming requests
|
|
547
549
|
|
|
548
550
|
The value returned/resolved from the request handler function will be sent back to the UI as the request response.
|
|
549
551
|
|
|
@@ -553,16 +555,16 @@ Example creating a request handler on the server:
|
|
|
553
555
|
// server side code
|
|
554
556
|
this.onRequest('/hello', async (payload) => {
|
|
555
557
|
console.log(payload) // the payload sent from the UI
|
|
556
|
-
return { hello: 'user' }
|
|
557
|
-
})
|
|
558
|
+
return { hello: 'user' }
|
|
559
|
+
})
|
|
558
560
|
```
|
|
559
561
|
|
|
560
562
|
The corresponding call in the UI to send requests to this endpoint:
|
|
561
563
|
|
|
562
564
|
```ts
|
|
563
565
|
// ui code
|
|
564
|
-
const response = await homebridge.request('/hello', { who: 'world' })
|
|
565
|
-
console.log(response)
|
|
566
|
+
const response = await homebridge.request('/hello', { who: 'world' })
|
|
567
|
+
console.log(response) // the response from the server
|
|
566
568
|
```
|
|
567
569
|
|
|
568
570
|
### Request Error Handling
|
|
@@ -573,22 +575,22 @@ Example:
|
|
|
573
575
|
|
|
574
576
|
```ts
|
|
575
577
|
// server side code
|
|
576
|
-
const { RequestError } = require('@homebridge/plugin-ui-utils')
|
|
578
|
+
const { RequestError } = require('@homebridge/plugin-ui-utils')
|
|
577
579
|
|
|
578
580
|
this.onRequest('/hello', async (payload) => {
|
|
579
581
|
// something went wrong, throw a RequestError:
|
|
580
|
-
throw new RequestError('Something went wrong!', { status: 404 })
|
|
581
|
-
})
|
|
582
|
+
throw new RequestError('Something went wrong!', { status: 404 })
|
|
583
|
+
})
|
|
582
584
|
```
|
|
583
585
|
|
|
584
586
|
You can then catch this in the UI:
|
|
585
587
|
|
|
586
588
|
```ts
|
|
587
589
|
try {
|
|
588
|
-
await homebridge.request('/hello', { who: 'world' })
|
|
590
|
+
await homebridge.request('/hello', { who: 'world' })
|
|
589
591
|
} catch (e) {
|
|
590
|
-
console.log(e.message)
|
|
591
|
-
console.log(e.error)
|
|
592
|
+
console.log(e.message) // 'Something went wrong!'
|
|
593
|
+
console.log(e.error) // { status: 404 }
|
|
592
594
|
}
|
|
593
595
|
```
|
|
594
596
|
|
|
@@ -602,21 +604,21 @@ Uncaught errors in event handlers, or errors thrown using `new Error` will still
|
|
|
602
604
|
|
|
603
605
|
Push events allow you to send data to the UI, without needed the UI to request it first.
|
|
604
606
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
+
- `event`: a string to describe the event type
|
|
608
|
+
- `data`: any data to send as an event payload to the UI.
|
|
607
609
|
|
|
608
610
|
Example pushing an event payload to the UI:
|
|
609
611
|
|
|
610
612
|
```ts
|
|
611
|
-
this.pushEvent('my-event', { some: 'data' })
|
|
613
|
+
this.pushEvent('my-event', { some: 'data' })
|
|
612
614
|
```
|
|
613
615
|
|
|
614
616
|
The corresponding code to watch for the event in the UI:
|
|
615
617
|
|
|
616
618
|
```ts
|
|
617
619
|
homebridge.addEventListener('my-event', (event) => {
|
|
618
|
-
console.log(event.data)
|
|
619
|
-
})
|
|
620
|
+
console.log(event.data) // the event payload from the server
|
|
621
|
+
})
|
|
620
622
|
```
|
|
621
623
|
|
|
622
624
|
### Server Information
|
|
@@ -628,7 +630,7 @@ homebridge.addEventListener('my-event', (event) => {
|
|
|
628
630
|
Returns the Homebridge instance's current storage path.
|
|
629
631
|
|
|
630
632
|
```ts
|
|
631
|
-
const storagePath = this.homebridgeStoragePath
|
|
633
|
+
const storagePath = this.homebridgeStoragePath
|
|
632
634
|
```
|
|
633
635
|
|
|
634
636
|
#### `this.homebridgeConfigPath`
|
|
@@ -638,7 +640,7 @@ const storagePath = this.homebridgeStoragePath;
|
|
|
638
640
|
Returns the path to the Homebridge `config.json` file:
|
|
639
641
|
|
|
640
642
|
```ts
|
|
641
|
-
const configPath = this.homebridgeConfigPath
|
|
643
|
+
const configPath = this.homebridgeConfigPath
|
|
642
644
|
```
|
|
643
645
|
|
|
644
646
|
#### `this.homebridgeUiVersion`
|
|
@@ -648,14 +650,13 @@ const configPath = this.homebridgeConfigPath;
|
|
|
648
650
|
Returns the version of the Homebridge UI:
|
|
649
651
|
|
|
650
652
|
```ts
|
|
651
|
-
const uiVersion = this.homebridgeUiVersion
|
|
653
|
+
const uiVersion = this.homebridgeUiVersion
|
|
652
654
|
```
|
|
653
655
|
|
|
654
656
|
## Examples
|
|
655
657
|
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
658
|
+
- [Basic Example](./examples/basic-ui-server) - demos a minimal custom user interface, interacting with server side scripts, updating the plugin config, and using toast notifications.
|
|
659
|
+
- [Push Events](./examples/push-events) - demos how to send push events from the server, and listen for them in the custom user interface.
|
|
659
660
|
|
|
660
661
|
A full list of plugins that have implemented the custom user interface can be found [here](https://www.npmjs.com/package/@homebridge/plugin-ui-utils?activeTab=dependents).
|
|
661
662
|
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import antfu from '@antfu/eslint-config'
|
|
2
|
+
|
|
3
|
+
export default antfu({
|
|
4
|
+
ignores: ['dist', 'README.md', 'DEVELOPMENT.md'],
|
|
5
|
+
rules: {
|
|
6
|
+
'new-cap': 'off',
|
|
7
|
+
'import/extensions': ['error', 'ignorePackages'],
|
|
8
|
+
'import/order': 'off',
|
|
9
|
+
'jsdoc/check-alignment': 'error',
|
|
10
|
+
'jsdoc/check-line-alignment': 'error',
|
|
11
|
+
'no-undef': 'error',
|
|
12
|
+
'perfectionist/sort-exports': 'error',
|
|
13
|
+
'perfectionist/sort-named-exports': 'error',
|
|
14
|
+
'perfectionist/sort-named-imports': 'error',
|
|
15
|
+
'quotes': ['error', 'single'],
|
|
16
|
+
'sort-imports': 'off',
|
|
17
|
+
'style/brace-style': ['error', '1tbs'],
|
|
18
|
+
'style/quote-props': ['error', 'consistent-as-needed'],
|
|
19
|
+
'test/no-only-tests': 'error',
|
|
20
|
+
'unicorn/no-useless-spread': 'error',
|
|
21
|
+
'unused-imports/no-unused-vars': ['error', { caughtErrors: 'none' }],
|
|
22
|
+
},
|
|
23
|
+
typescript: true,
|
|
24
|
+
formatters: {
|
|
25
|
+
markdown: true,
|
|
26
|
+
},
|
|
27
|
+
})
|