hass-taste-test 0.2.7 → 0.3.1
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/README.md +30 -23
- package/lib/bin.js +2 -2
- package/lib/index.d.ts +43 -15
- package/lib/index.js +247 -113
- package/lib/integrations/get-diffable-html.d.ts +2 -27
- package/lib/integrations/get-diffable-html.js +30 -27
- package/lib/integrations/playwright.d.ts +6 -7
- package/lib/integrations/playwright.js +10 -5
- package/lib/socket.d.ts +1 -1
- package/lib/socket.js +6 -4
- package/lib/types.d.ts +1 -3
- package/package.json +38 -27
- package/lib/hass-test.d.ts +0 -103
- package/lib/hass-test.js +0 -411
- package/lib/hasstest-test.d.ts +0 -1
- package/lib/hasstest-test.js +0 -48
- package/lib/hasstest.d.ts +0 -62
- package/lib/hasstest.js +0 -232
- package/lib/homeassistant-types.d.ts +0 -112
- package/lib/homeassistant-types.js +0 -2
package/lib/index.js
CHANGED
|
@@ -15,37 +15,61 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
37
|
};
|
|
28
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
39
|
+
exports.HassCard = exports.HomeAssistant = exports.PlaywrightPage = exports.PlaywrightBrowser = void 0;
|
|
40
|
+
exports.multiply = multiply;
|
|
30
41
|
const child_process_1 = require("child_process");
|
|
31
42
|
const path_1 = require("path");
|
|
32
43
|
const os_1 = require("os");
|
|
33
|
-
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
34
44
|
const fs = __importStar(require("fs/promises"));
|
|
35
45
|
const net = __importStar(require("net"));
|
|
36
46
|
const hass = __importStar(require("home-assistant-js-websocket"));
|
|
37
|
-
const
|
|
47
|
+
const socket_ts_1 = require("./socket.js");
|
|
38
48
|
const proper_lockfile_1 = __importDefault(require("proper-lockfile"));
|
|
39
49
|
const home_assistant_js_websocket_1 = require("home-assistant-js-websocket");
|
|
40
|
-
var
|
|
41
|
-
Object.defineProperty(exports, "PlaywrightBrowser", { enumerable: true, get: function () { return
|
|
50
|
+
var playwright_ts_1 = require("./integrations/playwright.js");
|
|
51
|
+
Object.defineProperty(exports, "PlaywrightBrowser", { enumerable: true, get: function () { return playwright_ts_1.PlaywrightBrowser; } });
|
|
52
|
+
Object.defineProperty(exports, "PlaywrightPage", { enumerable: true, get: function () { return playwright_ts_1.PlaywrightPage; } });
|
|
53
|
+
/** First port tried for Home Assistant, and how many ports to cycle through before wrapping */
|
|
54
|
+
const FIRST_PORT = 8130;
|
|
55
|
+
const PORT_RANGE = 1000;
|
|
42
56
|
/** Sleeps for 100 ms */
|
|
43
57
|
const sleep = () => new Promise((r) => setTimeout(r, 100));
|
|
44
|
-
|
|
45
|
-
|
|
58
|
+
/** Runs a command, rejecting if it cannot be started or exits with a non-zero code */
|
|
59
|
+
const exec = (command, args, quiet = false) => new Promise((resolve, reject) => {
|
|
60
|
+
const proc = (0, child_process_1.spawn)(command, args, { stdio: quiet ? 'ignore' : 'inherit' });
|
|
46
61
|
proc.on('error', (err) => reject(err));
|
|
47
|
-
proc.on('close', (code) =>
|
|
62
|
+
proc.on('close', (code) => {
|
|
63
|
+
if (code === 0)
|
|
64
|
+
resolve();
|
|
65
|
+
else
|
|
66
|
+
reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`));
|
|
67
|
+
});
|
|
48
68
|
});
|
|
69
|
+
const exists = (path) => fs.access(path).then(() => true, () => false);
|
|
70
|
+
/** Whether uv (https://docs.astral.sh/uv/) is available on the PATH */
|
|
71
|
+
let uvAvailable;
|
|
72
|
+
const hasUv = () => (uvAvailable ??= exec('uv', ['--version'], true).then(() => true, () => false));
|
|
49
73
|
class HomeAssistant {
|
|
50
74
|
venvDir;
|
|
51
75
|
configDir;
|
|
@@ -57,12 +81,10 @@ class HomeAssistant {
|
|
|
57
81
|
options;
|
|
58
82
|
dashboards = 0;
|
|
59
83
|
entities = {};
|
|
60
|
-
started = false;
|
|
61
84
|
ws;
|
|
62
85
|
constructor(options) {
|
|
63
86
|
this.venvDir = (0, path_1.join)((0, os_1.tmpdir)(), 'hasstest-venv');
|
|
64
87
|
this.options = {
|
|
65
|
-
python: 'python3',
|
|
66
88
|
hassArgs: [],
|
|
67
89
|
host: '127.0.0.1',
|
|
68
90
|
port: null,
|
|
@@ -77,8 +99,9 @@ class HomeAssistant {
|
|
|
77
99
|
path_confFile = () => (0, path_1.join)(this.configDir, 'configuration.yaml');
|
|
78
100
|
path_componentsDir = () => (0, path_1.join)(this.configDir, 'custom_components');
|
|
79
101
|
path_wwwDir = () => (0, path_1.join)(this.configDir, 'www');
|
|
102
|
+
path_storageDir = () => (0, path_1.join)(this.configDir, '.storage');
|
|
80
103
|
path_hass = () => (0, path_1.join)(this.venvDir, 'bin/hass');
|
|
81
|
-
|
|
104
|
+
path_python = () => (0, path_1.join)(this.venvDir, 'bin/python');
|
|
82
105
|
path_cache = () => (0, path_1.join)((0, os_1.tmpdir)(), 'hasstest-cache');
|
|
83
106
|
/** Create and connect to an isolated Home Assistant instance */
|
|
84
107
|
static async create(config, options) {
|
|
@@ -102,47 +125,96 @@ class HomeAssistant {
|
|
|
102
125
|
]);
|
|
103
126
|
// Step 2: Create venv and sort out networking under lock
|
|
104
127
|
const releaseLock = await this.acquireLock();
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
this.findPort()
|
|
108
|
-
this.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
128
|
+
try {
|
|
129
|
+
this.cache = await this.readCache();
|
|
130
|
+
await Promise.all([this.findPort(), this.setupVenv()]);
|
|
131
|
+
await fs.writeFile(this.path_cache(), JSON.stringify(this.cache));
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
await releaseLock();
|
|
135
|
+
}
|
|
136
|
+
await this.writeConfiguration(config);
|
|
112
137
|
// Step 3: Start Home Assistant and onboard
|
|
113
|
-
this.process = (0, child_process_1.spawn)(this.path_hass(), ['-c', this.configDir,
|
|
114
|
-
|
|
115
|
-
if (d.includes('Starting Home Assistant'))
|
|
116
|
-
this.started = true;
|
|
117
|
-
process.stderr.write(d);
|
|
118
|
-
});
|
|
119
|
-
this.process.on('error', (e) => {
|
|
120
|
-
console.error(`----\nHome Assistant is having trouble starting. This is likely due to a problem with your virtual environment; try removing the venv folder at ${this.venvDir} and re-running hass-taste-test\n---`);
|
|
121
|
-
throw e;
|
|
138
|
+
this.process = (0, child_process_1.spawn)(this.path_hass(), ['-c', this.configDir, ...this.options.hassArgs], {
|
|
139
|
+
stdio: 'pipe',
|
|
122
140
|
});
|
|
123
|
-
|
|
141
|
+
this.process.stderr.pipe(process.stderr);
|
|
142
|
+
await new Promise((resolve, reject) => {
|
|
143
|
+
let exited = false;
|
|
144
|
+
this.process.on('error', (e) => {
|
|
145
|
+
exited = true;
|
|
146
|
+
console.error(`----\nHome Assistant is having trouble starting. This is likely due to a problem with your virtual environment; try removing the venv folder at ${this.venvDir} and re-running hass-taste-test\n---`);
|
|
147
|
+
reject(e);
|
|
148
|
+
});
|
|
124
149
|
// Wait for Home Assistant to start, but quit early if the process exits
|
|
125
150
|
this.process.on('close', (code) => {
|
|
126
|
-
|
|
127
|
-
|
|
151
|
+
exited = true;
|
|
152
|
+
reject(new Error(`Home Assistant exited with code ${code}`));
|
|
128
153
|
});
|
|
129
|
-
|
|
130
|
-
await
|
|
131
|
-
|
|
154
|
+
(async () => {
|
|
155
|
+
while (!exited && !(await this.isUp()))
|
|
156
|
+
await sleep();
|
|
157
|
+
resolve();
|
|
158
|
+
})();
|
|
132
159
|
});
|
|
133
160
|
await this.onboard();
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
161
|
+
await this.waitUntilRunning();
|
|
162
|
+
}
|
|
163
|
+
/** Write configuration.yaml, plus HTTP settings in whichever form this Home Assistant expects */
|
|
164
|
+
async writeConfiguration(additionalConfig) {
|
|
165
|
+
// Only the frontend: its dependencies cover everything Hass Taste Test uses (auth, onboarding,
|
|
166
|
+
// config entries, lovelace, repairs, websocket API). default_config would also start ~20 more
|
|
167
|
+
// integrations (bluetooth, zeroconf, cloud, ...) that slow startup and fill the logs.
|
|
168
|
+
const config = ['frontend:'];
|
|
169
|
+
// Keep test output readable: only log errors, unless the caller configures logging themselves
|
|
170
|
+
if (!/^logger:/m.test(additionalConfig))
|
|
171
|
+
config.push('logger:', ' default: error');
|
|
172
|
+
if (await this.usesHttpStorage()) {
|
|
173
|
+
await this.writeHttpStorage();
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
config.push('http:', ` server_host: ${this.options.host}`, ` server_port: ${this.chosenPort}`);
|
|
177
|
+
}
|
|
178
|
+
config.push(additionalConfig);
|
|
179
|
+
await fs.writeFile(this.path_confFile(), config.join('\n'));
|
|
180
|
+
await this.writeOnboardingStorage();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Newer Home Assistant versions keep HTTP settings in `.storage/http` and only import the
|
|
184
|
+
* `http:` YAML block once. That import is applied as an unconfirmed trial configuration,
|
|
185
|
+
* which pops up a confirmation dialog in the frontend and reverts (restarting Home
|
|
186
|
+
* Assistant) after 5 minutes. Detect these versions so we can skip the YAML entirely.
|
|
187
|
+
*/
|
|
188
|
+
async usesHttpStorage() {
|
|
189
|
+
const configModule = (0, path_1.join)(await this.sitePackages(), 'homeassistant/components/http/config.py');
|
|
190
|
+
const source = await fs.readFile(configModule, 'utf-8').catch(() => '');
|
|
191
|
+
return source.includes('yaml_migration_done');
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Seed the HTTP config store so Home Assistant boots straight into our host and port.
|
|
195
|
+
* This uses the original (version 1) storage format, which Home Assistant migrates on load
|
|
196
|
+
* into a confirmed ("stable") config with all defaults filled in.
|
|
197
|
+
*/
|
|
198
|
+
async writeHttpStorage() {
|
|
199
|
+
await fs.mkdir(this.path_storageDir(), { recursive: true });
|
|
200
|
+
await fs.writeFile((0, path_1.join)(this.path_storageDir(), 'http'), JSON.stringify({
|
|
201
|
+
version: 1,
|
|
202
|
+
minor_version: 1,
|
|
203
|
+
key: 'http',
|
|
204
|
+
data: { server_host: [this.options.host], server_port: this.chosenPort },
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Seed the onboarding store so the core config and analytics steps start out done. Finishing
|
|
209
|
+
* the core config step kicks off config flows for shopping_list, met, google_translate, and
|
|
210
|
+
* radio_browser, which are never needed for tests and log errors when Home Assistant is closed
|
|
211
|
+
* before they finish setting up. Storage version 2 predates both steps, so every Home Assistant
|
|
212
|
+
* release since then migrates this file by marking them done, leaving only the user and
|
|
213
|
+
* integration steps (which hand us auth codes) for onboard() to complete.
|
|
214
|
+
*/
|
|
215
|
+
async writeOnboardingStorage() {
|
|
216
|
+
await fs.mkdir(this.path_storageDir(), { recursive: true });
|
|
217
|
+
await fs.writeFile((0, path_1.join)(this.path_storageDir(), 'onboarding'), JSON.stringify({ version: 2, minor_version: 1, key: 'onboarding', data: { done: [] } }));
|
|
146
218
|
}
|
|
147
219
|
/** Symlink custom component directories to the configuration directory */
|
|
148
220
|
async linkComponents() {
|
|
@@ -163,71 +235,132 @@ class HomeAssistant {
|
|
|
163
235
|
}
|
|
164
236
|
/** Find an open port to launch Home Assistant */
|
|
165
237
|
async findPort() {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
server.
|
|
178
|
-
resolve(true);
|
|
238
|
+
for (;;) {
|
|
239
|
+
this.chosenPort = this.cache.startPort++;
|
|
240
|
+
const server = net.createServer();
|
|
241
|
+
const success = await new Promise((resolve, reject) => {
|
|
242
|
+
server.on('error', (e) => {
|
|
243
|
+
if (e.code === 'EADDRINUSE' || e.code === 'EACCES')
|
|
244
|
+
resolve(false);
|
|
245
|
+
else
|
|
246
|
+
reject(e);
|
|
247
|
+
});
|
|
248
|
+
server.on('listening', () => server.close(() => resolve(true)));
|
|
249
|
+
server.listen(this.chosenPort, this.options.host);
|
|
179
250
|
});
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
251
|
+
if (success)
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/** Creates a python virtual environment and installs Home Assistant (using uv if available) */
|
|
256
|
+
async chooseInstaller() {
|
|
257
|
+
const requested = process.env.HASS_TASTE_TEST_INSTALLER;
|
|
258
|
+
if (requested === 'pip')
|
|
259
|
+
return 'pip';
|
|
260
|
+
if (requested === 'uv') {
|
|
261
|
+
if (!(await hasUv()))
|
|
262
|
+
throw new Error('The uv installer was requested, but uv is not on the PATH');
|
|
263
|
+
return 'uv';
|
|
264
|
+
}
|
|
265
|
+
if (requested)
|
|
266
|
+
throw new Error(`Unknown installer "${requested}"; expected "uv" or "pip"`);
|
|
267
|
+
if (await hasUv())
|
|
268
|
+
return 'uv';
|
|
269
|
+
console.warn('uv was not found on your PATH; falling back to pip. Install uv (https://docs.astral.sh/uv/) for much faster installs.');
|
|
270
|
+
return 'pip';
|
|
183
271
|
}
|
|
184
|
-
/** Creates a python virtual environment and installs Home Assistant */
|
|
185
272
|
async setupVenv() {
|
|
273
|
+
const uv = (await this.chooseInstaller()) === 'uv';
|
|
274
|
+
const python = this.cache.requiresPython;
|
|
275
|
+
const clearFlag = (clear) => (clear ? ['--clear'] : []);
|
|
276
|
+
const createVenv = (clear) => uv
|
|
277
|
+
? exec('uv', ['venv', ...clearFlag(clear), '--python', python, this.venvDir])
|
|
278
|
+
: exec('python3', ['-m', 'venv', ...clearFlag(clear), this.venvDir]);
|
|
279
|
+
const install = () => {
|
|
280
|
+
// Pin the version so an outdated interpreter fails loudly instead of resolving an old release
|
|
281
|
+
const pkg = `homeassistant==${this.cache.latestHass}`;
|
|
282
|
+
return uv
|
|
283
|
+
? exec('uv', ['pip', 'install', '--python', this.path_python(), pkg])
|
|
284
|
+
: exec(this.path_python(), ['-m', 'pip', 'install', pkg]);
|
|
285
|
+
};
|
|
186
286
|
// Create virtual environment if it does not already exist
|
|
187
|
-
await
|
|
188
|
-
|
|
189
|
-
.catch(() => exec(this.options.python, ['-m', 'venv', this.venvDir]));
|
|
287
|
+
if (!(await exists(this.path_python())))
|
|
288
|
+
await createVenv(false);
|
|
190
289
|
// Check if homeassistant needs an upgrade; saves a bit of time if it doesn't need one
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
await
|
|
290
|
+
if ((await this.hassVersion()) === this.cache.latestHass)
|
|
291
|
+
return;
|
|
292
|
+
try {
|
|
293
|
+
await install();
|
|
294
|
+
}
|
|
295
|
+
catch (e) {
|
|
296
|
+
// Most likely the venv's Python is too old for the latest Home Assistant, or the venv
|
|
297
|
+
// was created by uv (which does not include pip) and pip is now being used. Rebuild it.
|
|
298
|
+
console.warn(`Installing Home Assistant failed (${e}). Recreating ${this.venvDir}`);
|
|
299
|
+
await createVenv(true);
|
|
300
|
+
await install();
|
|
195
301
|
}
|
|
196
302
|
}
|
|
197
303
|
async readCache() {
|
|
304
|
+
let cached = {};
|
|
305
|
+
let fresh = false;
|
|
198
306
|
try {
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
307
|
+
const cacheFile = await fs.stat(this.path_cache());
|
|
308
|
+
cached = JSON.parse(await fs.readFile(this.path_cache(), 'utf-8'));
|
|
309
|
+
fresh = Date.now() - cacheFile.mtimeMs < 2000;
|
|
203
310
|
}
|
|
204
311
|
catch (e) { }
|
|
312
|
+
// Always continue from the last port handed out, even when the cache is stale.
|
|
313
|
+
let startPort = cached.startPort ?? FIRST_PORT;
|
|
314
|
+
if (!(startPort >= FIRST_PORT && startPort < FIRST_PORT + PORT_RANGE))
|
|
315
|
+
startPort = FIRST_PORT;
|
|
316
|
+
if (fresh && cached.requiresPython && cached.latestHass)
|
|
317
|
+
return { ...cached, startPort };
|
|
318
|
+
return { ...(await this.latestHAVersion()), startPort };
|
|
319
|
+
}
|
|
320
|
+
/** Fetch the latest hass version, and the Python versions it supports, from pypi */
|
|
321
|
+
async latestHAVersion() {
|
|
322
|
+
const response = await fetch('https://pypi.org/pypi/homeassistant/json');
|
|
323
|
+
if (!response.ok)
|
|
324
|
+
throw new Error(`Could not query PyPI: HTTP ${response.status}`);
|
|
325
|
+
const latest = (await response.json());
|
|
205
326
|
return {
|
|
206
|
-
latestHass:
|
|
207
|
-
|
|
327
|
+
latestHass: latest.info.version,
|
|
328
|
+
requiresPython: latest.info.requires_python || '3',
|
|
208
329
|
};
|
|
209
330
|
}
|
|
210
|
-
/**
|
|
211
|
-
async
|
|
212
|
-
const
|
|
213
|
-
|
|
331
|
+
/** Path to the venv's site-packages directory */
|
|
332
|
+
async sitePackages() {
|
|
333
|
+
const libFolders = await fs.readdir((0, path_1.join)(this.venvDir, 'lib')).catch(() => []);
|
|
334
|
+
const python = libFolders.find((f) => f.startsWith('python'));
|
|
335
|
+
if (!python)
|
|
336
|
+
throw new Error(`Could not find a Python installation in ${this.venvDir}`);
|
|
337
|
+
return (0, path_1.join)(this.venvDir, 'lib', python, 'site-packages');
|
|
214
338
|
}
|
|
215
339
|
/** Finds the version of Home Assistant installed, if any */
|
|
216
340
|
async hassVersion() {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
341
|
+
// An interrupted install can leave package metadata behind without the entry point
|
|
342
|
+
if (!(await exists(this.path_hass())))
|
|
343
|
+
return null;
|
|
344
|
+
const files = await this.sitePackages()
|
|
345
|
+
.then((dir) => fs.readdir(dir))
|
|
346
|
+
.catch(() => []);
|
|
347
|
+
const homeAssistant = files
|
|
221
348
|
.map((f) => f.match(/^homeassistant-(.*)\.dist-info/))
|
|
222
349
|
.find((f) => f !== null);
|
|
223
350
|
return homeAssistant ? homeAssistant[1] : null;
|
|
224
351
|
}
|
|
225
|
-
/** Checks if Home Assistant is
|
|
226
|
-
isUp = () =>
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
352
|
+
/** Checks if Home Assistant is serving the onboarding API, which onboard() starts with */
|
|
353
|
+
isUp = () => fetch(this.url + '/api/onboarding')
|
|
354
|
+
.then((response) => response.ok)
|
|
355
|
+
.catch(() => false);
|
|
356
|
+
/**
|
|
357
|
+
* Wait for Home Assistant to finish starting. Until then it has not installed its signal
|
|
358
|
+
* handlers, so close() would interrupt startup and log a KeyboardInterrupt traceback.
|
|
359
|
+
*/
|
|
360
|
+
async waitUntilRunning() {
|
|
361
|
+
while ((await hass.getConfig(this.ws)).state !== 'RUNNING')
|
|
362
|
+
await sleep();
|
|
363
|
+
}
|
|
231
364
|
/** Complete onboarding and fetch short-lived access token */
|
|
232
365
|
async onboard() {
|
|
233
366
|
let login;
|
|
@@ -239,7 +372,7 @@ class HomeAssistant {
|
|
|
239
372
|
username: this.options.username,
|
|
240
373
|
password: this.options.password,
|
|
241
374
|
});
|
|
242
|
-
if (login
|
|
375
|
+
if (login?.auth_code)
|
|
243
376
|
break;
|
|
244
377
|
else
|
|
245
378
|
await sleep();
|
|
@@ -249,13 +382,11 @@ class HomeAssistant {
|
|
|
249
382
|
await sleep();
|
|
250
383
|
}
|
|
251
384
|
}
|
|
252
|
-
if (!login
|
|
253
|
-
throw new Error('
|
|
385
|
+
if (!login?.auth_code)
|
|
386
|
+
throw new Error('Onboarding failed: no auth code found');
|
|
254
387
|
this.accessCode = login.auth_code;
|
|
255
388
|
await this.launchWebsocket(this.accessCode);
|
|
256
|
-
//
|
|
257
|
-
await this.post('/api/onboarding/core_config', {}, true);
|
|
258
|
-
await this.post('/api/onboarding/analytics', {}, true);
|
|
389
|
+
// Core config and analytics were already marked done (see writeOnboardingStorage)
|
|
259
390
|
const response = await this.post('/api/onboarding/integration', { redirect_uri: this.url + '/?auth_callback=1' }, true);
|
|
260
391
|
this.accessCode = response.auth_code;
|
|
261
392
|
}
|
|
@@ -275,7 +406,7 @@ class HomeAssistant {
|
|
|
275
406
|
return `${this.url}/${path}?auth_callback=1&code=${encodeURIComponent(code)}&state=${encodeURIComponent(state)}`;
|
|
276
407
|
}
|
|
277
408
|
async post(url, body, authorize = false) {
|
|
278
|
-
const response = await (
|
|
409
|
+
const response = await fetch(this.url + url, {
|
|
279
410
|
method: 'post',
|
|
280
411
|
headers: {
|
|
281
412
|
'Content-Type': 'application/json',
|
|
@@ -309,7 +440,7 @@ class HomeAssistant {
|
|
|
309
440
|
params.append('client_id', this.clientId);
|
|
310
441
|
params.append('code', code);
|
|
311
442
|
params.append('grant_type', 'authorization_code');
|
|
312
|
-
const response = await (
|
|
443
|
+
const response = await fetch(this.url + '/auth/token', {
|
|
313
444
|
method: 'post',
|
|
314
445
|
body: params,
|
|
315
446
|
});
|
|
@@ -324,9 +455,9 @@ class HomeAssistant {
|
|
|
324
455
|
});
|
|
325
456
|
this.ws = await hass.createConnection({
|
|
326
457
|
auth,
|
|
327
|
-
createSocket: async () => (0,
|
|
458
|
+
createSocket: async () => (0, socket_ts_1.createSocket)(auth, false),
|
|
328
459
|
});
|
|
329
|
-
(0, home_assistant_js_websocket_1.
|
|
460
|
+
(0, home_assistant_js_websocket_1.subscribeEntityStates)(this.ws, (e) => (this.entities = { ...this.entities, ...e }));
|
|
330
461
|
}
|
|
331
462
|
async addIntegration(name) {
|
|
332
463
|
const response = await this.post('/api/config/config_entries/flow', {
|
|
@@ -385,7 +516,7 @@ class HomeAssistant {
|
|
|
385
516
|
}
|
|
386
517
|
async Dashboard(config, options) {
|
|
387
518
|
if (!this.options.browser)
|
|
388
|
-
throw new Error('Cannot launch a dashboard without a browser integration. Make sure to specify options.
|
|
519
|
+
throw new Error('Cannot launch a dashboard without a browser integration. Make sure to specify options.browser');
|
|
389
520
|
const dashboard = await this.createDashboard({ title: options?.title });
|
|
390
521
|
await this.setDashboardView(dashboard, config);
|
|
391
522
|
const code = await this.fetchLoginCode();
|
|
@@ -401,18 +532,22 @@ class HomeAssistant {
|
|
|
401
532
|
await this.options.browser.close();
|
|
402
533
|
if (this.ws)
|
|
403
534
|
this.ws.close();
|
|
404
|
-
|
|
535
|
+
const running = this.process?.pid !== undefined &&
|
|
536
|
+
this.process.exitCode === null &&
|
|
537
|
+
this.process.signalCode === null;
|
|
538
|
+
if (running) {
|
|
539
|
+
const exited = new Promise((resolve) => this.process.once('exit', resolve));
|
|
405
540
|
this.process.kill('SIGINT');
|
|
406
|
-
await
|
|
541
|
+
await exited;
|
|
407
542
|
}
|
|
408
543
|
if (this.configDir)
|
|
409
|
-
await fs.rm(this.configDir, { recursive: true });
|
|
544
|
+
await fs.rm(this.configDir, { recursive: true, force: true });
|
|
410
545
|
}
|
|
411
546
|
HassDashboard = class {
|
|
547
|
+
cards = [];
|
|
412
548
|
parent;
|
|
413
549
|
path;
|
|
414
550
|
page;
|
|
415
|
-
cards = [];
|
|
416
551
|
constructor(parent, path, config, page) {
|
|
417
552
|
this.parent = parent;
|
|
418
553
|
this.path = path;
|
|
@@ -432,9 +567,9 @@ class HomeAssistant {
|
|
|
432
567
|
}
|
|
433
568
|
exports.HomeAssistant = HomeAssistant;
|
|
434
569
|
class HassCard {
|
|
570
|
+
selectors;
|
|
435
571
|
n;
|
|
436
572
|
page;
|
|
437
|
-
selectors;
|
|
438
573
|
constructor(n, page, selectors) {
|
|
439
574
|
this.n = n;
|
|
440
575
|
this.page = page;
|
|
@@ -471,4 +606,3 @@ function multiply(n, configFn) {
|
|
|
471
606
|
}
|
|
472
607
|
return config;
|
|
473
608
|
}
|
|
474
|
-
exports.multiply = multiply;
|
|
@@ -1,29 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/** tags on which to ignore the given attributes */
|
|
3
|
-
tags: string[];
|
|
4
|
-
/** attributes to ignore for the given tags */
|
|
5
|
-
attributes: string[];
|
|
6
|
-
}
|
|
7
|
-
interface DiffOptions {
|
|
8
|
-
/** array of attributes to ignore, when given a string that attribute will be ignored on all tags
|
|
9
|
-
* when given an object of type `IgnoreAttributesForTags`, you can specify on which tags to ignore which attributes */
|
|
10
|
-
ignoreAttributes: (string | IgnoreAttributesForTags)[];
|
|
11
|
-
/** array of tags to ignore, these tags are stripped from the output */
|
|
12
|
-
ignoreTags: string[];
|
|
13
|
-
/** array of tags whose children to ignore, the children of these tags are stripped from the output */
|
|
14
|
-
ignoreChildren: string[];
|
|
15
|
-
/** like ignoreChildren, but applies to shadow DOM rather than direct descendants */
|
|
16
|
-
ignoreShadowChildren: string[];
|
|
17
|
-
/** array of attributes which should be removed when empty.
|
|
18
|
-
* Be careful not to add any boolean attributes here (e.g. `hidden`) unless you know what you're doing */
|
|
19
|
-
stripEmptyAttributes: string[];
|
|
20
|
-
/** whether to strip attributes from Lit & Polymer with undefined values */
|
|
21
|
-
stripUndefinedAttributes: boolean;
|
|
22
|
-
}
|
|
23
|
-
declare const DEFAULT_IGNORE_TAGS: string[];
|
|
24
|
-
declare const DEFAULT_IGNORE_SHADOW_CHILDREN: string[];
|
|
25
|
-
declare const DEFAULT_EMPTY_ATTRS: string[];
|
|
26
|
-
declare const VOID_ELEMENTS: string[];
|
|
1
|
+
import type { DiffOptions } from '../types.ts';
|
|
27
2
|
/**
|
|
28
3
|
* Restructures given HTML string, returning it in a format which can be used for comparison:
|
|
29
4
|
* - whitespace and newlines are normalized
|
|
@@ -46,4 +21,4 @@ declare const VOID_ELEMENTS: string[];
|
|
|
46
21
|
* @param {DiffOptions} [options]
|
|
47
22
|
* @returns {string} html restructured in a diffable format
|
|
48
23
|
*/
|
|
49
|
-
declare function getDiffableHTML(container: Node, options?: DiffOptions): string;
|
|
24
|
+
export declare function getDiffableHTML(container: Node, options?: Partial<DiffOptions>): string;
|
|
@@ -3,33 +3,8 @@
|
|
|
3
3
|
// Changes are licensed under the repository license (see LICENSE.md)
|
|
4
4
|
//
|
|
5
5
|
// The code has been modified to traverse shadow DOM & find Lit and Polymer attributes
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
'ha-icon',
|
|
9
|
-
'ha-state-icon',
|
|
10
|
-
'ha-slider',
|
|
11
|
-
'mwc-button',
|
|
12
|
-
'paper-input', // Polymer elements
|
|
13
|
-
];
|
|
14
|
-
const DEFAULT_EMPTY_ATTRS = ['class', 'id'];
|
|
15
|
-
const VOID_ELEMENTS = [
|
|
16
|
-
'area',
|
|
17
|
-
'base',
|
|
18
|
-
'br',
|
|
19
|
-
'col',
|
|
20
|
-
'embed',
|
|
21
|
-
'hr',
|
|
22
|
-
'img',
|
|
23
|
-
'input',
|
|
24
|
-
'keygen',
|
|
25
|
-
'link',
|
|
26
|
-
'menuitem',
|
|
27
|
-
'meta',
|
|
28
|
-
'param',
|
|
29
|
-
'source',
|
|
30
|
-
'track',
|
|
31
|
-
'wbr',
|
|
32
|
-
];
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getDiffableHTML = getDiffableHTML;
|
|
33
8
|
/**
|
|
34
9
|
* Restructures given HTML string, returning it in a format which can be used for comparison:
|
|
35
10
|
* - whitespace and newlines are normalized
|
|
@@ -53,6 +28,34 @@ const VOID_ELEMENTS = [
|
|
|
53
28
|
* @returns {string} html restructured in a diffable format
|
|
54
29
|
*/
|
|
55
30
|
function getDiffableHTML(container, options) {
|
|
31
|
+
// Constants live inside the function because it is serialized and run in the browser
|
|
32
|
+
const DEFAULT_IGNORE_TAGS = ['script', 'style'];
|
|
33
|
+
const DEFAULT_IGNORE_SHADOW_CHILDREN = [
|
|
34
|
+
'ha-icon',
|
|
35
|
+
'ha-state-icon',
|
|
36
|
+
'ha-slider', // Home Assistant elements
|
|
37
|
+
'mwc-button', // Material web components
|
|
38
|
+
'paper-input', // Polymer elements
|
|
39
|
+
];
|
|
40
|
+
const DEFAULT_EMPTY_ATTRS = ['class', 'id'];
|
|
41
|
+
const VOID_ELEMENTS = [
|
|
42
|
+
'area',
|
|
43
|
+
'base',
|
|
44
|
+
'br',
|
|
45
|
+
'col',
|
|
46
|
+
'embed',
|
|
47
|
+
'hr',
|
|
48
|
+
'img',
|
|
49
|
+
'input',
|
|
50
|
+
'keygen',
|
|
51
|
+
'link',
|
|
52
|
+
'menuitem',
|
|
53
|
+
'meta',
|
|
54
|
+
'param',
|
|
55
|
+
'source',
|
|
56
|
+
'track',
|
|
57
|
+
'wbr',
|
|
58
|
+
];
|
|
56
59
|
const ignoreAttributes = options?.ignoreAttributes
|
|
57
60
|
? options.ignoreAttributes.filter((e) => typeof e === 'string')
|
|
58
61
|
: [];
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { BrowserIntegration, BrowserPage, DashboardOptions, DiffOptions } from '../types';
|
|
1
|
+
import type { Browser, ElementHandle, Page, LaunchOptions, BrowserContext } from 'playwright';
|
|
2
|
+
import type { BrowserIntegration, BrowserPage, DashboardOptions, DiffOptions } from '../types.ts';
|
|
4
3
|
export type PlaywrightElement = ElementHandle<Node>;
|
|
5
4
|
export declare class PlaywrightBrowser implements BrowserIntegration<PlaywrightElement> {
|
|
6
5
|
private static browsers;
|
|
@@ -15,10 +14,10 @@ export declare class PlaywrightBrowser implements BrowserIntegration<PlaywrightE
|
|
|
15
14
|
export declare class PlaywrightPage implements BrowserPage<PlaywrightElement> {
|
|
16
15
|
private page;
|
|
17
16
|
constructor(page: Page);
|
|
18
|
-
getNthCard(n: number): Promise<ElementHandle<
|
|
19
|
-
shadowHTML(element: PlaywrightElement, options?: DiffOptions): Promise<string>;
|
|
20
|
-
screenshot(element: PlaywrightElement): Promise<Buffer
|
|
21
|
-
find(element: PlaywrightElement, selector: string): Promise<ElementHandle<
|
|
17
|
+
getNthCard(n: number): Promise<ElementHandle<HTMLElement | SVGElement>>;
|
|
18
|
+
shadowHTML(element: PlaywrightElement, options?: Partial<DiffOptions>): Promise<string>;
|
|
19
|
+
screenshot(element: PlaywrightElement): Promise<Buffer<ArrayBufferLike>>;
|
|
20
|
+
find(element: PlaywrightElement, selector: string): Promise<ElementHandle<HTMLElement | SVGElement> | null>;
|
|
22
21
|
textContent(element: PlaywrightElement): Promise<string>;
|
|
23
22
|
get playwright(): Page;
|
|
24
23
|
}
|