Skip to content

Commit

Permalink
Added support for stack, grid and glance cards (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulonutor authored Jun 20, 2023
1 parent d467706 commit fb39c53
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
4 changes: 4 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% if installed %}

### Features
{% if version_installed.replace("v", "").replace(".","") | int < 10724 %}
- Added `support for stack, grid and glance cards`
{% endif %}

{% if version_installed.replace("v", "").replace(".","") | int < 10723 %}
- Fixed `actions no longer working`
- Removed `entity alongside more-info tap_actions`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "room-card",
"version": "1.07.23",
"version": "1.07.24",
"description": "Show entities in Home Assistant's Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion room-card.js

Large diffs are not rendered by default.

Binary file modified room-card.js.gz
Binary file not shown.
11 changes: 6 additions & 5 deletions src/types/room-card-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface EntityStyles {
template?: string;
}

export interface RoomCardConfig extends LovelaceCardConfig {
export interface RoomCardConfig extends LovelaceCardConfig {
info_entities?: RoomCardEntity[];
entities?: RoomCardEntity[];
entity?: string;
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface RoomCardIconTemplate {
styles?: string;
}

export interface HideIfConfig {
export interface HideIfConfig {
conditions?: EntityCondition[];
}

Expand All @@ -98,7 +98,7 @@ export interface FormattingOptions {
currency?: string;
}

export interface RoomCardTemplateContainer {
export interface RoomCardTemplateContainer {
name: string;
template: RoomCardTemplateDefinition;
}
Expand All @@ -110,18 +110,19 @@ export interface RoomCardTemplateDefinition {

export interface RoomCardLovelaceCardConfig extends LovelaceCardConfig {
hide_if?: HideIfConfig;
cards?: RoomCardLovelaceCardConfig[];
entities?: (string | { entity: string })[];
}

export interface RoomCardAttributeTemplate {
template: string;
}


export interface ActionHandler extends HTMLElement {
holdTime: number;
bind(element: Element, options: ActionHandlerOptions): void;
}

export interface ActionHandlerElement extends HTMLElement {
actionHandler?: boolean;
}
}
13 changes: 10 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export const getValue = (entity: RoomCardEntity) => {
return entity.attribute ? entity.stateObj.attributes[entity.attribute] : entity.stateObj.state;
}

export const getEntityIds = (config: RoomCardConfig) : string[] =>
export const getEntityIds = (config: RoomCardConfig): string[] =>
[config.entity]
.concat(config.entities?.map((entity) => getEntity(entity)))
.concat(config.info_entities?.map((entity) => getEntity(entity)))
.concat(config.rows?.flatMap(row => row.entities).map((entity) => getEntity(entity)))
.concat(config.cards?.map((card) => getEntity(card.entity)))
.concat(config.rows?.flatMap((row) => row.entities).map((entity) => getEntity(entity)))
.concat(config.cards?.flatMap((card) => getCardEntities(card)))
.concat(getConditionEntitiesFromConfig(config))
.filter((entity) => entity);

Expand Down Expand Up @@ -54,6 +54,13 @@ export const getConditionEntitiesFromConfig = (config: RoomCardConfig) : string[
return conditionWithEntities.filter(condition => condition.entity).map(condition => condition.entity);
}

export const getCardEntities = (card: RoomCardLovelaceCardConfig) : string[] => {
return [getEntity(card.entity)]
.concat(card.cards?.flatMap((card) => getCardEntities(card)))
.concat(card.entities?.flatMap((entity) => getEntity(entity as RoomCardEntity)))
.filter((entity) => entity);
}

export const hasConfigOrEntitiesChanged = (node: RoomCardConfig, changedProps: PropertyValues) => {
if (changedProps.has('config')) {
return true;
Expand Down
44 changes: 44 additions & 0 deletions tests/util/getEntityIds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,50 @@ describe('Testing util file function getEntityIds', () => {
'sensor.entity', 'sensor.entity2'
]);
}),
test('Passing stackable cards should return row entities', () => {
const config: RoomCardConfig = {
info_entities: undefined,
entities: undefined,
entity: undefined,
cards: [
{
type: 'grid',
cards: [
{
type: 'custom:room-card',
entity: 'sensor.entity1'
},
{
type: 'grid',
cards: [
{
type: 'custom:room-card',
entity: 'sensor.entity2'
}
]
}
]
},
{
type: 'glance',
entities: [
'sensor.entity3',
{
entity: 'sensor.entity4'
}
]
}
],
entityIds: [],
type: ''
};
expect(getEntityIds(config)).toMatchObject([
'sensor.entity1',
'sensor.entity2',
'sensor.entity3',
'sensor.entity4'
]);
}),
test('Passing entities with icon conditions should return entities and icon condition entities', () => {
const config: RoomCardConfig = {
info_entities: [{
Expand Down

0 comments on commit fb39c53

Please sign in to comment.