Skip to content

Commit 4308a4e

Browse files
committed
feat(#228): add ignore_missing_entities option
1 parent 10d85b0 commit 4308a4e

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Install through [HACS](https://hacs.xyz/)
5252
| sort_dir | string | desc | Sorting direction. Valid options are: 'asc' for smallest first & 'desc' for biggest first
5353
| time_period_from | string | | Start of custom time period (e.g., "now-1d", "now/d"). Not compatible with `energy_date_selection`. See [Time period](#time-period)
5454
| time_period_to | string | now | End of custom time period. Not compatible with `energy_date_selection`. See [Time period](#time-period)
55+
| ignore_missing_entities | boolean | false | If true, missing entities will be treated as having a state of 0 instead of throwing an error |
5556

5657
### Sections object
5758

__tests__/basic.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const hass = mockHass({
3131
},
3232
});
3333

34+
const ROOT_TAG = 'sankey-chart';
35+
3436
describe('SankeyChart', () => {
35-
const ROOT_TAG = 'sankey-chart';
3637
let sankeyChart: SankeyChart;
3738

3839
beforeEach(() => {
@@ -72,3 +73,37 @@ describe('SankeyChart', () => {
7273
expect(sankeyChartBase.shadowRoot?.innerHTML.replace(/<!--.*-->/g, '')).toMatchSnapshot();
7374
});
7475
});
76+
77+
describe('Missing entities', () => {
78+
let element: SankeyChart;
79+
80+
beforeEach(() => {
81+
element = document.createElement(ROOT_TAG) as SankeyChart;
82+
// @ts-ignore
83+
element.hass = hass as HomeAssistant;
84+
});
85+
86+
test('treats missing entity as 0 when ignore_missing_entities is true', () => {
87+
const config = {
88+
type: 'custom:sankey-chart',
89+
ignore_missing_entities: true,
90+
sections: [
91+
{
92+
entities: [
93+
{
94+
entity_id: 'sensor.missing',
95+
children: ['sensor.ent2'],
96+
},
97+
],
98+
},
99+
{
100+
entities: ['sensor.ent2'],
101+
},
102+
],
103+
};
104+
105+
element.setConfig(config, true);
106+
// Should not throw
107+
expect(() => element.requestUpdate()).not.toThrow();
108+
});
109+
});

src/chart.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,16 @@ export class Chart extends LitElement {
568568

569569
let entity = this.states[getEntityId(entityConf)];
570570
if (!entity) {
571+
if (this.config.ignore_missing_entities) {
572+
// Return a fake entity with state 0 if ignoring missing entities
573+
return {
574+
state: 0,
575+
attributes: {
576+
unit_of_measurement: entityConf.unit_of_measurement || '',
577+
friendly_name: entityConf.name || getEntityId(entityConf),
578+
},
579+
};
580+
}
571581
throw new Error('Entity not found "' + getEntityId(entityConf) + '"');
572582
}
573583

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface SankeyChartConfig extends LovelaceCardConfig {
4141
sort_dir?: 'asc' | 'desc';
4242
time_period_from?: string;
4343
time_period_to?: string;
44+
ignore_missing_entities?: boolean;
4445
}
4546

4647
declare global {

0 commit comments

Comments
 (0)