Split unused entities by domain (#2671)

This commit is contained in:
Paulus Schoutsen 2019-02-06 10:57:53 -08:00 committed by GitHub
parent 7a344c865f
commit 5d900f9ced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,11 +12,12 @@ import { createCardElement } from "./common/create-card-element";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { LovelaceCard } from "./types"; import { LovelaceCard } from "./types";
import { LovelaceConfig } from "../../data/lovelace"; import { LovelaceConfig } from "../../data/lovelace";
import computeDomain from "../../common/entity/compute_domain";
export class HuiUnusedEntities extends LitElement { export class HuiUnusedEntities extends LitElement {
private _hass?: HomeAssistant; private _hass?: HomeAssistant;
private _config?: LovelaceConfig; private _config?: LovelaceConfig;
private _element?: LovelaceCard; private _elements?: LovelaceCard[];
static get properties(): PropertyDeclarations { static get properties(): PropertyDeclarations {
return { return {
@ -27,16 +28,18 @@ export class HuiUnusedEntities extends LitElement {
set hass(hass: HomeAssistant) { set hass(hass: HomeAssistant) {
this._hass = hass; this._hass = hass;
if (!this._element) { if (!this._elements) {
this._createElement(); this._createElements();
return; return;
} }
this._element.hass = this._hass; for (const element of this._elements) {
element.hass = this._hass;
}
} }
public setConfig(config: LovelaceConfig): void { public setConfig(config: LovelaceConfig): void {
this._config = config; this._config = config;
this._createElement(); this._createElements();
} }
protected render(): TemplateResult | void { protected render(): TemplateResult | void {
@ -46,7 +49,7 @@ export class HuiUnusedEntities extends LitElement {
return html` return html`
${this.renderStyle()} ${this.renderStyle()}
<div id="root">${this._element}</div> <div id="root">${this._elements}</div>
`; `;
} }
@ -54,30 +57,47 @@ export class HuiUnusedEntities extends LitElement {
return html` return html`
<style> <style>
#root { #root {
max-width: 600px; padding: 4px;
margin: 0 auto; display: flex;
padding: 8px 0; flex-wrap: wrap;
}
hui-entities-card {
max-width: 400px;
padding: 4px;
flex: 1;
} }
</style> </style>
`; `;
} }
private _createElement(): void { private _createElements(): void {
if (this._hass) { if (!this._hass) {
const entities = computeUnusedEntities(this._hass, this._config!).map( return;
(entity) => ({
entity,
secondary_info: "entity-id",
})
);
this._element = createCardElement({
type: "entities",
title: "Unused entities",
entities,
show_header_toggle: false,
});
this._element!.hass = this._hass;
} }
const domains: { [domain: string]: string[] } = {};
computeUnusedEntities(this._hass, this._config!).forEach((entity) => {
const domain = computeDomain(entity);
if (!(domain in domains)) {
domains[domain] = [];
}
domains[domain].push(entity);
});
this._elements = Object.keys(domains)
.sort()
.map((domain) => {
const el = createCardElement({
type: "entities",
title: this._hass!.localize(`domain.${domain}`) || domain,
entities: domains[domain].map((entity) => ({
entity,
secondary_info: "entity-id",
})),
show_header_toggle: false,
});
el.hass = this._hass;
return el;
});
} }
} }