Skip to content

Commit

Permalink
feat(transform): Provide the full state as entity to the function (#77
Browse files Browse the repository at this point in the history
)

Fixes #71
  • Loading branch information
RomRider authored Feb 10, 2021
1 parent 99735b6 commit 9919c10
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ views:
floating: true

- type: custom:apexcharts-card
update_interval: 2s
stacked: true
experimental:
color_threshold: true
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
1 change: 0 additions & 1 deletion src/apex-layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
26 changes: 19 additions & 7 deletions src/graphEntry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9919c10

Please sign in to comment.