Skip to content

Commit

Permalink
feat(#228): add ignore_missing_entities option
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed Dec 10, 2024
1 parent 10d85b0 commit 4308a4e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Install through [HACS](https://hacs.xyz/)
| sort_dir | string | desc | Sorting direction. Valid options are: 'asc' for smallest first & 'desc' for biggest first
| 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)
| time_period_to | string | now | End of custom time period. Not compatible with `energy_date_selection`. See [Time period](#time-period)
| ignore_missing_entities | boolean | false | If true, missing entities will be treated as having a state of 0 instead of throwing an error |

### Sections object

Expand Down
37 changes: 36 additions & 1 deletion __tests__/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ const hass = mockHass({
},
});

const ROOT_TAG = 'sankey-chart';

describe('SankeyChart', () => {
const ROOT_TAG = 'sankey-chart';
let sankeyChart: SankeyChart;

beforeEach(() => {
Expand Down Expand Up @@ -72,3 +73,37 @@ describe('SankeyChart', () => {
expect(sankeyChartBase.shadowRoot?.innerHTML.replace(/<!--.*-->/g, '')).toMatchSnapshot();
});
});

describe('Missing entities', () => {
let element: SankeyChart;

beforeEach(() => {
element = document.createElement(ROOT_TAG) as SankeyChart;
// @ts-ignore
element.hass = hass as HomeAssistant;
});

test('treats missing entity as 0 when ignore_missing_entities is true', () => {
const config = {
type: 'custom:sankey-chart',
ignore_missing_entities: true,
sections: [
{
entities: [
{
entity_id: 'sensor.missing',
children: ['sensor.ent2'],
},
],
},
{
entities: ['sensor.ent2'],
},
],
};

element.setConfig(config, true);
// Should not throw
expect(() => element.requestUpdate()).not.toThrow();
});
});
10 changes: 10 additions & 0 deletions src/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,16 @@ export class Chart extends LitElement {

let entity = this.states[getEntityId(entityConf)];
if (!entity) {
if (this.config.ignore_missing_entities) {
// Return a fake entity with state 0 if ignoring missing entities
return {
state: 0,
attributes: {
unit_of_measurement: entityConf.unit_of_measurement || '',
friendly_name: entityConf.name || getEntityId(entityConf),
},
};
}
throw new Error('Entity not found "' + getEntityId(entityConf) + '"');
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface SankeyChartConfig extends LovelaceCardConfig {
sort_dir?: 'asc' | 'desc';
time_period_from?: string;
time_period_to?: string;
ignore_missing_entities?: boolean;
}

declare global {
Expand Down

0 comments on commit 4308a4e

Please sign in to comment.