Use a fresh hass object for templating
This commit is contained in:
parent
3cd04c3dbc
commit
e96d8c06f6
10
README.md
10
README.md
@ -40,7 +40,7 @@ The following functions and variables are defined:
|
|||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `window.cardTools.v` | 0.1 | Current `card-tools` version |
|
| `window.cardTools.v` | 0.1 | Current `card-tools` version |
|
||||||
| `window.cardTools.checkVersion(v)` | 0.1 | Check that the current `card-tools` version is at least `v` |
|
| `window.cardTools.checkVersion(v)` | 0.1 | Check that the current `card-tools` version is at least `v` |
|
||||||
| `window.cardTools.hass` | 0.1 | A reference to the `hass` object. Useful for plugins that are *not* custom cards. If you need it, you'll know it |
|
| `window.cardTools.hass()` | 0.1 | Returns A `hass` state object. Useful for plugins that are *not* custom cards. If you need it, you'll know it |
|
||||||
| `window.cardTool.fireEvent(event, detail)` | 0.1 | Fire lovelace event `event` with options `detail` |
|
| `window.cardTool.fireEvent(event, detail)` | 0.1 | Fire lovelace event `event` with options `detail` |
|
||||||
| `window.cardTools.LitElement` | 0.1 | A reference to the LitElement base class. |
|
| `window.cardTools.LitElement` | 0.1 | A reference to the LitElement base class. |
|
||||||
| `window.cardTools.litHtml` | 0.1 | A reference to the litHtml template function (requires Home Assistant 0.84 or later) |
|
| `window.cardTools.litHtml` | 0.1 | A reference to the litHtml template function (requires Home Assistant 0.84 or later) |
|
||||||
@ -50,7 +50,7 @@ The following functions and variables are defined:
|
|||||||
| `window.cardTools.deviceID` | 0.1 | Kind of unique and kind of persistent identifier for the browser viewing the page |
|
| `window.cardTools.deviceID` | 0.1 | Kind of unique and kind of persistent identifier for the browser viewing the page |
|
||||||
| `window.cardTools.moreInfo(entity)` | 0.1 | Brings up the `more-info` dialog for the specified `entity` id |
|
| `window.cardTools.moreInfo(entity)` | 0.1 | Brings up the `more-info` dialog for the specified `entity` id |
|
||||||
| `window.cardTools.longPress(element)` | 0.1 | Bind `element` to the long-press handler of lovelace |
|
| `window.cardTools.longPress(element)` | 0.1 | Bind `element` to the long-press handler of lovelace |
|
||||||
| `window.cardTools.parseTemplate(str)` | 0.2 | Parse a simple state template and return results |
|
| `window.cardTools.parseTemplate(text, [error])` | 0.2 | Parse a simple state template and return results |
|
||||||
|
|
||||||
> Another way to use the `card-tools` is to just copy the function you want, and paste it into your card. It requires a bit of more work, but may be more user friendly.
|
> Another way to use the `card-tools` is to just copy the function you want, and paste it into your card. It requires a bit of more work, but may be more user friendly.
|
||||||
|
|
||||||
@ -74,8 +74,8 @@ This is provided for plugins that *aren't* cards, elements or entity rows. For t
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
...
|
...
|
||||||
greeting.innerHTML = `Hi there, ${window.cardTools.hass.user.name}`;
|
greeting.innerHTML = `Hi there, ${window.cardTools.hass().user.name}`;
|
||||||
window.cardTools.hass.connection.subscribeEvents((event) => {console.log("A service was called in Home Assistant")}, 'call-service');
|
window.cardTools.hass().connection.subscribeEvents((event) => {console.log("A service was called in Home Assistant")}, 'call-service');
|
||||||
```
|
```
|
||||||
|
|
||||||
### fireEvent
|
### fireEvent
|
||||||
@ -200,3 +200,5 @@ Next is one of:
|
|||||||
- `last_updated`
|
- `last_updated`
|
||||||
|
|
||||||
The function replaces any template found in the supplied string with the requested value, or an error message on failure.
|
The function replaces any template found in the supplied string with the requested value, or an error message on failure.
|
||||||
|
|
||||||
|
The optional argument `error` is a string that replaces the default error message.
|
||||||
|
@ -22,7 +22,9 @@ if (!window.cardTools){
|
|||||||
cardTools.LitElement.prototype.html;
|
cardTools.LitElement.prototype.html;
|
||||||
|
|
||||||
cardTools.hass =
|
cardTools.hass =
|
||||||
document.querySelector('home-assistant').hass;
|
() => {
|
||||||
|
return document.querySelector('home-assistant').hass;
|
||||||
|
};
|
||||||
|
|
||||||
cardTools.fireEvent =
|
cardTools.fireEvent =
|
||||||
(ev, detail) => {
|
(ev, detail) => {
|
||||||
@ -177,13 +179,18 @@ if (!window.cardTools){
|
|||||||
return element;
|
return element;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
cardTools.hasTemplate =
|
||||||
|
(text) => {
|
||||||
|
return /\[\[\s+.*\s+\]\]/.test(text);
|
||||||
|
};
|
||||||
|
|
||||||
cardTools.parseTemplate =
|
cardTools.parseTemplate =
|
||||||
(text, error) => {
|
(text, error) => {
|
||||||
const _parse = (str) => {
|
const _parse = (str) => {
|
||||||
try {
|
try {
|
||||||
str = str.replace(/^\[\[\s+|\s+\]\]$/g, '')
|
str = str.replace(/^\[\[\s+|\s+\]\]$/g, '')
|
||||||
const parts = str.split(".");
|
const parts = str.split(".");
|
||||||
let v = cardTools.hass.states[`${parts[0]}.${parts[1]}`];
|
let v = cardTools.hass().states[`${parts[0]}.${parts[1]}`];
|
||||||
parts.shift();
|
parts.shift();
|
||||||
parts.shift();
|
parts.shift();
|
||||||
parts.forEach(item => v = v[item]);
|
parts.forEach(item => v = v[item]);
|
||||||
@ -194,7 +201,7 @@ if (!window.cardTools){
|
|||||||
}
|
}
|
||||||
text = text.replace(/(\[\[\s.*?\s\]\])/g, (str, p1, offset, s) => _parse(str));
|
text = text.replace(/(\[\[\s.*?\s\]\])/g, (str, p1, offset, s) => _parse(str));
|
||||||
return text;
|
return text;
|
||||||
}
|
};
|
||||||
|
|
||||||
window.cardTools = cardTools;
|
window.cardTools = cardTools;
|
||||||
cardTools.fireEvent("rebuild-view");
|
cardTools.fireEvent("rebuild-view");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user