mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-06-17 20:08:33 +00:00
Restyled by prettier
This commit is contained in:
107
web/main.js
107
web/main.js
@ -14,8 +14,7 @@ async function get_entities() {
|
|||||||
Authorization: `Bearer ${api_token}`,
|
Authorization: `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
body :
|
body: `{"template":"[{% for entity in states %}[\\"{{ entity.entity_id }}\\",\\"{{ entity.name }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||||
`{"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');
|
||||||
@ -42,8 +41,7 @@ async function get_devices() {
|
|||||||
Authorization: `Bearer ${api_token}`,
|
Authorization: `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
body :
|
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 %}]"}`,
|
||||||
`{"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');
|
||||||
@ -70,8 +68,7 @@ async function get_areas() {
|
|||||||
Authorization: `Bearer ${api_token}`,
|
Authorization: `Bearer ${api_token}`,
|
||||||
},
|
},
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
body :
|
body: `{"template":"[{% for area in areas() %}[\\"{{ area }}\\",\\"{{ area_name(area) }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||||
`{"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');
|
||||||
@ -127,7 +124,8 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,8 +423,7 @@ 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
|
||||||
@ -434,8 +431,7 @@ async function loadSchema() {
|
|||||||
validate: true,
|
validate: true,
|
||||||
schemas: [
|
schemas: [
|
||||||
{
|
{
|
||||||
uri :
|
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
|
||||||
fileMatch: [window.modelUri.toString()],
|
fileMatch: [window.modelUri.toString()],
|
||||||
schema,
|
schema,
|
||||||
},
|
},
|
||||||
@ -453,8 +449,7 @@ 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(
|
var modelUri = monaco.Uri.parse('/config/www/garmin/menu.json'); // a made up unique URI for our model
|
||||||
'/config/www/garmin/menu.json'); // a made up unique URI for our model
|
|
||||||
window.modelUri = modelUri;
|
window.modelUri = modelUri;
|
||||||
|
|
||||||
if (schema) {
|
if (schema) {
|
||||||
@ -463,8 +458,7 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
validate: true,
|
validate: true,
|
||||||
schemas: [
|
schemas: [
|
||||||
{
|
{
|
||||||
uri :
|
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
|
||||||
fileMatch: [modelUri.toString()],
|
fileMatch: [modelUri.toString()],
|
||||||
schema,
|
schema,
|
||||||
},
|
},
|
||||||
@ -629,13 +623,18 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
|
|
||||||
setInterval(checkRemoteMenu, 30000);
|
setInterval(checkRemoteMenu, 30000);
|
||||||
|
|
||||||
var model = monaco.editor.createModel(localStorage.getItem('json') ?? '{}',
|
var model = monaco.editor.createModel(
|
||||||
'json', modelUri);
|
localStorage.getItem('json') ?? '{}',
|
||||||
|
'json',
|
||||||
|
modelUri
|
||||||
|
);
|
||||||
|
|
||||||
monaco.editor.defineTheme(
|
monaco.editor.defineTheme(
|
||||||
'mocha',
|
'mocha',
|
||||||
await fetch('https://josephabbey.github.io/catppuccin-monaco/mocha.json')
|
await fetch(
|
||||||
.then((r) => r.json()));
|
'https://josephabbey.github.io/catppuccin-monaco/mocha.json'
|
||||||
|
).then((r) => r.json())
|
||||||
|
);
|
||||||
|
|
||||||
monaco.languages.registerCompletionItemProvider('json', {
|
monaco.languages.registerCompletionItemProvider('json', {
|
||||||
triggerCharacters: ['.'],
|
triggerCharacters: ['.'],
|
||||||
@ -648,7 +647,8 @@ 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: [] };
|
||||||
}
|
}
|
||||||
@ -659,8 +659,10 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
i--;
|
i--;
|
||||||
} while (i >= 0 &&
|
} while (
|
||||||
word.word[i].toUpperCase() != word.word[i].toLowerCase());
|
i >= 0 &&
|
||||||
|
word.word[i].toUpperCase() != word.word[i].toLowerCase()
|
||||||
|
);
|
||||||
i++;
|
i++;
|
||||||
var range = {
|
var range = {
|
||||||
startLineNumber: position.lineNumber,
|
startLineNumber: position.lineNumber,
|
||||||
@ -669,8 +671,7 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
endColumn: word.endColumn,
|
endColumn: word.endColumn,
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
suggestions : Object.entries(entities).map(
|
suggestions: Object.entries(entities).map(([entity, name]) => ({
|
||||||
([ entity, name ]) => ({
|
|
||||||
label: entity,
|
label: entity,
|
||||||
kind: monaco.languages.CompletionItemKind.Variable,
|
kind: monaco.languages.CompletionItemKind.Variable,
|
||||||
documentation: name,
|
documentation: name,
|
||||||
@ -692,7 +693,8 @@ 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'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -700,7 +702,9 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
|
|
||||||
let markers = [];
|
let markers = [];
|
||||||
|
|
||||||
const renderTemplate = editor.addCommand(0, async function(_, template) {
|
const renderTemplate = editor.addCommand(
|
||||||
|
0,
|
||||||
|
async function (_, template) {
|
||||||
const t = toast({
|
const t = toast({
|
||||||
text: 'Rendering template...',
|
text: 'Rendering template...',
|
||||||
});
|
});
|
||||||
@ -740,9 +744,13 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
});
|
});
|
||||||
document.querySelector('#api_url').classList.add('invalid');
|
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('.');
|
const service = action.tap_action.service.split('.');
|
||||||
let data = action.tap_action.data;
|
let data = action.tap_action.data;
|
||||||
if (data) {
|
if (data) {
|
||||||
@ -756,15 +764,17 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
text: 'Running action...',
|
text: 'Running action...',
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const res =
|
const res = await fetch(
|
||||||
await fetch(api_url + '/services/' + service[0] + '/' + service[1], {
|
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({
|
||||||
@ -801,15 +811,21 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
});
|
});
|
||||||
document.querySelector('#api_url').classList.add('invalid');
|
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 entity = item.entity.split('.');
|
||||||
const t = toast({
|
const t = toast({
|
||||||
text: 'Toggling...',
|
text: 'Toggling...',
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const res = await fetch(api_url + '/services/' + entity[0] + '/toggle', {
|
const res = await fetch(
|
||||||
|
api_url + '/services/' + entity[0] + '/toggle',
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${api_token}`,
|
Authorization: `Bearer ${api_token}`,
|
||||||
@ -818,7 +834,8 @@ 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({
|
||||||
@ -855,7 +872,9 @@ 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 {
|
||||||
@ -1016,8 +1035,7 @@ 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();
|
||||||
@ -1075,8 +1093,10 @@ require([ 'vs/editor/editor.main' ], async () => {
|
|||||||
arguments: [node.value[0].value],
|
arguments: [node.value[0].value],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if (node.key[0].value === 'type' &&
|
} else if (
|
||||||
node.value[0].value === 'toggle') {
|
node.key[0].value === 'type' &&
|
||||||
|
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,
|
||||||
@ -1105,13 +1125,14 @@ 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) { return codeLens; },
|
resolveCodeLens: function (model, codeLens, token) {
|
||||||
|
return codeLens;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user