Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Content_alignment for entities row (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcokreeft87 committed Oct 12, 2022
1 parent 3054e76 commit 3a06aa4
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 34 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 < 10630 %}
- Added `content_alignment (left, center or right) for entities row`
{% endif %}

{% if version_installed.replace("v", "").replace(".","") | int < 10621 %}
- Fixed `Cards not updating on every state change`
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.06.21",
"version": "1.06.30",
"description": "Show entities in Home Assistant's Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
16 changes: 12 additions & 4 deletions room-card.js

Large diffs are not rendered by default.

Binary file modified room-card.js.gz
Binary file not shown.
12 changes: 6 additions & 6 deletions src/entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { secondsToDuration } from './lib/seconds_to_duration';
import { formatNumber } from './lib/format_number';
import { computeStateDisplay, computeStateDomain } from './lib/compute_state_display';
import { checkConditionalValue, evalTemplate, getValue, isObject, isUnavailable } from './util';
import { checkConditionalValue, evalTemplate, getValue, isObject, isUnavailable, renderClasses } from './util';
import { ActionConfig, handleClick, HomeAssistant } from 'custom-card-helpers';
import { HomeAssistantEntity, EntityCondition, RoomCardEntity, RoomCardIcon, RoomCardConfig, EntityStyles, RoomCardRow } from './types/room-card-types';
import { html, HTMLTemplateResult, LitElement } from 'lit';
Expand Down Expand Up @@ -123,20 +123,20 @@ export const entityStyles = (styles: EntityStyles) =>
.join('')
: '';

export const renderRows = (rows: RoomCardRow[], hass: HomeAssistant, element: LitElement) : HTMLTemplateResult => {
export const renderRows = (config: RoomCardConfig, rows: RoomCardRow[], hass: HomeAssistant, element: LitElement) : HTMLTemplateResult => {
const filteredRows = rows.filter(row => { return !hideIfRow(row, hass); });

return html`${filteredRows.map((row) => {
return renderEntitiesRow(row.entities, hass, element);
return renderEntitiesRow(config, row.entities, hass, element);
})}`;
}

export const renderEntitiesRow = (entities: RoomCardEntity[], hass: HomeAssistant, element: LitElement, classes?: string) : HTMLTemplateResult => {
export const renderEntitiesRow = (config: RoomCardConfig, entities: RoomCardEntity[], hass: HomeAssistant, element: LitElement, classes?: string) : HTMLTemplateResult => {
if(entities === undefined) {
return null;
}
}

return html`<div class="entities-row${classes !== undefined ? ` ${classes}` : '' }">${entities.map((entity) => renderEntity(entity, hass, element))}</div>`;
return html`<div class="${renderClasses(config, classes)}">${entities.map((entity) => renderEntity(entity, hass, element))}</div>`;
}

export const renderEntity = (entity: RoomCardEntity, hass: HomeAssistant, element: LitElement) : HTMLTemplateResult => {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export default class RoomCard extends LitElement {
</div>
</div>
${this.rows !== undefined && this.rows.length > 0 ?
renderRows(this.rows, this._hass, this) :
renderEntitiesRow(this.entities, this._hass, this)}
renderRows(this.config, this.rows, this._hass, this) :
renderEntitiesRow(this.config, this.entities, this._hass, this)}
${this._refCards}
</ha-card>
`;
Expand Down
10 changes: 9 additions & 1 deletion src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const style = css`
flex-direction: row;
flex-wrap: wrap;
display: inline-flex;
justify-content: left;
align-items: center;
padding: 0 20px 10px 20px;
}
Expand Down Expand Up @@ -79,4 +78,13 @@ export const style = css`
.clickable {
cursor: pointer;
}
.content-left {
justify-content: left;
}
.content-center {
justify-content: center;
}
.content-right {
justify-content: right;
}
`;
9 changes: 8 additions & 1 deletion src/types/room-card-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ export interface RoomCardConfig extends LovelaceCardConfig {
title?: string;
name?: string;
styles?: EntityStyles;
templates?: RoomCardTemplateContainer[]
templates?: RoomCardTemplateContainer[];
content_alignment?: RoomCardAlignment
}

export enum RoomCardAlignment {
Left = 'left',
Center = 'center',
Right = 'right'
}

export interface RoomCardRow {
Expand Down
6 changes: 5 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ export const evalTemplate = (hass: HomeAssistant | undefined, state: HassEntity,
e.name = 'RoomCardJSTemplateError';
throw e;
}
}
}

export const renderClasses = (config: RoomCardConfig, classes?: string): string => {
return `entities-row ${config.content_alignment ? `content-${config.content_alignment}` : 'content-left'}${classes !== undefined ? ` ${classes}` : '' }`;
}
16 changes: 9 additions & 7 deletions tests/entity/renderEntitiesRow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import { HomeAssistant } from 'custom-card-helpers';
import { createMock } from 'ts-auto-mock';
import { LitElement } from 'lit';
import { renderEntitiesRow } from '../../src/entity';
import { HomeAssistantEntity, RoomCardEntity } from '../../src/types/room-card-types';
import { HomeAssistantEntity, RoomCardConfig, RoomCardEntity } from '../../src/types/room-card-types';
import { getRenderString } from '../utils';

describe('Testing entity file function renderEntitiesRow', () => {
const hass = createMock<HomeAssistant>();
const stateObj = createMock<HomeAssistantEntity>();
const config = createMock<RoomCardConfig>();

test('Passing empty entities should return entities-row withouth children', () => {

const entities: RoomCardEntity[] = [];
const element: LitElement = createMock<LitElement>();

const result = renderEntitiesRow(entities, hass, element);
const result = renderEntitiesRow(config, entities, hass, element);
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<div class="entities-row"></div>');
expect(htmlResult).toMatch('<div class="entities-row content-left"></div>');
}),
test('Passing entities should return entities-row with one child element', () => {

Expand All @@ -30,10 +32,10 @@ describe('Testing entity file function renderEntitiesRow', () => {
}];
const element: LitElement = createMock<LitElement>();

const result = renderEntitiesRow(entities, hass, element);
const result = renderEntitiesRow(config, entities, hass, element);
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<div class="entities-row"><div class="entity" style="" @mousedown="start" @mouseup="end" @touchstart="start" @touchend="end" @touchcancel="end"> <span>Test Entity</span> <div><state-badge class="icon-small " .stateObj="" .overrideIcon="mdi:light" .stateColor="" style="" ></state-badge></div> </div></div>');
expect(htmlResult).toMatch('<div class="entities-row content-left"><div class="entity" style="" @mousedown="start" @mouseup="end" @touchstart="start" @touchend="end" @touchcancel="end"> <span>Test Entity</span> <div><state-badge class="icon-small " .stateObj="" .overrideIcon="mdi:light" .stateColor="" style="" ></state-badge></div> </div></div>');
}),
test('Passing entities and classes should return entities-row with one child element', () => {

Expand All @@ -47,9 +49,9 @@ describe('Testing entity file function renderEntitiesRow', () => {
}];
const element: LitElement = createMock<LitElement>();

const result = renderEntitiesRow(entities, hass, element, 'test-class');
const result = renderEntitiesRow(config, entities, hass, element, 'test-class');
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<div class="entities-row test-class"><div class="entity" style="" @mousedown="start" @mouseup="end" @touchstart="start" @touchend="end" @touchcancel="end"> <span>Test Entity</span> <div><state-badge class="icon-small " .stateObj="" .overrideIcon="mdi:light" .stateColor="" style="" ></state-badge></div> </div></div>');
expect(htmlResult).toMatch('<div class="entities-row content-left test-class"><div class="entity" style="" @mousedown="start" @mouseup="end" @touchstart="start" @touchend="end" @touchcancel="end"> <span>Test Entity</span> <div><state-badge class="icon-small " .stateObj="" .overrideIcon="mdi:light" .stateColor="" style="" ></state-badge></div> </div></div>');
})
})
Loading

0 comments on commit 3a06aa4

Please sign in to comment.