mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-04-30 20:52:27 +00:00
Restyled by clang-format
This commit is contained in:
121
web/main.js
121
web/main.js
@ -14,7 +14,8 @@ async function get_entities() {
|
||||
Authorization : `Bearer ${api_token}`,
|
||||
},
|
||||
mode : 'cors',
|
||||
body: `{"template":"[{% for entity in states %}[\\"{{ entity.entity_id }}\\",\\"{{ entity.name }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body :
|
||||
`{"template":"[{% for entity in states %}[\\"{{ entity.entity_id }}\\",\\"{{ entity.name }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -41,7 +42,8 @@ async function get_devices() {
|
||||
Authorization : `Bearer ${api_token}`,
|
||||
},
|
||||
mode : 'cors',
|
||||
body: `{"template":"{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq', None) | list %}[{% for device in devices %}[\\"{{ device }}\\",\\"{{ device_attr(device, 'name') }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body :
|
||||
`{"template":"{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq', None) | list %}[{% for device in devices %}[\\"{{ device }}\\",\\"{{ device_attr(device, 'name') }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -68,7 +70,8 @@ async function get_areas() {
|
||||
Authorization : `Bearer ${api_token}`,
|
||||
},
|
||||
mode : 'cors',
|
||||
body: `{"template":"[{% for area in areas() %}[\\"{{ area }}\\",\\"{{ area_name(area) }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body :
|
||||
`{"template":"[{% for area in areas() %}[\\"{{ area }}\\",\\"{{ area_name(area) }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -85,7 +88,9 @@ async function get_areas() {
|
||||
|
||||
/**
|
||||
* Get all services in HomeAssistant.
|
||||
* @returns {Promise<[string, { name: string; description: string; fields: Record<string, { name: string; description: string; example: string; selector: unknown; required?: boolean }> }][]>} [id, data]
|
||||
* @returns {Promise<[string, { name: string; description: string; fields:
|
||||
* Record<string, { name: string; description: string; example: string;
|
||||
* selector: unknown; required?: boolean }> }][]>} [id, data]
|
||||
*/
|
||||
async function get_services() {
|
||||
try {
|
||||
@ -122,8 +127,7 @@ async function get_services() {
|
||||
*/
|
||||
async function get_schema() {
|
||||
const res = await fetch(
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json'
|
||||
);
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json');
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@ -132,7 +136,9 @@ async function get_schema() {
|
||||
* @param {Record<string, string>} entities
|
||||
* @param {Record<string, string>} devices
|
||||
* @param {Record<string, string>} areas
|
||||
* @param {[string, { name: string; description: string; fields: Record<string, { name: string; description: string; example: string; selector: unknown; required?: boolean }> }][]} services
|
||||
* @param {[string, { name: string; description: string; fields:
|
||||
* Record<string, { name: string; description: string; example: string;
|
||||
* selector: unknown; required?: boolean }> }][]} services
|
||||
* @param {{}} schema
|
||||
* @returns {Promise<{}>}
|
||||
*/
|
||||
@ -419,7 +425,8 @@ async function loadSchema() {
|
||||
}
|
||||
try {
|
||||
schema = await generate_schema(entities, devices, areas, services, schema);
|
||||
} catch {}
|
||||
} catch {
|
||||
}
|
||||
console.log(schema);
|
||||
if (window.m && window.modelUri) {
|
||||
// configure the JSON language support with schemas and schema associations
|
||||
@ -427,7 +434,8 @@ async function loadSchema() {
|
||||
validate : true,
|
||||
schemas : [
|
||||
{
|
||||
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
uri :
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
fileMatch : [ window.modelUri.toString() ],
|
||||
schema,
|
||||
},
|
||||
@ -445,7 +453,8 @@ require.config({
|
||||
});
|
||||
require([ 'vs/editor/editor.main' ], async () => {
|
||||
window.m = monaco;
|
||||
var modelUri = monaco.Uri.parse('/config/www/garmin/menu.json'); // a made up unique URI for our model
|
||||
var modelUri = monaco.Uri.parse(
|
||||
'/config/www/garmin/menu.json'); // a made up unique URI for our model
|
||||
window.modelUri = modelUri;
|
||||
|
||||
if (schema) {
|
||||
@ -454,7 +463,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
validate : true,
|
||||
schemas : [
|
||||
{
|
||||
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
uri :
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
fileMatch : [ modelUri.toString() ],
|
||||
schema,
|
||||
},
|
||||
@ -619,18 +629,13 @@ require(['vs/editor/editor.main'], async () => {
|
||||
|
||||
setInterval(checkRemoteMenu, 30000);
|
||||
|
||||
var model = monaco.editor.createModel(
|
||||
localStorage.getItem('json') ?? '{}',
|
||||
'json',
|
||||
modelUri
|
||||
);
|
||||
var model = monaco.editor.createModel(localStorage.getItem('json') ?? '{}',
|
||||
'json', modelUri);
|
||||
|
||||
monaco.editor.defineTheme(
|
||||
'mocha',
|
||||
await fetch(
|
||||
'https://josephabbey.github.io/catppuccin-monaco/mocha.json'
|
||||
).then((r) => r.json())
|
||||
);
|
||||
await fetch('https://josephabbey.github.io/catppuccin-monaco/mocha.json')
|
||||
.then((r) => r.json()));
|
||||
|
||||
monaco.languages.registerCompletionItemProvider('json', {
|
||||
triggerCharacters : [ '.' ],
|
||||
@ -643,8 +648,7 @@ require(['vs/editor/editor.main'], async () => {
|
||||
endColumn : position.column,
|
||||
});
|
||||
var match = /"content"\s*:\s*"[^"]*[^\w]?\w+\.[^.\s{}()[\]'"]*$/.test(
|
||||
textUntilPosition
|
||||
);
|
||||
textUntilPosition);
|
||||
if (!match) {
|
||||
return {suggestions : []};
|
||||
}
|
||||
@ -655,10 +659,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
}
|
||||
do {
|
||||
i--;
|
||||
} while (
|
||||
i >= 0 &&
|
||||
word.word[i].toUpperCase() != word.word[i].toLowerCase()
|
||||
);
|
||||
} while (i >= 0 &&
|
||||
word.word[i].toUpperCase() != word.word[i].toLowerCase());
|
||||
i++;
|
||||
var range = {
|
||||
startLineNumber : position.lineNumber,
|
||||
@ -667,7 +669,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
endColumn : word.endColumn,
|
||||
};
|
||||
return {
|
||||
suggestions: Object.entries(entities).map(([entity, name]) => ({
|
||||
suggestions : Object.entries(entities).map(
|
||||
([ entity, name ]) => ({
|
||||
label : entity,
|
||||
kind : monaco.languages.CompletionItemKind.Variable,
|
||||
documentation : name,
|
||||
@ -689,8 +692,7 @@ require(['vs/editor/editor.main'], async () => {
|
||||
if (e.key == 's' && e.ctrlKey) {
|
||||
e.preventDefault();
|
||||
model.setValue(
|
||||
JSON.stringify(JSON.parse(editor.getValue()), undefined, 2) + '\n'
|
||||
);
|
||||
JSON.stringify(JSON.parse(editor.getValue()), undefined, 2) + '\n');
|
||||
}
|
||||
});
|
||||
|
||||
@ -698,9 +700,7 @@ require(['vs/editor/editor.main'], async () => {
|
||||
|
||||
let markers = [];
|
||||
|
||||
const renderTemplate = editor.addCommand(
|
||||
0,
|
||||
async function (_, template) {
|
||||
const renderTemplate = editor.addCommand(0, async function(_, template) {
|
||||
const t = toast({
|
||||
text : 'Rendering template...',
|
||||
});
|
||||
@ -740,13 +740,9 @@ require(['vs/editor/editor.main'], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
},
|
||||
''
|
||||
);
|
||||
}, '');
|
||||
|
||||
const runAction = editor.addCommand(
|
||||
0,
|
||||
async function (_, action) {
|
||||
const runAction = editor.addCommand(0, async function(_, action) {
|
||||
const service = action.tap_action.service.split('.');
|
||||
let data = action.tap_action.data;
|
||||
if (data) {
|
||||
@ -760,17 +756,15 @@ require(['vs/editor/editor.main'], async () => {
|
||||
text : 'Running action...',
|
||||
});
|
||||
try {
|
||||
const res = await fetch(
|
||||
api_url + '/services/' + service[0] + '/' + service[1],
|
||||
{
|
||||
const res =
|
||||
await fetch(api_url + '/services/' + service[0] + '/' + service[1], {
|
||||
method : 'POST',
|
||||
headers : {
|
||||
Authorization : `Bearer ${api_token}`,
|
||||
},
|
||||
mode : 'cors',
|
||||
body : JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
});
|
||||
t.hideToast();
|
||||
if (res.status == 200) {
|
||||
toast({
|
||||
@ -807,21 +801,15 @@ require(['vs/editor/editor.main'], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
},
|
||||
''
|
||||
);
|
||||
}, '');
|
||||
|
||||
const toggle = editor.addCommand(
|
||||
0,
|
||||
async function (_, item) {
|
||||
const toggle = editor.addCommand(0, async function(_, item) {
|
||||
const entity = item.entity.split('.');
|
||||
const t = toast({
|
||||
text : 'Toggling...',
|
||||
});
|
||||
try {
|
||||
const res = await fetch(
|
||||
api_url + '/services/' + entity[0] + '/toggle',
|
||||
{
|
||||
const res = await fetch(api_url + '/services/' + entity[0] + '/toggle', {
|
||||
method : 'POST',
|
||||
headers : {
|
||||
Authorization : `Bearer ${api_token}`,
|
||||
@ -830,8 +818,7 @@ require(['vs/editor/editor.main'], async () => {
|
||||
body : JSON.stringify({
|
||||
entity_id : item.entity,
|
||||
}),
|
||||
}
|
||||
);
|
||||
});
|
||||
t.hideToast();
|
||||
if (res.status == 200) {
|
||||
toast({
|
||||
@ -868,9 +855,7 @@ require(['vs/editor/editor.main'], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
},
|
||||
''
|
||||
);
|
||||
}, '');
|
||||
|
||||
async function makeMarkers() {
|
||||
try {
|
||||
@ -952,7 +937,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
}
|
||||
const templates = [];
|
||||
/**
|
||||
* @param {import('json-ast-comments').JsonAst | import('json-ast-comments').JsonProperty} node
|
||||
* @param {import('json-ast-comments').JsonAst |
|
||||
* import('json-ast-comments').JsonProperty} node
|
||||
* @param {string[]} path
|
||||
*/
|
||||
function recurse(node, path) {
|
||||
@ -1030,7 +1016,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
await Promise.all(templates.map((t) => testTemplate(...t)));
|
||||
toggles.forEach((t) => testToggle(...t));
|
||||
monaco.editor.setModelMarkers(model, 'template', markers);
|
||||
} catch {}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
window.makeMarkers = makeMarkers;
|
||||
makeMarkers();
|
||||
@ -1047,7 +1034,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
const ast = json.parse(model.getValue());
|
||||
const data = JSON.parse(model.getValue());
|
||||
/**
|
||||
* @param {import('json-ast-comments').JsonAst | import('json-ast-comments').JsonProperty} node
|
||||
* @param {import('json-ast-comments').JsonAst |
|
||||
* import('json-ast-comments').JsonProperty} node
|
||||
* @param {string[]} path
|
||||
*/
|
||||
function recurse(node, path) {
|
||||
@ -1087,10 +1075,8 @@ require(['vs/editor/editor.main'], async () => {
|
||||
arguments : [ node.value[0].value ],
|
||||
},
|
||||
});
|
||||
} else if (
|
||||
node.key[0].value === 'type' &&
|
||||
node.value[0].value === 'toggle'
|
||||
) {
|
||||
} else if (node.key[0].value === 'type' &&
|
||||
node.value[0].value === 'toggle') {
|
||||
lenses.push({
|
||||
range : {
|
||||
startLineNumber : node.key[0].range.start.line + 1,
|
||||
@ -1119,14 +1105,13 @@ require(['vs/editor/editor.main'], async () => {
|
||||
}
|
||||
}
|
||||
recurse(ast.body[0], []);
|
||||
} catch {}
|
||||
} catch {
|
||||
}
|
||||
return {
|
||||
lenses,
|
||||
dispose : () => {},
|
||||
};
|
||||
},
|
||||
resolveCodeLens: function (model, codeLens, token) {
|
||||
return codeLens;
|
||||
},
|
||||
resolveCodeLens : function(model, codeLens, token) { return codeLens; },
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user