From 861a52cfea02b6920c6b9433ff8223fe8cf83658 Mon Sep 17 00:00:00 2001 From: Daniel Toth Date: Sun, 16 Jun 2024 20:40:32 +0200 Subject: [PATCH] feat: Extend callService with target to make it possible to call a service on a custom entity. (#363) * Small typo fix. * Extended callService function with target to make it possible to call service on a custom target. Extended README.md with new target property. * Run format and lint --- README.md | 1 + src/config.ts | 2 +- src/editor.ts | 13 +++++----- src/localize.ts | 2 +- src/purifier-card.ts | 61 +++++++++++++++++++++++++++----------------- src/types.ts | 2 ++ 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d0c480a9..ca76899e 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ You can define [custom scripts][ha-scripts] for custom actions or add shortcuts | `name` | `string` | Optional | Friendly name of the shortcut, i.e. `Switch to Auto`. | | `icon` | `string` | Optional | Any icon for shortcut button. | | `service` | `string` | Optional | A service to call, i.e. `script.clean_air`. | +| `target` | `object` | Optional | A `HassServiceTarget`, to define a target for the current service call. | | `service_data` | `object` | Optional | `service_data` for `service` call | | `percentage` | `object` | Optional | A `percentage` to switch to, i.e. `27`, etc. See `entity`'s `percentage_step` for valid values. | | `preset_mode` | `object` | Optional | A `speed` to switch to, i.e. `Auto`, etc | diff --git a/src/config.ts b/src/config.ts index cc40ec90..7f6e2b1b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,7 +2,7 @@ import { PurifierCardConfig } from './types'; import localize from './localize'; export default function buildConfig( - config?: Partial + config?: Partial, ): PurifierCardConfig { if (!config) { throw new Error(localize('error.invalid_config')); diff --git a/src/editor.ts b/src/editor.ts index 041205ba..c712c5b0 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -63,9 +63,8 @@ export class PurifierCardEditor extends LitElement { validationMessage=${localize('error.missing_entity')} > ${fanEntities.map( - (entity) => html`${entity}` + (entity) => + html`${entity}`, )} @@ -75,7 +74,7 @@ export class PurifierCardEditor extends LitElement { aria-label=${localize( this.compact_view ? 'editor.compact_view_aria_label_off' - : 'editor.compact_view_aria_label_on' + : 'editor.compact_view_aria_label_on', )} .checked=${this.compact_view} .configValue=${'compact_view'} @@ -90,7 +89,7 @@ export class PurifierCardEditor extends LitElement { aria-label=${localize( this.show_name ? 'editor.show_name_aria_label_off' - : 'editor.show_name_aria_label_on' + : 'editor.show_name_aria_label_on', )} .checked=${this.show_name} .configValue=${'show_name'} @@ -105,7 +104,7 @@ export class PurifierCardEditor extends LitElement { aria-label=${localize( this.show_state ? 'editor.show_state_aria_label_off' - : 'editor.show_state_aria_label_on' + : 'editor.show_state_aria_label_on', )} .checked=${this.show_state} .configValue=${'show_state'} @@ -120,7 +119,7 @@ export class PurifierCardEditor extends LitElement { aria-label=${localize( this.show_name ? 'editor.show_toolbar_aria_label_off' - : 'editor.show_toolbar_aria_label_on' + : 'editor.show_toolbar_aria_label_on', )} .checked=${this.show_toolbar} .configValue=${'show_toolbar'} diff --git a/src/localize.ts b/src/localize.ts index 6ec95103..34fed6cb 100644 --- a/src/localize.ts +++ b/src/localize.ts @@ -48,7 +48,7 @@ const DEFAULT_LANG = 'en'; export default function localize( str: string, search?: string, - replace?: string + replace?: string, ): string | undefined { const [section, key] = str.toLowerCase().split('.'); diff --git a/src/purifier-card.ts b/src/purifier-card.ts index 8de62050..4f915d2d 100644 --- a/src/purifier-card.ts +++ b/src/purifier-card.ts @@ -23,19 +23,19 @@ import buildConfig from './config'; registerTemplates(); -// String in the right side will be replaced by Rollup +// String on the right side will be replaced by Rollup const PKG_VERSION = 'PKG_VERSION_VALUE'; console.info( `%c PURIFIER-CARD %c ${PKG_VERSION} `, 'color: white; background: blue; font-weight: 700;', - 'color: blue; background: white; font-weight: 700;' + 'color: blue; background: white; font-weight: 700;', ); if (!customElements.get('ha-icon-button')) { customElements.define( 'ha-icon-button', - class extends (customElements.get('paper-icon-button') ?? HTMLElement) {} + class extends (customElements.get('paper-icon-button') ?? HTMLElement) {}, ); } @@ -58,7 +58,7 @@ export class PurifierCard extends LitElement { public static getStubConfig( _: unknown, - entities: string[] + entities: string[], ): Partial { const [purifierEntity] = entities.filter((eid) => eid.startsWith('fan')); @@ -103,20 +103,26 @@ export class PurifierCard extends LitElement { { bubbles: false, composed: true, - } + }, ); } private callService( service: ServiceCallRequest['service'], options: ServiceCallRequest['serviceData'] = {}, - request = true + target?: ServiceCallRequest['target'], + request = true, ) { const [domain, name] = service.split('.'); - this.hass.callService(domain, name, { - entity_id: this.config.entity, - ...options, - }); + this.hass.callService( + domain, + name, + { + entity_id: this.config.entity, + ...options, + }, + target, + ); if (request) { this.requestInProgress = true; @@ -161,16 +167,15 @@ export class PurifierCard extends LitElement { ${preset_modes.map( - (item, index) => - html` - this.handlePresetMode(e)} - > - ${localize(`preset_mode.${item.toLowerCase()}`) || item} - - ` + (item, index) => html` + this.handlePresetMode(e)} + > + ${localize(`preset_mode.${item.toLowerCase()}`) || item} + + `, )} @@ -316,7 +321,7 @@ export class PurifierCard extends LitElement {
${subtitle}
`; - } + }, ); return stats.length ? html`
${stats}
` : nothing; @@ -331,10 +336,18 @@ export class PurifierCard extends LitElement { } const buttons = shortcuts.map( - ({ name, icon, service, service_data, preset_mode, percentage }) => { + ({ + name, + icon, + service, + service_data, + target, + preset_mode, + percentage, + }) => { const execute = () => { if (service) { - this.callService(service, service_data); + this.callService(service, target, service_data); } if (preset_mode) { @@ -362,7 +375,7 @@ export class PurifierCard extends LitElement { > `; - } + }, ); return html` diff --git a/src/types.ts b/src/types.ts index 183f3cfd..22aa64f0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,7 @@ import { HassEntityAttributeBase, HassEntityBase, + HassServiceTarget, } from 'home-assistant-js-websocket'; import { TemplateResult, nothing } from 'lit'; @@ -44,6 +45,7 @@ export interface PurifierCardShortcut { icon?: string; service?: string; service_data?: Record; + target?: HassServiceTarget; percentage?: number; preset_mode?: string; }