Skip to content

Commit 79958bb

Browse files
authored
Update custom card feature example to use isSupported (#3102)
1 parent b9c618b commit 79958bb

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

docs/frontend/custom-ui/custom-card-feature.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
css,
1818
} from "https://unpkg.com/lit-element@2.0.1/lit-element.js?module";
1919

20-
const supportsButtonPressCardFeature = (stateObj) => {
20+
const supportsButtonPressCardFeature = (hass, context) => {
21+
const stateObj = context.entity_id
22+
? hass.states[context.entity_id]
23+
: undefined;
24+
if (!stateObj) return false;
2125
const domain = stateObj.entity_id.split(".")[0];
2226
return domain === "button";
2327
};
@@ -27,7 +31,7 @@ class ButtonPressCardFeature extends LitElement {
2731
return {
2832
hass: undefined,
2933
config: undefined,
30-
stateObj: undefined,
34+
context: undefined,
3135
};
3236
}
3337

@@ -45,19 +49,26 @@ class ButtonPressCardFeature extends LitElement {
4549
this.config = config;
4650
}
4751

52+
get _stateObj() {
53+
if (!this.hass || !this.context?.entity_id) return undefined;
54+
return this.hass.states[this.context.entity_id];
55+
}
56+
4857
_press(ev) {
4958
ev.stopPropagation();
59+
const stateObj = this._stateObj;
60+
if (!stateObj) return;
5061
this.hass.callService("button", "press", {
51-
entity_id: this.stateObj.entity_id,
62+
entity_id: stateObj.entity_id,
5263
});
5364
}
5465

5566
render() {
5667
if (
5768
!this.config ||
5869
!this.hass ||
59-
!this.stateObj ||
60-
!supportsButtonPressCardFeature(this.stateObj)
70+
!this.context ||
71+
!supportsButtonPressCardFeature(this.hass, this.context)
6172
) {
6273
return null;
6374
}
@@ -97,11 +108,20 @@ window.customCardFeatures = window.customCardFeatures || [];
97108
window.customCardFeatures.push({
98109
type: "button-press-card-feature",
99110
name: "Button press",
100-
supported: supportsButtonPressCardFeature, // Optional
111+
isSupported: supportsButtonPressCardFeature, // Optional
101112
configurable: true, // Optional - defaults to false
102113
});
103114
```
104115
116+
## Context
117+
118+
A card feature is rendered inside a card and receives the same context the card is bound to. This is passed both to the element as the `context` property and as the second argument of the `isSupported` function.
119+
120+
The `context` object exposes:
121+
122+
- `entity_id` _(optional)_: the entity id from the parent card.
123+
- `area_id` _(optional)_: the area id from the parent card.
124+
105125
If you want your feature to better integrate with the default design of home assistant, you can use these CSS variables:
106126
107127
- `--feature-height`: Recommended height (42px).
@@ -111,6 +131,6 @@ If you want your feature to better integrate with the default design of home ass
111131
The main difference with custom cards is the graphical configuration option.
112132
To have it displayed in the card editor, you must add an object describing it to the array `window.customCardFeatures`.
113133
114-
Required properties of the object are `type` and `name`. It is recommended to define the `supported` option with a function, so the editor can only propose the feature if it is compatible with the selected entity in the card. Set `configurable` to `true` if your entity has additional configuration (e.g. `label` option in the example above) so the editor.
134+
Required properties of the object are `type` and `name`. It is recommended to define the `isSupported` option with a function `(hass, context) => boolean`, so the editor can only propose the feature if it is compatible with the selected entity in the card. Set `configurable` to `true` if your entity has additional configuration (e.g. `label` option in the example above).
115135
116136
Also, the static functions `getConfigElement` and `getStubConfig` work the same as with normal custom cards.

0 commit comments

Comments
 (0)