hass-taste-test 0.2.7 → 0.3.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/README.md +30 -23
- package/lib/bin.js +2 -2
- package/lib/index.d.ts +42 -15
- package/lib/index.js +237 -108
- 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 +5 -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/hasstest.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.HassCard = exports.HassDashboard = void 0;
|
|
7
|
-
const child_process_1 = require("child_process");
|
|
8
|
-
const path_1 = require("path");
|
|
9
|
-
const os_1 = require("os");
|
|
10
|
-
const fs_1 = require("fs");
|
|
11
|
-
const util_1 = require("util");
|
|
12
|
-
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
13
|
-
const net_1 = require("net");
|
|
14
|
-
const home_assistant_js_websocket_1 = require("home-assistant-js-websocket");
|
|
15
|
-
const socket_1 = require("./socket");
|
|
16
|
-
const DEFAULT_CONFIG = (options) => `
|
|
17
|
-
frontend:
|
|
18
|
-
http:
|
|
19
|
-
server_host: ${options.host}
|
|
20
|
-
server_port: ${options.port}
|
|
21
|
-
`;
|
|
22
|
-
const exec = (command, args) => new Promise((resolve, reject) => {
|
|
23
|
-
const proc = child_process_1.spawn(command, args, { stdio: 'inherit' });
|
|
24
|
-
proc.on('error', err => reject(err));
|
|
25
|
-
proc.on('close', code => resolve(code));
|
|
26
|
-
});
|
|
27
|
-
class HassTest {
|
|
28
|
-
config;
|
|
29
|
-
venvDir;
|
|
30
|
-
configDir;
|
|
31
|
-
configFile;
|
|
32
|
-
process;
|
|
33
|
-
accessCode;
|
|
34
|
-
accessToken;
|
|
35
|
-
options;
|
|
36
|
-
dashboards = 0;
|
|
37
|
-
ws;
|
|
38
|
-
constructor(config, options) {
|
|
39
|
-
this.config = config;
|
|
40
|
-
this.venvDir = path_1.join(os_1.tmpdir(), 'hasstest-venv');
|
|
41
|
-
this.options = {
|
|
42
|
-
python: 'python3',
|
|
43
|
-
hassArgs: [],
|
|
44
|
-
host: '127.0.0.1',
|
|
45
|
-
port: 8091,
|
|
46
|
-
username: 'dev',
|
|
47
|
-
password: 'dev',
|
|
48
|
-
userLanguage: 'en',
|
|
49
|
-
userDisplayName: 'Developer',
|
|
50
|
-
...options
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
/** Start the HomeAssistant server and connect to its websocket */
|
|
54
|
-
async start() {
|
|
55
|
-
this.configDir = await util_1.promisify(fs_1.mkdtemp)(path_1.join(os_1.tmpdir(), 'hasstest-'));
|
|
56
|
-
this.configFile = path_1.join(this.configDir, 'configuration.yaml');
|
|
57
|
-
await util_1.promisify(fs_1.writeFile)(this.configFile, DEFAULT_CONFIG(this.options) + this.config);
|
|
58
|
-
await this.setupVenv();
|
|
59
|
-
this.process = child_process_1.spawn(path_1.join(this.venvDir, 'bin/hass'), ['-c', this.configDir, ...this.options.hassArgs], {
|
|
60
|
-
stdio: 'inherit'
|
|
61
|
-
});
|
|
62
|
-
while (!(await this.isUp())) { /* wait */ }
|
|
63
|
-
await this.onboard();
|
|
64
|
-
}
|
|
65
|
-
/** Creates a python virtual environment and installs Home Assistant */
|
|
66
|
-
async setupVenv() {
|
|
67
|
-
await exec(this.options.python, ['-m', 'venv', this.venvDir]);
|
|
68
|
-
// Check if homeassistant needs an upgrade; saves a bit of time if it doesn't need one
|
|
69
|
-
const latest = await node_fetch_1.default('https://pypi.org/pypi/homeassistant/json').then(r => r.json());
|
|
70
|
-
const { stdout } = await util_1.promisify(child_process_1.execFile)(path_1.join(this.venvDir, 'bin/pip'), ['freeze']);
|
|
71
|
-
const installed = stdout.split('\n').find(v => v.startsWith('homeassistant=='));
|
|
72
|
-
if (!installed || installed.split('==')[1] !== latest.info.version) {
|
|
73
|
-
await exec(path_1.join(this.venvDir, 'bin/pip'), ['install', '--upgrade', 'homeassistant']);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/** Checks if Home Assistant is listening on its TCP port */
|
|
77
|
-
isUp = () => new Promise(resolve => {
|
|
78
|
-
net_1.createConnection(this.options.port, this.options.host).on('connect', () => {
|
|
79
|
-
resolve(true);
|
|
80
|
-
}).on("error", () => {
|
|
81
|
-
setTimeout(() => resolve(false), 300);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
/** Complete onboarding and fetch short-lived access token */
|
|
85
|
-
async onboard() {
|
|
86
|
-
const login = await this.post('/api/onboarding/users', {
|
|
87
|
-
language: this.options.userLanguage,
|
|
88
|
-
name: this.options.userDisplayName,
|
|
89
|
-
username: this.options.username,
|
|
90
|
-
password: this.options.password
|
|
91
|
-
});
|
|
92
|
-
this.accessCode = login.auth_code;
|
|
93
|
-
await this.launchWebsocket(this.accessCode);
|
|
94
|
-
// Step through the onboarding pages
|
|
95
|
-
await this.post('/api/onboarding/core_config', {}, true);
|
|
96
|
-
await this.post('/api/onboarding/analytics', {}, true);
|
|
97
|
-
const response = await this.post('/api/onboarding/integration', {
|
|
98
|
-
redirect_uri: this.url + '/?auth_callback=1'
|
|
99
|
-
}, true);
|
|
100
|
-
this.accessCode = response.auth_code;
|
|
101
|
-
}
|
|
102
|
-
get url() { return `http://${this.options.host}:${this.options.port}`; }
|
|
103
|
-
get clientId() { return this.url + '/'; }
|
|
104
|
-
get dashboard() { return this.customDashboard(''); }
|
|
105
|
-
customDashboard(name, code = this.accessCode) {
|
|
106
|
-
if (!code)
|
|
107
|
-
throw new Error("You haven't logged into Home Assistant yet. Have you called hasstest.start()?");
|
|
108
|
-
const state = Buffer.from(JSON.stringify({ hassUrl: this.url, clientId: this.clientId })).toString('base64');
|
|
109
|
-
return this.url + '/' + name + `?auth_callback=1&code=${encodeURIComponent(this.accessCode)}&state=${encodeURIComponent(state)}`;
|
|
110
|
-
}
|
|
111
|
-
async post(url, body, authorize = false) {
|
|
112
|
-
const response = await node_fetch_1.default(this.url + url, {
|
|
113
|
-
method: 'post',
|
|
114
|
-
headers: { 'Content-Type': 'application/json', ...(authorize ? { 'Authorization': 'Bearer ' + this.accessToken } : undefined) },
|
|
115
|
-
body: JSON.stringify({ client_id: this.clientId, ...body })
|
|
116
|
-
});
|
|
117
|
-
return await response.json();
|
|
118
|
-
}
|
|
119
|
-
/** Fetch a access code from username and password */
|
|
120
|
-
async fetchLoginCode() {
|
|
121
|
-
const login = await this.post('/auth/login_flow', {
|
|
122
|
-
handler: ["homeassistant", null],
|
|
123
|
-
redirect_uri: this.url + '/?auth_callback=1'
|
|
124
|
-
});
|
|
125
|
-
const response = await this.post('/auth/login_flow/' + login.flow_id, {
|
|
126
|
-
username: this.options.username,
|
|
127
|
-
password: this.options.password
|
|
128
|
-
});
|
|
129
|
-
return response.result;
|
|
130
|
-
}
|
|
131
|
-
/** Login and create tokens and websocket */
|
|
132
|
-
async login() {
|
|
133
|
-
this.accessCode = await this.fetchLoginCode();
|
|
134
|
-
await this.launchWebsocket(this.accessCode);
|
|
135
|
-
}
|
|
136
|
-
async launchWebsocket(code) {
|
|
137
|
-
const params = new URLSearchParams();
|
|
138
|
-
params.append('client_id', this.clientId);
|
|
139
|
-
params.append('code', code);
|
|
140
|
-
params.append('grant_type', 'authorization_code');
|
|
141
|
-
const response = await node_fetch_1.default(this.url + '/auth/token', {
|
|
142
|
-
method: 'post', body: params
|
|
143
|
-
});
|
|
144
|
-
const tokens = await response.json();
|
|
145
|
-
this.accessToken = tokens.access_token;
|
|
146
|
-
const auth = new home_assistant_js_websocket_1.Auth({
|
|
147
|
-
...tokens,
|
|
148
|
-
hassUrl: this.url,
|
|
149
|
-
clientId: this.clientId,
|
|
150
|
-
});
|
|
151
|
-
this.ws = await home_assistant_js_websocket_1.createConnection({ auth, createSocket: async () => socket_1.createSocket(auth, false), });
|
|
152
|
-
}
|
|
153
|
-
async createDashboard(options) {
|
|
154
|
-
if (!this.ws)
|
|
155
|
-
throw new Error('Hass-test has not yet been initialized. Did you call hass.start()?');
|
|
156
|
-
const id = ++this.dashboards;
|
|
157
|
-
const args = {
|
|
158
|
-
type: "lovelace/dashboards/create",
|
|
159
|
-
url_path: `lovelace-${id}`,
|
|
160
|
-
mode: 'storage',
|
|
161
|
-
require_admin: false,
|
|
162
|
-
show_in_sidebar: true,
|
|
163
|
-
title: `Dashboard ${id}`,
|
|
164
|
-
...options
|
|
165
|
-
};
|
|
166
|
-
await this.ws.sendMessagePromise(args);
|
|
167
|
-
return args.url_path;
|
|
168
|
-
}
|
|
169
|
-
async setDashboardView(dashboard_path, config) {
|
|
170
|
-
if (!this.ws)
|
|
171
|
-
throw new Error('Hass-test has not yet been initialized. Did you call hass.start()?');
|
|
172
|
-
await this.ws.sendMessagePromise({
|
|
173
|
-
type: "lovelace/config/save",
|
|
174
|
-
url_path: dashboard_path,
|
|
175
|
-
config: {
|
|
176
|
-
title: "View",
|
|
177
|
-
views: [{
|
|
178
|
-
path: "default_view",
|
|
179
|
-
title: "View",
|
|
180
|
-
cards: config
|
|
181
|
-
}]
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
async Dashboard(config) {
|
|
186
|
-
if (!this.options.integration)
|
|
187
|
-
throw new Error('Cannot launch a dashboard without a browser integration. Make sure to specify options.integration');
|
|
188
|
-
const dashboard = await this.createDashboard();
|
|
189
|
-
await this.setDashboardView(dashboard, config);
|
|
190
|
-
const code = await this.fetchLoginCode();
|
|
191
|
-
const page = await this.options.integration.open(this.customDashboard(dashboard, code));
|
|
192
|
-
return new HassDashboard(this, config, page);
|
|
193
|
-
}
|
|
194
|
-
/** Clean up connections and stop the HomeAssistant server */
|
|
195
|
-
async close() {
|
|
196
|
-
if (this.options.integration)
|
|
197
|
-
await this.options.integration.close();
|
|
198
|
-
if (this.ws)
|
|
199
|
-
this.ws.close();
|
|
200
|
-
if (this.process) {
|
|
201
|
-
this.process.kill('SIGINT');
|
|
202
|
-
await new Promise(resolve => this.process.on('exit', resolve));
|
|
203
|
-
}
|
|
204
|
-
if (this.configDir)
|
|
205
|
-
await util_1.promisify(fs_1.rm)(this.configDir, { recursive: true });
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
exports.default = HassTest;
|
|
209
|
-
class HassDashboard {
|
|
210
|
-
parent;
|
|
211
|
-
cards = [];
|
|
212
|
-
constructor(parent, config, page) {
|
|
213
|
-
this.parent = parent;
|
|
214
|
-
for (let i = 0; i < config.length; i++) {
|
|
215
|
-
this.cards.push(new HassCard(i, page));
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
exports.HassDashboard = HassDashboard;
|
|
220
|
-
class HassCard {
|
|
221
|
-
n;
|
|
222
|
-
page;
|
|
223
|
-
constructor(n, page) {
|
|
224
|
-
this.n = n;
|
|
225
|
-
this.page = page;
|
|
226
|
-
}
|
|
227
|
-
async html() {
|
|
228
|
-
const card = await this.page.getNthCard(this.n);
|
|
229
|
-
return this.page.shadowHTML(card);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
exports.HassCard = HassCard;
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
export interface LovelacePanelConfig {
|
|
2
|
-
mode: "yaml" | "storage";
|
|
3
|
-
}
|
|
4
|
-
export interface LovelaceConfig {
|
|
5
|
-
title?: string;
|
|
6
|
-
strategy?: {
|
|
7
|
-
type: string;
|
|
8
|
-
options?: Record<string, unknown>;
|
|
9
|
-
};
|
|
10
|
-
views: LovelaceViewConfig[];
|
|
11
|
-
background?: string;
|
|
12
|
-
}
|
|
13
|
-
export interface LegacyLovelaceConfig extends LovelaceConfig {
|
|
14
|
-
resources?: LovelaceResource[];
|
|
15
|
-
}
|
|
16
|
-
export interface LovelaceResource {
|
|
17
|
-
id: string;
|
|
18
|
-
type: "css" | "js" | "module" | "html";
|
|
19
|
-
url: string;
|
|
20
|
-
}
|
|
21
|
-
export interface LovelaceResourcesMutableParams {
|
|
22
|
-
res_type: LovelaceResource["type"];
|
|
23
|
-
url: string;
|
|
24
|
-
}
|
|
25
|
-
export declare type LovelaceDashboard = LovelaceYamlDashboard | LovelaceStorageDashboard;
|
|
26
|
-
interface LovelaceGenericDashboard {
|
|
27
|
-
id: string;
|
|
28
|
-
url_path: string;
|
|
29
|
-
require_admin: boolean;
|
|
30
|
-
show_in_sidebar: boolean;
|
|
31
|
-
icon?: string;
|
|
32
|
-
title: string;
|
|
33
|
-
}
|
|
34
|
-
export interface LovelaceYamlDashboard extends LovelaceGenericDashboard {
|
|
35
|
-
mode: "yaml";
|
|
36
|
-
filename: string;
|
|
37
|
-
}
|
|
38
|
-
export interface LovelaceStorageDashboard extends LovelaceGenericDashboard {
|
|
39
|
-
mode: "storage";
|
|
40
|
-
}
|
|
41
|
-
export interface LovelaceDashboardMutableParams {
|
|
42
|
-
require_admin: boolean;
|
|
43
|
-
show_in_sidebar: boolean;
|
|
44
|
-
icon?: string;
|
|
45
|
-
title: string;
|
|
46
|
-
}
|
|
47
|
-
export interface LovelaceDashboardCreateParams extends LovelaceDashboardMutableParams {
|
|
48
|
-
url_path: string;
|
|
49
|
-
mode: "storage";
|
|
50
|
-
}
|
|
51
|
-
export interface LovelaceViewConfig {
|
|
52
|
-
index?: number;
|
|
53
|
-
title?: string;
|
|
54
|
-
type?: string;
|
|
55
|
-
strategy?: {
|
|
56
|
-
type: string;
|
|
57
|
-
options?: Record<string, unknown>;
|
|
58
|
-
};
|
|
59
|
-
badges?: Array<string | LovelaceBadgeConfig>;
|
|
60
|
-
cards?: LovelaceCardConfig[];
|
|
61
|
-
path?: string;
|
|
62
|
-
icon?: string;
|
|
63
|
-
theme?: string;
|
|
64
|
-
panel?: boolean;
|
|
65
|
-
background?: string;
|
|
66
|
-
visible?: boolean | ShowViewConfig[];
|
|
67
|
-
}
|
|
68
|
-
export interface ShowViewConfig {
|
|
69
|
-
user?: string;
|
|
70
|
-
}
|
|
71
|
-
export interface LovelaceBadgeConfig {
|
|
72
|
-
type?: string;
|
|
73
|
-
[key: string]: any;
|
|
74
|
-
}
|
|
75
|
-
export interface LovelaceCardConfig {
|
|
76
|
-
index?: number;
|
|
77
|
-
view_index?: number;
|
|
78
|
-
view_layout?: any;
|
|
79
|
-
type: string;
|
|
80
|
-
[key: string]: any;
|
|
81
|
-
}
|
|
82
|
-
export interface ToggleActionConfig extends BaseActionConfig {
|
|
83
|
-
action: "toggle";
|
|
84
|
-
}
|
|
85
|
-
export interface NavigateActionConfig extends BaseActionConfig {
|
|
86
|
-
action: "navigate";
|
|
87
|
-
navigation_path: string;
|
|
88
|
-
}
|
|
89
|
-
export interface UrlActionConfig extends BaseActionConfig {
|
|
90
|
-
action: "url";
|
|
91
|
-
url_path: string;
|
|
92
|
-
}
|
|
93
|
-
export interface MoreInfoActionConfig extends BaseActionConfig {
|
|
94
|
-
action: "more-info";
|
|
95
|
-
}
|
|
96
|
-
export interface NoActionConfig extends BaseActionConfig {
|
|
97
|
-
action: "none";
|
|
98
|
-
}
|
|
99
|
-
export interface CustomActionConfig extends BaseActionConfig {
|
|
100
|
-
action: "fire-dom-event";
|
|
101
|
-
}
|
|
102
|
-
export interface BaseActionConfig {
|
|
103
|
-
confirmation?: ConfirmationRestrictionConfig;
|
|
104
|
-
}
|
|
105
|
-
export interface ConfirmationRestrictionConfig {
|
|
106
|
-
text?: string;
|
|
107
|
-
exemptions?: RestrictionConfig[];
|
|
108
|
-
}
|
|
109
|
-
export interface RestrictionConfig {
|
|
110
|
-
user: string;
|
|
111
|
-
}
|
|
112
|
-
export {};
|