Remove default

This commit is contained in:
Paul Bottein 2024-12-11 08:57:56 +01:00
parent 82a7d8ea06
commit 41d3c1efc0
No known key found for this signature in database
5 changed files with 91 additions and 34 deletions

View File

@ -13,7 +13,6 @@ import type { RegistryEntry } from "./registry";
type EntityCategory = "config" | "diagnostic"; type EntityCategory = "config" | "diagnostic";
export type EntityRegistryIcon = { export type EntityRegistryIcon = {
default: string;
state?: Record<string, string>; state?: Record<string, string>;
}; };

View File

@ -189,7 +189,9 @@ export const entityIcon = async (
return entry.icon; return entry.icon;
} }
const state = stateValue ?? stateObj.state; const state = stateValue ?? stateObj.state;
return entry.icon.state?.[state] || entry.icon.default; if (entry.icon.state?.[state]) {
return entry.icon.state?.[state];
}
} }
if (stateObj?.attributes.icon) { if (stateObj?.attributes.icon) {
@ -205,15 +207,15 @@ export const entryIcon = async (
hass: HomeAssistant, hass: HomeAssistant,
entry: EntityRegistryEntry | EntityRegistryDisplayEntry entry: EntityRegistryEntry | EntityRegistryDisplayEntry
) => { ) => {
if (entry.icon) { if (entry.icon && typeof entry.icon === "string") {
return typeof entry.icon === "string" ? entry.icon : entry.icon.default; return entry.icon;
} }
const stateObj = hass.states[entry.entity_id] as HassEntity | undefined; const stateObj = hass.states[entry.entity_id] as HassEntity | undefined;
const domain = computeDomain(entry.entity_id); const domain = computeDomain(entry.entity_id);
return getEntityIcon(hass, domain, stateObj, undefined, entry); return getEntityIcon(hass, domain, stateObj, undefined, entry);
}; };
const getEntityIcon = async ( export const getEntityIcon = async (
hass: HomeAssistant, hass: HomeAssistant,
domain: string, domain: string,
stateObj?: HassEntity, stateObj?: HassEntity,

View File

@ -1,18 +1,22 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { until } from "lit/directives/until";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-button"; import { computeStateDomain } from "../../../../common/entity/compute_state_domain";
import { getStates } from "../../../../common/entity/get_states";
import "../../../../components/ha-alert"; import "../../../../components/ha-alert";
import "../../../../components/ha-area-picker"; import "../../../../components/ha-area-picker";
import "../../../../components/ha-button";
import "../../../../components/ha-dialog"; import "../../../../components/ha-dialog";
import "../../../../components/ha-labels-picker"; import "../../../../components/ha-labels-picker";
import "../../../../components/ha-textfield"; import "../../../../components/ha-textfield";
import "../../../../components/ha-yaml-editor"; import "../../../../components/ha-yaml-editor";
import { EntityRegistryIcon } from "../../../../data/entity_registry"; import type { EntityRegistryIcon } from "../../../../data/entity_registry";
import { entryIcon } from "../../../../data/icons"; import { getEntityIcon } from "../../../../data/icons";
import { haStyle, haStyleDialog } from "../../../../resources/styles"; import { haStyle, haStyleDialog } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
import { EntityStateIconDialogParams } from "./show-dialog-entity-state-icon"; import type { EntityStateIconDialogParams } from "./show-dialog-entity-state-icon";
@customElement("dialog-entity-state-icon") @customElement("dialog-entity-state-icon")
class DialogEntityStateIcon extends LitElement { class DialogEntityStateIcon extends LitElement {
@ -29,13 +33,7 @@ class DialogEntityStateIcon extends LitElement {
const icon = this._params.icon; const icon = this._params.icon;
this._config = this._config = typeof icon === "object" ? icon : {};
typeof icon === "object"
? icon
: {
default:
icon || (await entryIcon(this.hass, this._params.entry)) || "",
};
await this.updateComplete; await this.updateComplete;
} }
@ -46,21 +44,47 @@ class DialogEntityStateIcon extends LitElement {
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
} }
private get _states() {
const entity = this.hass.states[this._params!.entry.entity_id];
const states = getStates(entity);
return states.filter((s) => !["unknown", "unavailable"].includes(s));
}
protected render() { protected render() {
if (!this._params) { if (!this._params || !this._config) {
return nothing; return nothing;
} }
const stateObj = this.hass.states[this._params.entry.entity_id];
const domain = computeStateDomain(stateObj);
return html` return html`
<ha-dialog <ha-dialog
open open
@closed=${this.closeDialog} @closed=${this.closeDialog}
.heading=${"Entity state icon"} .heading=${"Entity state icon"}
> >
<ha-yaml-editor ${this._states.map((s) => {
.defaultValue=${this._config} const value = this._config?.state?.[s];
@value-changed=${this._dataChanged}
></ha-yaml-editor> const placeholder = until(
getEntityIcon(this.hass, domain, stateObj, s, this._params!.entry)
);
return html`
<div class="row">
<p>${this.hass.formatEntityState(stateObj, s)}</p>
<ha-icon-picker
.hass=${this.hass}
.id=${s}
.value=${value}
@value-changed=${this._iconChanged}
.placeholder=${placeholder}
></ha-icon-picker>
</div>
`;
})}
<ha-button <ha-button
slot="secondaryAction" slot="secondaryAction"
@ -80,8 +104,23 @@ class DialogEntityStateIcon extends LitElement {
`; `;
} }
private _dataChanged(ev): void { private _iconChanged(ev): void {
this._config = ev.detail.value; const value = ev.detail.value;
const id = ev.currentTarget.id;
const newConfig = {
...this._config!,
state: {
...this._config!.state,
[id]: value,
},
};
if (!value) {
delete newConfig.state[id];
}
this._config = newConfig;
} }
private async _updateEntry(): Promise<void> { private async _updateEntry(): Promise<void> {
@ -102,7 +141,25 @@ class DialogEntityStateIcon extends LitElement {
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return [haStyle, haStyleDialog, css``]; return [
haStyle,
haStyleDialog,
css`
.row {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
width: 100%;
}
.row p {
width: 100px;
}
.row ha-icon-picker {
flex: 1;
}
`,
];
} }
} }

View File

@ -1,5 +1,5 @@
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { import type {
EntityRegistryEntry, EntityRegistryEntry,
EntityRegistryIcon, EntityRegistryIcon,
} from "../../../../data/entity_registry"; } from "../../../../data/entity_registry";

View File

@ -1,6 +1,6 @@
import "@material/mwc-button/mwc-button"; import "@material/mwc-button/mwc-button";
import "@material/mwc-formfield/mwc-formfield"; import "@material/mwc-formfield/mwc-formfield";
import { mdiCog, mdiContentCopy } from "@mdi/js"; import { mdiBrush, mdiContentCopy } from "@mdi/js";
import type { HassEntity } from "home-assistant-js-websocket"; import type { HassEntity } from "home-assistant-js-websocket";
import type { CSSResultGroup, PropertyValues } from "lit"; import type { CSSResultGroup, PropertyValues } from "lit";
import { css, html, LitElement, nothing } from "lit"; import { css, html, LitElement, nothing } from "lit";
@ -374,12 +374,11 @@ export class EntityRegistrySettingsEditor extends LitElement {
> >
${useStateIcon ${useStateIcon
? html` ? html`
<ha-icon <ha-state-icon
slot="fallback" slot="fallback"
.icon=${(this._icon as EntityRegistryIcon).state?.[ .hass=${this.hass}
stateObj.state .stateObj=${stateObj}
] || (this._icon as EntityRegistryIcon).default} ></ha-state-icon>
></ha-icon>
` `
: !this._icon && !stateObj?.attributes.icon && stateObj : !this._icon && !stateObj?.attributes.icon && stateObj
? html` ? html`
@ -392,7 +391,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
: nothing} : nothing}
</ha-icon-picker> </ha-icon-picker>
<ha-icon-button <ha-icon-button
.path=${mdiCog} .path=${mdiBrush}
@click=${this._openEntityStateIcon} @click=${this._openEntityStateIcon}
></ha-icon-button> ></ha-icon-button>
</div> </div>