Skip to content

Commit

Permalink
Allow simple string as an action #147
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Oct 24, 2020
1 parent 78052c9 commit 1136ed6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/battery-provider.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -207,7 +207,7 @@ export class BatteryProvider {
entity,
ActionFactory.getAction({
card: this.cardNode,
config: entity.tap_action || this.config.tap_action || <any>null,
config: safeGetConfigObject(entity.tap_action || this.config.tap_action || <any>null, "action"),
entity: entity
})
);
Expand Down
22 changes: 21 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;",
);
Expand Down Expand Up @@ -85,6 +85,26 @@ export const safeGetArray = <T>(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 = <T>(value: string | T, propertyName: string): T => {

switch (typeof value) {
case "string":
const result = <any>{};
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
Expand Down

0 comments on commit 1136ed6

Please sign in to comment.