mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-05-02 05:32: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}`,
|
Authorization : `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode : 'cors',
|
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) {
|
if (res.status == 401 || res.status == 403) {
|
||||||
document.querySelector('#api_token').classList.add('invalid');
|
document.querySelector('#api_token').classList.add('invalid');
|
||||||
@ -41,7 +42,8 @@ async function get_devices() {
|
|||||||
Authorization : `Bearer ${api_token}`,
|
Authorization : `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode : 'cors',
|
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) {
|
if (res.status == 401 || res.status == 403) {
|
||||||
document.querySelector('#api_token').classList.add('invalid');
|
document.querySelector('#api_token').classList.add('invalid');
|
||||||
@ -68,7 +70,8 @@ async function get_areas() {
|
|||||||
Authorization : `Bearer ${api_token}`,
|
Authorization : `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode : 'cors',
|
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) {
|
if (res.status == 401 || res.status == 403) {
|
||||||
document.querySelector('#api_token').classList.add('invalid');
|
document.querySelector('#api_token').classList.add('invalid');
|
||||||
@ -85,7 +88,9 @@ async function get_areas() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all services in HomeAssistant.
|
* 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() {
|
async function get_services() {
|
||||||
try {
|
try {
|
||||||
@ -122,8 +127,7 @@ async function get_services() {
|
|||||||
*/
|
*/
|
||||||
async function get_schema() {
|
async function get_schema() {
|
||||||
const res = await fetch(
|
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();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +136,9 @@ async function get_schema() {
|
|||||||
* @param {Record<string, string>} entities
|
* @param {Record<string, string>} entities
|
||||||
* @param {Record<string, string>} devices
|
* @param {Record<string, string>} devices
|
||||||
* @param {Record<string, string>} areas
|
* @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
|
* @param {{}} schema
|
||||||
* @returns {Promise<{}>}
|
* @returns {Promise<{}>}
|
||||||
*/
|
*/
|
||||||
@ -419,7 +425,8 @@ async function loadSchema() {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
schema = await generate_schema(entities, devices, areas, services, schema);
|
schema = await generate_schema(entities, devices, areas, services, schema);
|
||||||
} catch {}
|
} catch {
|
||||||
|
}
|
||||||
console.log(schema);
|
console.log(schema);
|
||||||
if (window.m && window.modelUri) {
|
if (window.m && window.modelUri) {
|
||||||
// configure the JSON language support with schemas and schema associations
|
// configure the JSON language support with schemas and schema associations
|
||||||
@ -427,7 +434,8 @@ async function loadSchema() {
|
|||||||
validate : true,
|
validate : true,
|
||||||
schemas : [
|
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() ],
|
fileMatch : [ window.modelUri.toString() ],
|
||||||
schema,
|
schema,
|
||||||
},
|
},
|
||||||
@ -445,7 +453,8 @@ require.config({
|
|||||||
});
|
});
|
||||||
require([ 'vs/editor/editor.main' ], async () => {
|
require([ 'vs/editor/editor.main' ], async () => {
|
||||||
window.m = monaco;
|
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;
|
window.modelUri = modelUri;
|
||||||
|
|
||||||
if (schema) {
|
if (schema) {
|
||||||
@ -454,7 +463,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
validate : true,
|
validate : true,
|
||||||
schemas : [
|
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() ],
|
fileMatch : [ modelUri.toString() ],
|
||||||
schema,
|
schema,
|
||||||
},
|
},
|
||||||
@ -619,18 +629,13 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
|
|
||||||
setInterval(checkRemoteMenu, 30000);
|
setInterval(checkRemoteMenu, 30000);
|
||||||
|
|
||||||
var model = monaco.editor.createModel(
|
var model = monaco.editor.createModel(localStorage.getItem('json') ?? '{}',
|
||||||
localStorage.getItem('json') ?? '{}',
|
'json', modelUri);
|
||||||
'json',
|
|
||||||
modelUri
|
|
||||||
);
|
|
||||||
|
|
||||||
monaco.editor.defineTheme(
|
monaco.editor.defineTheme(
|
||||||
'mocha',
|
'mocha',
|
||||||
await fetch(
|
await fetch('https://josephabbey.github.io/catppuccin-monaco/mocha.json')
|
||||||
'https://josephabbey.github.io/catppuccin-monaco/mocha.json'
|
.then((r) => r.json()));
|
||||||
).then((r) => r.json())
|
|
||||||
);
|
|
||||||
|
|
||||||
monaco.languages.registerCompletionItemProvider('json', {
|
monaco.languages.registerCompletionItemProvider('json', {
|
||||||
triggerCharacters : [ '.' ],
|
triggerCharacters : [ '.' ],
|
||||||
@ -643,8 +648,7 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
endColumn : position.column,
|
endColumn : position.column,
|
||||||
});
|
});
|
||||||
var match = /"content"\s*:\s*"[^"]*[^\w]?\w+\.[^.\s{}()[\]'"]*$/.test(
|
var match = /"content"\s*:\s*"[^"]*[^\w]?\w+\.[^.\s{}()[\]'"]*$/.test(
|
||||||
textUntilPosition
|
textUntilPosition);
|
||||||
);
|
|
||||||
if (!match) {
|
if (!match) {
|
||||||
return {suggestions : []};
|
return {suggestions : []};
|
||||||
}
|
}
|
||||||
@ -655,10 +659,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
i--;
|
i--;
|
||||||
} while (
|
} while (i >= 0 &&
|
||||||
i >= 0 &&
|
word.word[i].toUpperCase() != word.word[i].toLowerCase());
|
||||||
word.word[i].toUpperCase() != word.word[i].toLowerCase()
|
|
||||||
);
|
|
||||||
i++;
|
i++;
|
||||||
var range = {
|
var range = {
|
||||||
startLineNumber : position.lineNumber,
|
startLineNumber : position.lineNumber,
|
||||||
@ -667,7 +669,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
endColumn : word.endColumn,
|
endColumn : word.endColumn,
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
suggestions: Object.entries(entities).map(([entity, name]) => ({
|
suggestions : Object.entries(entities).map(
|
||||||
|
([ entity, name ]) => ({
|
||||||
label : entity,
|
label : entity,
|
||||||
kind : monaco.languages.CompletionItemKind.Variable,
|
kind : monaco.languages.CompletionItemKind.Variable,
|
||||||
documentation : name,
|
documentation : name,
|
||||||
@ -689,8 +692,7 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
if (e.key == 's' && e.ctrlKey) {
|
if (e.key == 's' && e.ctrlKey) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
model.setValue(
|
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 = [];
|
let markers = [];
|
||||||
|
|
||||||
const renderTemplate = editor.addCommand(
|
const renderTemplate = editor.addCommand(0, async function(_, template) {
|
||||||
0,
|
|
||||||
async function (_, template) {
|
|
||||||
const t = toast({
|
const t = toast({
|
||||||
text : 'Rendering template...',
|
text : 'Rendering template...',
|
||||||
});
|
});
|
||||||
@ -740,13 +740,9 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
});
|
});
|
||||||
document.querySelector('#api_url').classList.add('invalid');
|
document.querySelector('#api_url').classList.add('invalid');
|
||||||
}
|
}
|
||||||
},
|
}, '');
|
||||||
''
|
|
||||||
);
|
|
||||||
|
|
||||||
const runAction = editor.addCommand(
|
const runAction = editor.addCommand(0, async function(_, action) {
|
||||||
0,
|
|
||||||
async function (_, action) {
|
|
||||||
const service = action.tap_action.service.split('.');
|
const service = action.tap_action.service.split('.');
|
||||||
let data = action.tap_action.data;
|
let data = action.tap_action.data;
|
||||||
if (data) {
|
if (data) {
|
||||||
@ -760,17 +756,15 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
text : 'Running action...',
|
text : 'Running action...',
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const res = await fetch(
|
const res =
|
||||||
api_url + '/services/' + service[0] + '/' + service[1],
|
await fetch(api_url + '/services/' + service[0] + '/' + service[1], {
|
||||||
{
|
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
headers : {
|
headers : {
|
||||||
Authorization : `Bearer ${api_token}`,
|
Authorization : `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode : 'cors',
|
mode : 'cors',
|
||||||
body : JSON.stringify(data),
|
body : JSON.stringify(data),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
t.hideToast();
|
t.hideToast();
|
||||||
if (res.status == 200) {
|
if (res.status == 200) {
|
||||||
toast({
|
toast({
|
||||||
@ -807,21 +801,15 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
});
|
});
|
||||||
document.querySelector('#api_url').classList.add('invalid');
|
document.querySelector('#api_url').classList.add('invalid');
|
||||||
}
|
}
|
||||||
},
|
}, '');
|
||||||
''
|
|
||||||
);
|
|
||||||
|
|
||||||
const toggle = editor.addCommand(
|
const toggle = editor.addCommand(0, async function(_, item) {
|
||||||
0,
|
|
||||||
async function (_, item) {
|
|
||||||
const entity = item.entity.split('.');
|
const entity = item.entity.split('.');
|
||||||
const t = toast({
|
const t = toast({
|
||||||
text : 'Toggling...',
|
text : 'Toggling...',
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const res = await fetch(
|
const res = await fetch(api_url + '/services/' + entity[0] + '/toggle', {
|
||||||
api_url + '/services/' + entity[0] + '/toggle',
|
|
||||||
{
|
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
headers : {
|
headers : {
|
||||||
Authorization : `Bearer ${api_token}`,
|
Authorization : `Bearer ${api_token}`,
|
||||||
@ -830,8 +818,7 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
body : JSON.stringify({
|
body : JSON.stringify({
|
||||||
entity_id : item.entity,
|
entity_id : item.entity,
|
||||||
}),
|
}),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
t.hideToast();
|
t.hideToast();
|
||||||
if (res.status == 200) {
|
if (res.status == 200) {
|
||||||
toast({
|
toast({
|
||||||
@ -868,9 +855,7 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
});
|
});
|
||||||
document.querySelector('#api_url').classList.add('invalid');
|
document.querySelector('#api_url').classList.add('invalid');
|
||||||
}
|
}
|
||||||
},
|
}, '');
|
||||||
''
|
|
||||||
);
|
|
||||||
|
|
||||||
async function makeMarkers() {
|
async function makeMarkers() {
|
||||||
try {
|
try {
|
||||||
@ -952,7 +937,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
}
|
}
|
||||||
const templates = [];
|
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
|
* @param {string[]} path
|
||||||
*/
|
*/
|
||||||
function recurse(node, path) {
|
function recurse(node, path) {
|
||||||
@ -1030,7 +1016,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
await Promise.all(templates.map((t) => testTemplate(...t)));
|
await Promise.all(templates.map((t) => testTemplate(...t)));
|
||||||
toggles.forEach((t) => testToggle(...t));
|
toggles.forEach((t) => testToggle(...t));
|
||||||
monaco.editor.setModelMarkers(model, 'template', markers);
|
monaco.editor.setModelMarkers(model, 'template', markers);
|
||||||
} catch {}
|
} catch {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
window.makeMarkers = makeMarkers;
|
window.makeMarkers = makeMarkers;
|
||||||
makeMarkers();
|
makeMarkers();
|
||||||
@ -1047,7 +1034,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
const ast = json.parse(model.getValue());
|
const ast = json.parse(model.getValue());
|
||||||
const data = 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
|
* @param {string[]} path
|
||||||
*/
|
*/
|
||||||
function recurse(node, path) {
|
function recurse(node, path) {
|
||||||
@ -1087,10 +1075,8 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
arguments : [ node.value[0].value ],
|
arguments : [ node.value[0].value ],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if (
|
} else if (node.key[0].value === 'type' &&
|
||||||
node.key[0].value === 'type' &&
|
node.value[0].value === 'toggle') {
|
||||||
node.value[0].value === 'toggle'
|
|
||||||
) {
|
|
||||||
lenses.push({
|
lenses.push({
|
||||||
range : {
|
range : {
|
||||||
startLineNumber : node.key[0].range.start.line + 1,
|
startLineNumber : node.key[0].range.start.line + 1,
|
||||||
@ -1119,14 +1105,13 @@ require(['vs/editor/editor.main'], async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
recurse(ast.body[0], []);
|
recurse(ast.body[0], []);
|
||||||
} catch {}
|
} catch {
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
lenses,
|
lenses,
|
||||||
dispose : () => {},
|
dispose : () => {},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
resolveCodeLens: function (model, codeLens, token) {
|
resolveCodeLens : function(model, codeLens, token) { return codeLens; },
|
||||||
return codeLens;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user