Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions src/dialogs/more-info/controls/more-info-group.js

This file was deleted.

111 changes: 111 additions & 0 deletions src/dialogs/more-info/controls/more-info-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { HassEntity } from "home-assistant-js-websocket";
import {
LitElement,
property,
CSSResult,
css,
internalProperty,
PropertyValues,
} from "lit-element";
import { html, TemplateResult } from "lit-html";
import { dynamicElement } from "../../../common/dom/dynamic-element-directive";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
import "../../../state-summary/state-card-content";
import { GroupEntity, HomeAssistant } from "../../../types";
import {
importMoreInfoControl,
domainMoreInfoType,
} from "../state_more_info_control";

class MoreInfoGroup extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property() public stateObj?: GroupEntity;

@internalProperty() private _groupDomainStateObj?: HassEntity;

@internalProperty() private _moreInfoType?: string;

protected updated(changedProperties: PropertyValues) {
if (
!this.hass ||
!this.stateObj ||
(!changedProperties.has("hass") && !changedProperties.has("stateObj"))
) {
return;
}

const states = this.stateObj.attributes.entity_id
.map((entity_id) => this.hass.states[entity_id])
.filter((state) => state);

if (!states.length) {
this._groupDomainStateObj = undefined;
this._moreInfoType = undefined;
return;
}

const baseStateObj = states.find((s) => s.state === "on") || states[0];
const groupDomain = computeStateDomain(baseStateObj);

// Groups need to be filtered out or we'll show content of
// first child above the children of the current group
if (
groupDomain !== "group" &&
states.every((state) => groupDomain === computeStateDomain(state))
) {
this._groupDomainStateObj = {
...baseStateObj,
entity_id: this.stateObj.entity_id,
attributes: { ...baseStateObj.attributes },
};
const type = domainMoreInfoType(groupDomain);
importMoreInfoControl(type);
this._moreInfoType = type === "hidden" ? undefined : `more-info-${type}`;
} else {
this._groupDomainStateObj = undefined;
this._moreInfoType = undefined;
}
}

protected render(): TemplateResult {
if (!this.hass || !this.stateObj) {
return html``;
}
return html`${this._moreInfoType
? dynamicElement(this._moreInfoType, {
hass: this.hass,
stateObj: this._groupDomainStateObj,
})
: ""}
${this.stateObj.attributes.entity_id.map((entity_id) => {
const state = this.hass!.states[entity_id];
if (!state) {
return "";
}
return html`
<state-card-content
.stateObj=${state}
.hass=${this.hass}
></state-card-content>
`;
})}`;
}

static get styles(): CSSResult {
return css`
state-card-content {
display: block;
margin-top: 8px;
}
`;
}
}

customElements.define("more-info-group", MoreInfoGroup);

declare global {
interface HTMLElementTagNameMap {
"more-info-group": MoreInfoGroup;
}
}
4 changes: 4 additions & 0 deletions src/dialogs/more-info/state_more_info_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const LAZY_LOADED_MORE_INFO_CONTROL = {
export const stateMoreInfoType = (stateObj: HassEntity): string => {
const domain = computeStateDomain(stateObj);

return domainMoreInfoType(domain);
};

export const domainMoreInfoType = (domain: string): string => {
if (DOMAINS_WITH_MORE_INFO.includes(domain)) {
return domain;
}
Expand Down