From 48df45059b2ccec315992e71fabb753c5ca71be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20T=C3=B3th?= Date: Sun, 16 Jun 2024 17:17:41 +0200 Subject: [PATCH 1/3] Small typo fix. --- src/purifier-card.ts | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/purifier-card.ts b/src/purifier-card.ts index 8de6205..47fbc87 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,14 +103,14 @@ export class PurifierCard extends LitElement { { bubbles: false, composed: true, - } + }, ); } private callService( service: ServiceCallRequest['service'], options: ServiceCallRequest['serviceData'] = {}, - request = true + request = true, ) { const [domain, name] = service.split('.'); this.hass.callService(domain, name, { @@ -161,16 +161,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 +315,7 @@ export class PurifierCard extends LitElement {
${subtitle}
`; - } + }, ); return stats.length ? html`
${stats}
` : nothing; @@ -362,7 +361,7 @@ export class PurifierCard extends LitElement { > `; - } + }, ); return html` From e6697382bb854cd3bc077a071a4fa431daa6a860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20T=C3=B3th?= Date: Sun, 16 Jun 2024 17:19:12 +0200 Subject: [PATCH 2/3] Extended callService function with target to make it possible to call service on a custom target. Extended README.md with new target property. --- README.md | 1 + src/purifier-card.ts | 26 ++++++++++++++++++++------ src/types.ts | 2 ++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d0c480a..ca76899 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/purifier-card.ts b/src/purifier-card.ts index 47fbc87..4f915d2 100644 --- a/src/purifier-card.ts +++ b/src/purifier-card.ts @@ -110,13 +110,19 @@ export class PurifierCard extends LitElement { private callService( service: ServiceCallRequest['service'], options: ServiceCallRequest['serviceData'] = {}, + 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; @@ -330,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) { diff --git a/src/types.ts b/src/types.ts index 183f3cf..22aa64f 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; } From b57cc420af1436f08c02eca133135bd47410041b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20T=C3=B3th?= Date: Sun, 16 Jun 2024 17:45:44 +0200 Subject: [PATCH 3/3] Run format and lint --- src/config.ts | 2 +- src/editor.ts | 13 ++++++------- src/localize.ts | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/config.ts b/src/config.ts index cc40ec9..7f6e2b1 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 041205b..c712c5b 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 6ec9510..34fed6c 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('.');