Update web editor

This commit is contained in:
Joseph Abbey
2025-10-20 09:46:19 +01:00
parent e697f75ce3
commit a52d6188be

View File

@@ -101,12 +101,12 @@ async function get_areas() {
} }
/** /**
* Get all services in HomeAssistant. * Get all actions in HomeAssistant.
* @returns {Promise<[string, { name: string; description: string; fields: * @returns {Promise<[string, { name: string; description: string; fields:
* Record<string, { name: string; description: string; example: string; * Record<string, { name: string; description: string; example: string;
* selector: unknown; required?: boolean }> }][]>} [id, data] * selector: unknown; required?: boolean }> }][]>} [id, data]
*/ */
async function get_services() { async function get_actions() {
try { try {
const res = await fetch(api_url + '/services', { const res = await fetch(api_url + '/services', {
method: 'GET', method: 'GET',
@@ -122,15 +122,15 @@ async function get_services() {
document.querySelector('#api_url').classList.remove('invalid'); document.querySelector('#api_url').classList.remove('invalid');
document.querySelector('#api_token').classList.remove('invalid'); document.querySelector('#api_token').classList.remove('invalid');
const data = await res.json(); const data = await res.json();
const services = []; const actions = [];
for (const d of data) { for (const d of data) {
for (const service in d.services) { for (const action in d.services) {
services.push([`${d.domain}.${service}`, d.services[service]]); actions.push([`${d.domain}.${action}`, d.services[action]]);
} }
} }
return services; return actions;
} catch (e) { } catch (e) {
console.error('Error fetching services:', e); console.error('Error fetching actions:', e);
document.querySelector('#api_url').classList.add('invalid'); document.querySelector('#api_url').classList.add('invalid');
return []; return [];
} }
@@ -154,11 +154,11 @@ async function get_schema() {
* @param {Record<string, string>} areas * @param {Record<string, string>} areas
* @param {[string, { name: string; description: string; fields: * @param {[string, { name: string; description: string; fields:
* Record<string, { name: string; description: string; example: string; * Record<string, { name: string; description: string; example: string;
* selector: unknown; required?: boolean }> }][]} services * selector: unknown; required?: boolean }> }][]} actions
* @param {{}} schema * @param {{}} schema
* @returns {Promise<{}>} * @returns {Promise<{}>}
*/ */
async function generate_schema(entities, devices, areas, services, schema) { async function generate_schema(entities, devices, areas, actions, schema) {
schema.$defs.entity = { schema.$defs.entity = {
enum: Object.keys(entities), enum: Object.keys(entities),
}; };
@@ -170,7 +170,7 @@ async function generate_schema(entities, devices, areas, services, schema) {
}; };
const oneOf = []; const oneOf = [];
for (const [id, data] of services) { for (const [id, data] of actions) {
const i_properties = { const i_properties = {
service: { service: {
title: data.name, title: data.name,
@@ -428,22 +428,22 @@ let entities;
let devices; let devices;
/** @type {Awaited<ReturnType<typeof get_areas>>} */ /** @type {Awaited<ReturnType<typeof get_areas>>} */
let areas; let areas;
/** @type {Awaited<ReturnType<typeof get_services>>} */ /** @type {Awaited<ReturnType<typeof get_actions>>} */
let services; let actions;
let schema; let schema;
async function loadSchema() { async function loadSchema() {
[entities, devices, areas, services, schema] = await Promise.all([ [entities, devices, areas, actions, schema] = await Promise.all([
get_entities(), get_entities(),
get_devices(), get_devices(),
get_areas(), get_areas(),
get_services(), get_actions(),
get_schema(), get_schema(),
]); ]);
if (window.makeMarkers) { if (window.makeMarkers) {
window.makeMarkers(); window.makeMarkers();
} }
try { try {
schema = await generate_schema(entities, devices, areas, services, schema); schema = await generate_schema(entities, devices, areas, actions, schema);
} catch {} } catch {}
console.log(schema); console.log(schema);
if (window.m && window.modelUri) { if (window.m && window.modelUri) {