homebridge-blueair-plugin 1.1.0 → 1.2.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.
@@ -0,0 +1,122 @@
1
+ const { HomebridgePluginUiServer, RequestError } = require('@homebridge/plugin-ui-utils');
2
+ const { defaultConfig, defaultDeviceConfig } = require('../dist/utils.js');
3
+ const BlueAirAwsApi = require('../dist/api/BlueAirAwsApi.js').default;
4
+
5
+ var _ = require('lodash');
6
+
7
+ /*********************************************************************
8
+ * Logger
9
+ * Lightweight log class to mimic the homebridge log capability
10
+ */
11
+ class Logger {
12
+ _debug;
13
+ _Reset = '\x1b[0m';
14
+ _Bright = '\x1b[1m';
15
+ _Dim = '\x1b[2m';
16
+
17
+ _FgBlack = '\x1b[30m';
18
+ _FgRed = '\x1b[31m';
19
+ _FgGreen = '\x1b[32m';
20
+ _FgYellow = '\x1b[33m';
21
+ _FgBlue = '\x1b[34m';
22
+ _FgMagenta = '\x1b[35m';
23
+ _FgCyan = '\x1b[36m';
24
+ _FgWhite = '\x1b[37m';
25
+ _FgGray = '\x1b[90m';
26
+
27
+ constructor(uiDebug = false) {
28
+ this._debug = uiDebug;
29
+ }
30
+
31
+ info(str) {
32
+ console.info(this._FgWhite + str + this._Reset);
33
+ }
34
+
35
+ warn(str) {
36
+ console.warn(this._FgYellow + str + this._Reset);
37
+ }
38
+
39
+ error(str) {
40
+ console.error(this._FgRed + str + this._Reset);
41
+ }
42
+
43
+ debug(str) {
44
+ if (this._debug) {
45
+ console.debug(this._FgGray + str + this._Reset);
46
+ }
47
+ }
48
+
49
+ setDebugEnabled(enabled = true) {
50
+ this._debug = enabled;
51
+ }
52
+ }
53
+
54
+ /*********************************************************************
55
+ * UIServer
56
+ * Main server-side script called when Custom UI client sends requests
57
+ */
58
+ class UiServer extends HomebridgePluginUiServer {
59
+
60
+ logger;
61
+ config;
62
+ api;
63
+
64
+ constructor() {
65
+ super();
66
+ // Obtain the plugin configuration from homebridge config JSON file.
67
+ const config = require(this.homebridgeConfigPath).platforms.find((obj) => obj.platform === 'blueair-purifier');
68
+ this.logger = new Logger(config?.uiDebug ? config.uiDebug : false);
69
+ this.logger.info('Custom UI created.');
70
+ this.logger.debug(`ENV:\n${JSON.stringify(process.env, null, 2)}`);
71
+
72
+ this.onRequest('/mergeToDefault', async ({ config }) => {
73
+ _.defaultsDeep(config, defaultConfig);
74
+ config.devices.forEach((device) => {
75
+ _.defaultsDeep(device, defaultDeviceConfig);
76
+ });
77
+ this.config = config;
78
+ this.logger.setDebugEnabled(config.uiDebug ? config.uiDebug : false);
79
+ this.logger.debug(`Merged config:\n${JSON.stringify(config, null, 2)}`);
80
+ return config;
81
+ });
82
+
83
+ this.onRequest('/getDefaults', async () => {
84
+ return {
85
+ defaultConfig,
86
+ defaultDeviceConfig,
87
+ };
88
+ });
89
+
90
+ this.onRequest('/discover', async ({ username, password, region }) => {
91
+ try {
92
+ this.api = new BlueAirAwsApi(username, password, region, this.logger);
93
+ await this.api.login();
94
+ const devices = await this.api.getDevices();
95
+
96
+ return devices;
97
+ } catch (e) {
98
+ const msg = e instanceof Error ? e.stack : e;
99
+ this.logger.error(`Device discovery failed:\n${msg}`);
100
+ throw new RequestError(`Device discovery failed:\n${msg}`);
101
+ }
102
+ });
103
+
104
+ this.onRequest('/getInitialDeviceStates', async ({ accountUuid, uuids }) => {
105
+ try {
106
+ return await this.api.getDeviceStatus(accountUuid, uuids);
107
+ } catch (e) {
108
+ const msg = e instanceof Error ? e.stack : e;
109
+ this.logger.error(`Failed to get initial device states:\n${msg}`);
110
+ throw new RequestError(`Failed to get initial device states:\n${msg}`);
111
+ }
112
+ });
113
+
114
+ // inform client-side script that we are ready to receive requests.
115
+ this.ready();
116
+ }
117
+ }
118
+
119
+ // start the instance of the class
120
+ (() => {
121
+ return new UiServer();
122
+ })();