Script edit: Display conditions (#3042)

Added support so conditions will be displayed when script rules are viewed.

Signed-off-by: Jeff James <jeff@james-online.com>
l10n_crowdin_habpanel
jsjames 2025-02-18 02:41:37 -08:00 committed by GitHub
parent 5ad2b2bf79
commit 8b7f4700dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 5 deletions

View File

@ -426,9 +426,10 @@ export default {
if (this.loading) return
this.loading = true
Promise.all([this.$oh.api.get('/rest/module-types?type=trigger'), this.$oh.api.get('/rest/rules/' + this.ruleId)]).then((data) => {
Promise.all([this.$oh.api.get('/rest/module-types?type=trigger'), this.$oh.api.get('/rest/module-types?type=condition'), this.$oh.api.get('/rest/rules/' + this.ruleId)]).then((data) => {
this.$set(this.moduleTypes, 'triggers', data[0])
this.$set(this, 'rule', data[1])
this.$set(this.moduleTypes, 'conditions', data[1])
this.$set(this, 'rule', data[2])
if (this.moduleId) {
this.$set(this, 'currentModule', this.rule.actions.concat(this.rule.conditions).find((m) => m.id === this.moduleId))
@ -441,7 +442,7 @@ export default {
if (!this.rule.editable) {
const commentChar = AUTOMATION_LANGUAGES[this.mode].commentChar
let triggerDescriptionComments = `${commentChar} Triggers:\n`
let preamble = `${commentChar} Triggers:\n`
for (const trigger of this.rule.triggers) {
const triggerModuleType = this.moduleTypes.triggers.find((t) => t.uid === trigger.type)
let description = trigger.label || this.suggestedModuleTitle(trigger, triggerModuleType, 'trigger')
@ -450,9 +451,20 @@ export default {
} else {
description = 'When ' + description
}
triggerDescriptionComments += `${commentChar} - ${description}\n`
preamble += `${commentChar} - ${description}\n`
}
this.script = triggerDescriptionComments + '\n' + this.script
if (this.rule.conditions.length > 0) {
preamble += `\n${commentChar} Conditions:\n`
for (const condition of this.rule.conditions) {
const conditionModuleType = this.moduleTypes.conditions.find((t) => t.uid === condition.type)
let description = condition.label || this.suggestedModuleTitle(condition, conditionModuleType, 'condition')
description = 'Only If ' + description
preamble += `${commentChar} - ${description}\n`
}
}
this.script = preamble + '\n' + this.script
}
this.loadScriptModuleTypes().then(() => {