Skip to content

Commit

Permalink
Refactoring infocard renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Barma-lej committed Oct 28, 2024
1 parent eaf70a4 commit 464552d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 67 deletions.
74 changes: 29 additions & 45 deletions src/landroid-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,25 +566,43 @@ class LandroidCard extends LitElement {
}

/**
* Renders the Entities Card for a given card type.
* Renders the Info Card for a given card type.
*
* @param {string} card - The type of card to render.
* @return {TemplateResult|nothing} The rendered Entities Card or nothing if the card is not visible.
* @return {TemplateResult|nothing} The rendered Info Card or nothing if the card is not visible.
*/
renderInfoCard(card) {
if (!consts.CARD_MAP[card].visibility) return nothing;

try {
const entities = this.findEntitiesBySuffixes(
consts.CARD_MAP[card].entities,
);

const entities = this.findEntitiesBySuffixes(consts.CARD_MAP[card].entities);

return html`
<div class="entitiescard">
<div class="info-card">
<div id="states" class="card-content">
${Object.values(entities).map((entity) =>
this.renderEntityRow(entity),
)}
${Object.values(entities).map((stateObj) => {
if (!stateObj || stateObj.state === consts.UNAVAILABLE) return nothing;
const entity_id = stateObj.entity_id;
const title = this.getEntityName(stateObj);
const config = { entity: entity_id, name: title };
return html`
<hui-generic-entity-row .hass=${this.hass} .config=${config}>
<div class="text-content value" @action=${() => this.handleMore(entity_id)}>
${stateObj.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP && !stateObj.state.includes('unknown')
? html`
<hui-timestamp-display
.hass=${this.hass}
.ts=${new Date(stateObj.state)}
capitalize
></hui-timestamp-display>
`
: this.hass.formatEntityState(stateObj)}
</div>
</hui-generic-entity-row>
`;
})}
</div>
</div>
`;
Expand All @@ -594,40 +612,6 @@ class LandroidCard extends LitElement {
}
}

/**
* Renders a row for a given entity in the UI.
*
* @param {Object} stateObj - The entity object to render.
* @return {TemplateResult} The rendered row as a TemplateResult.
*/
renderEntityRow(stateObj) {
if (!stateObj || stateObj.state === consts.UNAVAILABLE) return nothing;

const entity_id = stateObj.entity_id;
const title = this.getEntityName(stateObj);
const config = { entity: entity_id, name: title };

return html`
<hui-generic-entity-row .hass=${this.hass} .config=${config}>
<div
class="text-content value"
@action=${() => this.handleMore(entity_id)}
>
${stateObj.attributes.device_class ===
SENSOR_DEVICE_CLASS_TIMESTAMP && !stateObj.state.includes('unknown')
? html`
<hui-timestamp-display
.hass=${this.hass}
.ts=${new Date(stateObj.state)}
capitalize
></hui-timestamp-display>
`
: this.hass.formatEntityState(stateObj)}
</div>
</hui-generic-entity-row>
`;
}

/**
* Renders the camera or image based on the provided state.
*
Expand Down
24 changes: 2 additions & 22 deletions src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ const styles = css`
display: flex;
}
/* Input number row */
.entitiescard {
/* Info Card */
.info-card {
height: 100%;
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -350,26 +350,6 @@ const styles = css`
text-align: end;
}
.slider {
flex-grow: 2;
width: 100px;
max-width: 200px;
}
ha-textfield {
text-align: end;
}
ha-slider {
width: 100%;
max-width: 200px;
}
/* hui-input-select-entity-row */
ha-select {
width: 100%;
--ha-select-min-width: 0;
}
`;

export default styles;

0 comments on commit 464552d

Please sign in to comment.