From 1136ed6f5a863c8c3c8593e5836bd41c8484beb0 Mon Sep 17 00:00:00 2001 From: maxwroc Date: Sat, 24 Oct 2020 22:57:30 +0100 Subject: [PATCH] Allow simple string as an action #147 --- README.md | 42 +++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/battery-provider.ts | 4 ++-- src/utils.ts | 22 ++++++++++++++++++++- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27c76912..89f218fb 100644 --- a/README.md +++ b/README.md @@ -453,6 +453,48 @@ entities: secondary_info: "Motorola" # Static text ``` +### Tap actions + +![image](https://user-images.githubusercontent.com/8268674/97094268-62bf6200-164b-11eb-8d7d-344f9842f85e.png) + +```yaml +type: 'custom:battery-state-card'name: Click +color_gradient: + - '#ff0000' + - '#0000ff' + - '#00ff00' +entities: + - entity: sensor.bedroom_motion_battery_level + name: More info + tap_action: more-info + value_override: 100 + - entity: sensor.bathroom_motion_battery_level + name: Navigation path + tap_action: + action: navigate + navigation_path: /lovelace/1 + value_override: 0 + - entity: sensor.bedroomtemp_battery_level + name: Call service - Pushover + tap_action: + action: call-service + service: notify.pushover + service_data: + message: Call service works + title: Some title + value_override: 60 + - entity: sensor.bedroom_balcony_battery_level + name: Url + tap_action: + action: url + url_path: 'http://reddit.com' + value_override: 20 + - entity: sensor.bedroom_switch_battery_level + name: No action + value_override: 80 + +``` + ### Extra styles You can add CSS code which can change the appearance of the card. diff --git a/package.json b/package.json index 7f2a3a56..d081e3cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "1.5.0", + "version": "1.5.1", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "repository": { diff --git a/src/battery-provider.ts b/src/battery-provider.ts index 10532ec1..78876072 100644 --- a/src/battery-provider.ts +++ b/src/battery-provider.ts @@ -1,7 +1,7 @@ import BatteryViewModel from "./battery-vm"; import { IBatteryStateCardConfig, IFilter, FilterOperator, IBatteryEntity, IHomeAssistantGroupProps, IBatteriesResultViewData, IGroupDataMap } from "./types"; import { HassEntity, HomeAssistant } from "./ha-types"; -import { log } from "./utils"; +import { log, safeGetConfigObject } from "./utils"; import { ActionFactory } from "./action"; import { getBatteryCollections } from "./grouping"; @@ -207,7 +207,7 @@ export class BatteryProvider { entity, ActionFactory.getAction({ card: this.cardNode, - config: entity.tap_action || this.config.tap_action || null, + config: safeGetConfigObject(entity.tap_action || this.config.tap_action || null, "action"), entity: entity }) ); diff --git a/src/utils.ts b/src/utils.ts index bf863dae..af024ead 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import { HomeAssistant } from "./ha-types"; console.info( - "%c BATTERY-STATE-CARD %c 1.5.0", + "%c BATTERY-STATE-CARD %c 1.5.1", "color: white; background: forestgreen; font-weight: 700;", "color: forestgreen; background: white; font-weight: 700;", ); @@ -85,6 +85,26 @@ export const safeGetArray = (val: T | T[] | undefined): T[] => { return val ? [val] : []; }; +/** + * Converts string to object with given property or returns the object if it is not a string + * @param value Value from the config + * @param propertyName Property name of the expected config object to which value will be assigned + */ +export const safeGetConfigObject = (value: string | T, propertyName: string): T => { + + switch (typeof value) { + case "string": + const result = {}; + result[propertyName] = value; + return result; + case "object": + // make a copy as the original one is immutable + return { ...value }; + } + + return value; +} + /** * Converts given date to localized string representation of relative time * @param hass Home Assistant instance