From 9919c10543b70220f5c7e6dab972324e807c095c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20W?= Date: Wed, 10 Feb 2021 21:56:50 +0100 Subject: [PATCH] feat(transform): Provide the full state as `entity` to the function (#77) Fixes #71 --- .devcontainer/ui-lovelace.yaml | 3 ++- README.md | 1 + src/apex-layouts.ts | 1 - src/graphEntry.ts | 26 +++++++++++++++++++------- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.devcontainer/ui-lovelace.yaml b/.devcontainer/ui-lovelace.yaml index a47b4cc..abe0992 100644 --- a/.devcontainer/ui-lovelace.yaml +++ b/.devcontainer/ui-lovelace.yaml @@ -83,6 +83,7 @@ views: floating: true - type: custom:apexcharts-card + update_interval: 2s stacked: true experimental: color_threshold: true @@ -97,7 +98,7 @@ views: group_by: duration: 1m func: last - transform: return x/10; + transform: console.log(entity); return x/10; color_threshold: - value: 33 color: '#00ff00' diff --git a/README.md b/README.md index 9a13a60..02b1563 100644 --- a/README.md +++ b/README.md @@ -312,6 +312,7 @@ Some of the things you can do: Your javascript code will receive: * `x`: a state or a value of the attribute if you defined one (it can be a `string`, `null` or a `number` depending on the entity type you've assigned) * `hass`: the full `hass` object (`hass.states['other.entity']` to get the state object of another entity for eg.) +* `entity`: the full state object of the entity from the history entry currently being transformed And should return a `number`, a `float` or `null`. diff --git a/src/apex-layouts.ts b/src/apex-layouts.ts index c604dfe..851df45 100644 --- a/src/apex-layouts.ts +++ b/src/apex-layouts.ts @@ -80,7 +80,6 @@ export function getLayoutConfig(config: ChartCardConfig, hass: HomeAssistant | u ua: ua, 'zh-cn': zh_cn, }; - console.log(hass?.language); const def = { chart: { locales: [(config.locale && locales[config.locale]) || (hass?.language && locales[hass.language]) || en], diff --git a/src/graphEntry.ts b/src/graphEntry.ts index 80b5c30..7a91715 100644 --- a/src/graphEntry.ts +++ b/src/graphEntry.ts @@ -1,5 +1,12 @@ import { HomeAssistant } from 'custom-card-helpers'; -import { ChartCardSeriesConfig, EntityCachePoints, EntityEntryCache, HassHistory, HistoryBuckets } from './types'; +import { + ChartCardSeriesConfig, + EntityCachePoints, + EntityEntryCache, + HassHistory, + HassHistoryEntry, + HistoryBuckets, +} from './types'; import { compress, decompress, log } from './utils'; import localForage from 'localforage'; import { HassEntity } from 'home-assistant-js-websocket'; @@ -171,15 +178,15 @@ export default class GraphEntry { new Date(history.data.slice(-1)[0]![0] + 1) : startHistory, end, - this._config.attribute ? false : skipInitialState, - this._config.attribute ? true : false, + this._config.attribute || this._config.transform ? false : skipInitialState, + this._config.attribute || this._config.transform ? true : false, ); if (newHistory && newHistory[0] && newHistory[0].length > 0) { /* hack because HA doesn't return anything if skipInitialState is false when retrieving for attributes so we retrieve it and we remove it. */ - if (this._config.attribute && skipInitialState) { + if ((this._config.attribute || this._config.transform) && skipInitialState) { newHistory[0].shift(); } let lastNonNull: number | null = null; @@ -198,7 +205,7 @@ export default class GraphEntry { currentState = item.state; } if (this._config.transform) { - currentState = this._applyTransform(currentState); + currentState = this._applyTransform(currentState, item); } let stateParsed: number | null = parseFloat(currentState as string); stateParsed = !Number.isNaN(stateParsed) ? stateParsed : null; @@ -259,8 +266,13 @@ export default class GraphEntry { return true; } - private _applyTransform(value: unknown): number | null { - return new Function('x', 'hass', `'use strict'; ${this._config.transform}`).call(this, value, this._hass); + private _applyTransform(value: unknown, historyItem: HassHistoryEntry): number | null { + return new Function('x', 'hass', 'entity', `'use strict'; ${this._config.transform}`).call( + this, + value, + this._hass, + historyItem, + ); } private async _fetchRecent(