Skip to content

Commit

Permalink
feat(actions)!: Support for the new action (assist) and all the futur…
Browse files Browse the repository at this point in the history
…e ones

This also fixes the latest translations missing and starts the work to remove custom-cards-helper from the dependencies.

BREAKING CHANGE: Requires HA 2023.4 minimum. Support for the new action format (`target` is also be supported), `service_data` should be renamed to `data` (but it still works with the old format)

Fix #711, #685
  • Loading branch information
RomRider committed Jul 23, 2023
1 parent 4351895 commit d9c17a4
Show file tree
Hide file tree
Showing 24 changed files with 4,974 additions and 2,064 deletions.
20 changes: 19 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,17 @@ views:
type: entity-button
entity: switch.skylight
name: Default HASS
show_name: false
show_state: true
- type: custom:card-modder
styles:
card:
- border-radius: 10px
card:
type: 'custom:button-card'
entity: switch.skylight
show_state: true
show_name: false
- type: custom:card-modder
styles:
card:
Expand Down Expand Up @@ -984,6 +988,7 @@ views:
name: more-info
tap_action:
action: more-info
entity: light.test_light
- type: 'custom:button-card'
entity: switch.skylight
name: call-service
Expand All @@ -992,6 +997,19 @@ views:
service: switch.toggle
service_data:
entity_id: switch.skylight
- type: 'custom:button-card'
entity: switch.skylight
name: call-service
tap_action:
action: call-service
service: timer.start
target:
entity_id: timer.laundry
- type: 'custom:button-card'
entity: switch.skylight
name: assist
tap_action:
action: assist
- type: 'custom:button-card'
entity: switch.skylight
name: none
Expand Down Expand Up @@ -1634,7 +1652,7 @@ views:
cards:
- type: horizontal-stack
cards:
- type: entity-button
- type: button-card
entity: switch.skylight
name: Default
- type: 'custom:button-card'
Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,18 @@ Lovelace Button card for your entities.

All the fields support templates, see [templates](#javascript-templates).

| Name | Type | Default | Supported options | Description |
| ----------------- | ------ | -------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `action` | string | `toggle` | `more-info`, `toggle`, `call-service`, `none`, `navigate`, `url` | Action to perform |
| `entity` | string | none | Any entity id | **Only valid for `action: more-info`** to override the entity on which you want to call `more-info` |
| `navigation_path` | string | none | Eg: `/lovelace/0/` | Path to navigate to (e.g. `/lovelace/0/`) when action defined as navigate |
| `url_path` | string | none | Eg: `https://www.google.fr` | URL to open on click when action is `url`. The URL will open in a new tab |
| `service` | string | none | Any service | Service to call (e.g. `media_player.media_play_pause`) when `action` defined as `call-service` |
| `service_data` | object | none | Any service data | Service data to include (e.g. `entity_id: media_player.bedroom`) when `action` defined as `call-service`. If your `service_data` requires an `entity_id`, you can use the keywork `entity`, this will actually call the service on the entity defined in the main configuration of this card. Useful for [configuration templates](#configuration-templates) |
| `haptic` | string | none | `success`, `warning`, `failure`, `light`, `medium`, `heavy`, `selection` | Haptic feedback for the [Beta IOS App](http://home-assistant.io/ios/beta) |
| `repeat` | number | none | eg: `500` | For a hold_action, you can optionally configure the action to repeat while the button is being held down (for example, to repeatedly increase the volume of a media player). Define the number of milliseconds between repeat actions here. |
| `confirmation` | object | none | See [confirmation](#confirmation) | Display a confirmation popup, overrides the default `confirmation` object |
| Name | Type | Default | Supported options | Description |
| ------------------------ | ------ | -------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `action` | string | `toggle` | `more-info`, `toggle`, `call-service`, `none`, `navigate`, `url`, `assist` | Action to perform |
| `entity` | string | none | Any entity id | **Only valid for `action: more-info`** to override the entity on which you want to call `more-info` |
| `target` | object | none | | Only works with `call-service`. Follows the [home-assistant syntax](https://www.home-assistant.io/docs/scripts/service-calls/#targeting-areas-and-devices) |
| `navigation_path` | string | none | Eg: `/lovelace/0/` | Path to navigate to (e.g. `/lovelace/0/`) when action defined as navigate |
| `url_path` | string | none | Eg: `https://www.google.fr` | URL to open on click when action is `url`. The URL will open in a new tab |
| `service` | string | none | Any service | Service to call (e.g. `media_player.media_play_pause`) when `action` defined as `call-service` |
| `data` or `service_data` | object | none | Any service data | Service data to include (e.g. `entity_id: media_player.bedroom`) when `action` defined as `call-service`. If your `data` requires an `entity_id`, you can use the keywork `entity`, this will actually call the service on the entity defined in the main configuration of this card. Useful for [configuration templates](#configuration-templates) |
| `haptic` | string | none | `success`, `warning`, `failure`, `light`, `medium`, `heavy`, `selection` | Haptic feedback for the [Beta IOS App](http://home-assistant.io/ios/beta) |
| `repeat` | number | none | eg: `500` | For a hold_action, you can optionally configure the action to repeat while the button is being held down (for example, to repeatedly increase the volume of a media player). Define the number of milliseconds between repeat actions here. |
| `confirmation` | object | none | See [confirmation](#confirmation) | Display a confirmation popup, overrides the default `confirmation` object |

### Confirmation

Expand Down Expand Up @@ -1031,7 +1032,7 @@ Horizontal stack with :
tap_action:
action: call-service
service: media_player.volume_up
service_data:
data:
entity_id: media_player.living_room_speaker
- type: 'custom:button-card'
color_type: card
Expand All @@ -1040,7 +1041,7 @@ Horizontal stack with :
tap_action:
action: call-service
service: media_player.volume_down
service_data:
data:
entity_id: media_player.living_room_speaker
- type: 'custom:button-card'
color_type: blank-card
Expand Down Expand Up @@ -1115,7 +1116,7 @@ If you don't specify any operator, `==` will be used to match the current state
tap_action:
action: call-service
service: input_select.select_next
service_data:
data:
entity_id: input_select.cube_mode
show_state: true
state:
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@
"dependencies": {
"@ctrl/tinycolor": "^3.1.6",
"@material/mwc-ripple": "^0.19.1",
"custom-card-helpers": "^1.7.0",
"custom-card-helpers": "^1.9.0",
"fast-copy": "^2.1.0",
"home-assistant-js-websocket": "^5.7.0",
"lit-element": "^2.4.0",
"lit-html": "^1.3.0"
"home-assistant-js-websocket": "^8.2.0",
"lit": "^2.7.6",
"lit-element": "^3.3.2",
"lit-html": "^2.7.5",
"memoize-one": "^6.0.0"
}
}
20 changes: 15 additions & 5 deletions src/action-handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { directive, PropertyPart } from 'lit-html';
import { PropertyPart, noChange } from 'lit-html';
// import '@material/mwc-ripple';
// tslint:disable-next-line
import { Ripple } from '@material/mwc-ripple';
import { myFireEvent } from './my-fire-event';
import { deepEqual } from './deep-equal';
import { AttributePart, Directive, DirectiveParameters, directive } from 'lit-html/directive';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;

interface ActionHandler extends HTMLElement {
Expand Down Expand Up @@ -254,14 +257,21 @@ const getActionHandler = (): ActionHandler => {
return actionhandler as ActionHandler;
};

export const actionHandlerBind = (element: ActionHandlerElement, options: ActionHandlerOptions): void => {
export const actionHandlerBind = (element: ActionHandlerElement, options?: ActionHandlerOptions) => {
const actionhandler: ActionHandler = getActionHandler();
if (!actionhandler) {
return;
}
actionhandler.bind(element, options);
};
export const actionHandler = directive(
class extends Directive {
update(part: AttributePart, [options]: DirectiveParameters<this>) {
actionHandlerBind(part.element as ActionHandlerElement, options);
return noChange;
}

export const actionHandler = directive((options: ActionHandlerOptions = {}) => (part: PropertyPart): void => {
actionHandlerBind(part.committer.element as ActionHandlerElement, options);
});
// eslint-disable-next-line @typescript-eslint/no-empty-function
render(_options?: ActionHandlerOptions) {}
},
);
Loading

0 comments on commit d9c17a4

Please sign in to comment.