Use a fresh hass object for templating

This commit is contained in:
Thomas Lovén 2018-12-13 00:00:01 +01:00
parent 3cd04c3dbc
commit e96d8c06f6
2 changed files with 16 additions and 7 deletions

View File

@ -40,7 +40,7 @@ The following functions and variables are defined:
| --- | --- | --- |
| `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.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.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) |
@ -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.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.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.
@ -74,8 +74,8 @@ This is provided for plugins that *aren't* cards, elements or entity rows. For t
```js
...
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');
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');
```
### fireEvent
@ -200,3 +200,5 @@ Next is one of:
- `last_updated`
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.

View File

@ -22,7 +22,9 @@ if (!window.cardTools){
cardTools.LitElement.prototype.html;
cardTools.hass =
document.querySelector('home-assistant').hass;
() => {
return document.querySelector('home-assistant').hass;
};
cardTools.fireEvent =
(ev, detail) => {
@ -177,13 +179,18 @@ if (!window.cardTools){
return element;
};
cardTools.hasTemplate =
(text) => {
return /\[\[\s+.*\s+\]\]/.test(text);
};
cardTools.parseTemplate =
(text, error) => {
const _parse = (str) => {
try {
str = str.replace(/^\[\[\s+|\s+\]\]$/g, '')
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.forEach(item => v = v[item]);
@ -194,7 +201,7 @@ if (!window.cardTools){
}
text = text.replace(/(\[\[\s.*?\s\]\])/g, (str, p1, offset, s) => _parse(str));
return text;
}
};
window.cardTools = cardTools;
cardTools.fireEvent("rebuild-view");