Skip to content

Commit

Permalink
Merge pull request #240 from cgiesche/feature/bugfixes_and_improvements
Browse files Browse the repository at this point in the history
Feature/bugfixes and improvements
  • Loading branch information
cgiesche authored Jan 31, 2024
2 parents 88b5832 + 99bac4f commit d2e48ad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Icon": "images/actionImage",
"Name": "Entity (custom icons)",
"PropertyInspectorPath": "pi.html",
"DisableAutomaticStates": true,
"States": [
{
"Image": "images/ha-button-off",
Expand Down
20 changes: 15 additions & 5 deletions src/components/ServiceCallConfiguration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
<details v-if="dataProperties && dataProperties.length > 0">
<summary>Available options</summary>
<div v-for="item in dataProperties" v-bind:key="item.name" class="form-text">
<span class="text-info font-monospace">{{ item.name }}&nbsp;</span>{{ item.info.description }}
<span class="text-info font-monospace">{{ item.name }}&nbsp;</span> <span class="text-warning font-monospace" v-if="item.info.required">(required) </span>{{ item.info.description }}
<template v-if="item.info.example">
<br>
<span class="ml-2">Example: <i>{{ item.info.example }}</i></span>
<span class="ml-2">Example: <i>{{ item.info.example }}</i></span>
</template>
</div>
</details>
Expand Down Expand Up @@ -156,8 +156,10 @@ const domainEntities = computed(() => {
return [];
}
let selectedService = props.availableServices.filter(service => service.serviceId === props.modelValue.serviceId)[0]
if (selectedService && selectedService.target && Array.isArray(selectedService.target.entity)) {
let targetDomains = selectedService.target.entity.filter(entity => entity.domain).flatMap(entity => entity.domain);
if (selectedService && selectedService.target && selectedService.target.entity) {
// target.entity may contain a single or an array of entities. Make sure we always work with array.
let targetEntities = ensureArray(selectedService.target.entity);
let targetDomains = targetEntities.filter(entity => entity.domain).flatMap(entity => ensureArray(entity.domain));
if (targetDomains.length > 0) {
return props.availableEntities.filter(entity => targetDomains.includes(entity.domain)).sort(titleSort);
} else {
Expand All @@ -173,7 +175,11 @@ const serviceDataInvalidFeedback = computed(() => {
return "";
}
try {
const renderedServiceData = nunjucks.renderString(serviceDataString, {ticks: 5, rotationPercent: 100, rotationAbsolute: 100});
const renderedServiceData = nunjucks.renderString(serviceDataString, {
ticks: 5,
rotationPercent: 100,
rotationAbsolute: 100
});
const json = JSON.parse(renderedServiceData);
return (typeof json === "object") ? "" : "Service data must be an JSON object."
Expand All @@ -199,4 +205,8 @@ const dataProperties = computed(() => {
})
function ensureArray(input) {
return Array.isArray(input) ? input : [input];
}
</script>
6 changes: 4 additions & 2 deletions src/modules/homeassistant/actions/service-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ export class ServiceAction extends Action {
if (typeof service !== 'string' || !service.trim()) {
throw new Error('Service must be a non-empty string')
}
if (!Array.isArray(entity_id)) {
if (entity_id && !Array.isArray(entity_id)) {
throw new TypeError('entity_id must be an array')
}
if (typeof serviceData !== 'object' || serviceData === null) {
throw new TypeError('serviceData must be an object')
}
if (entity_id) {
this.target = {entity_id: entity_id}
}

this.service = `${domain}.${service}`
this.data = serviceData
this.target = { entity_id: entity_id }
}
}
2 changes: 1 addition & 1 deletion src/modules/homeassistant/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Homeassistant {

callService(service, domain, entity_id = null, serviceData = null, callback = null) {
let executeScriptCmd = new ExecuteScriptCommand(this.nextRequestId(), [
new ServiceAction(domain, service, entity_id ? [entity_id] : [], serviceData || {})
new ServiceAction(domain, service, entity_id ? [entity_id]: null, serviceData || {})
])
this.sendCommand(executeScriptCmd, callback)
}
Expand Down

0 comments on commit d2e48ad

Please sign in to comment.