Compare commits

...

3 Commits

Author SHA1 Message Date
Paul Bottein
41d3c1efc0 Remove default 2024-12-11 08:57:56 +01:00
Paul Bottein
82a7d8ea06 Improve icon picker 2024-12-10 14:35:19 +01:00
Paul Bottein
bfa21b5380 Allow to change icon based on state 2024-12-10 14:35:18 +01:00
8 changed files with 310 additions and 49 deletions

View File

@@ -67,11 +67,14 @@ const loadCustomIconItems = async (iconsetPrefix: string) => {
} }
}; };
const rowRenderer: ComboBoxLitRenderer<IconItem | RankedIcon> = (item) => const rowRenderer: ComboBoxLitRenderer<IconItem | RankedIcon> = (item) => html`
html`<ha-list-item graphic="avatar"> <ha-list-item graphic="avatar">
<ha-icon .icon=${item.icon} slot="graphic"></ha-icon> <ha-icon .icon=${item.icon} slot="graphic"></ha-icon>
${item.icon} ${item.icon}
</ha-list-item>`; </ha-list-item>
`;
export const CUSTOM_STATE_ICON = "___CUSTOM_STATE_ICON___";
@customElement("ha-icon-picker") @customElement("ha-icon-picker")
export class HaIconPicker extends LitElement { export class HaIconPicker extends LitElement {
@@ -93,6 +96,9 @@ export class HaIconPicker extends LitElement {
@property({ type: Boolean }) public invalid = false; @property({ type: Boolean }) public invalid = false;
@property({ type: Boolean, attribute: "force-fallback-icon" })
public forceFallbackIcon = false;
protected render(): TemplateResult { protected render(): TemplateResult {
return html` return html`
<ha-combo-box <ha-combo-box
@@ -114,7 +120,7 @@ export class HaIconPicker extends LitElement {
@opened-changed=${this._openedChanged} @opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
> >
${this._value || this.placeholder ${(this._value || this.placeholder) && !this.forceFallbackIcon
? html` ? html`
<ha-icon .icon=${this._value || this.placeholder} slot="icon"> <ha-icon .icon=${this._value || this.placeholder} slot="icon">
</ha-icon> </ha-icon>

View File

@@ -35,7 +35,6 @@ export class HaIconSelector extends LitElement {
const placeholder = const placeholder =
this.selector.icon?.placeholder || this.selector.icon?.placeholder ||
stateObj?.attributes.icon ||
(stateObj && until(entityIcon(this.hass, stateObj))); (stateObj && until(entityIcon(this.hass, stateObj)));
return html` return html`

View File

@@ -20,10 +20,8 @@ export class HaStateIcon extends LitElement {
@property() public icon?: string; @property() public icon?: string;
protected render() { protected render() {
const overrideIcon = const overrideIcon = this.icon;
this.icon ||
(this.stateObj && this.hass?.entities[this.stateObj.entity_id]?.icon) ||
this.stateObj?.attributes.icon;
if (overrideIcon) { if (overrideIcon) {
return html`<ha-icon .icon=${overrideIcon}></ha-icon>`; return html`<ha-icon .icon=${overrideIcon}></ha-icon>`;
} }

View File

@@ -12,10 +12,14 @@ import type { RegistryEntry } from "./registry";
type EntityCategory = "config" | "diagnostic"; type EntityCategory = "config" | "diagnostic";
export type EntityRegistryIcon = {
state?: Record<string, string>;
};
export interface EntityRegistryDisplayEntry { export interface EntityRegistryDisplayEntry {
entity_id: string; entity_id: string;
name?: string; name?: string;
icon?: string; icon?: EntityRegistryIcon | string;
device_id?: string; device_id?: string;
area_id?: string; area_id?: string;
labels: string[]; labels: string[];
@@ -47,7 +51,7 @@ export interface EntityRegistryEntry extends RegistryEntry {
id: string; id: string;
entity_id: string; entity_id: string;
name: string | null; name: string | null;
icon: string | null; icon: EntityRegistryIcon | string | null;
platform: string; platform: string;
config_entry_id: string | null; config_entry_id: string | null;
device_id: string | null; device_id: string | null;
@@ -128,7 +132,7 @@ export interface EntityRegistryOptions {
export interface EntityRegistryEntryUpdateParams { export interface EntityRegistryEntryUpdateParams {
name?: string | null; name?: string | null;
icon?: string | null; icon?: string | EntityRegistryIcon | null;
device_class?: string | null; device_class?: string | null;
area_id?: string | null; area_id?: string | null;
disabled_by?: string | null; disabled_by?: string | null;

View File

@@ -178,24 +178,36 @@ export const getServiceIcons = async (
export const entityIcon = async ( export const entityIcon = async (
hass: HomeAssistant, hass: HomeAssistant,
stateObj: HassEntity, stateObj: HassEntity,
state?: string stateValue?: string
) => { ): Promise<string | undefined> => {
const entry = hass.entities?.[stateObj.entity_id] as const entry = hass.entities?.[stateObj.entity_id] as
| EntityRegistryDisplayEntry | EntityRegistryDisplayEntry
| undefined; | undefined;
if (entry?.icon) { if (entry?.icon) {
if (typeof entry.icon === "string") {
return entry.icon; return entry.icon;
} }
const state = stateValue ?? stateObj.state;
if (entry.icon.state?.[state]) {
return entry.icon.state?.[state];
}
}
if (stateObj?.attributes.icon) {
return stateObj.attributes.icon;
}
const domain = computeStateDomain(stateObj); const domain = computeStateDomain(stateObj);
return getEntityIcon(hass, domain, stateObj, state, entry); return getEntityIcon(hass, domain, stateObj, stateValue, entry);
}; };
export const entryIcon = async ( 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 entry.icon; return entry.icon;
} }
const stateObj = hass.states[entry.entity_id] as HassEntity | undefined; const stateObj = hass.states[entry.entity_id] as HassEntity | undefined;
@@ -203,7 +215,7 @@ export const entryIcon = async (
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

@@ -0,0 +1,170 @@
import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { until } from "lit/directives/until";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeStateDomain } from "../../../../common/entity/compute_state_domain";
import { getStates } from "../../../../common/entity/get_states";
import "../../../../components/ha-alert";
import "../../../../components/ha-area-picker";
import "../../../../components/ha-button";
import "../../../../components/ha-dialog";
import "../../../../components/ha-labels-picker";
import "../../../../components/ha-textfield";
import "../../../../components/ha-yaml-editor";
import type { EntityRegistryIcon } from "../../../../data/entity_registry";
import { getEntityIcon } from "../../../../data/icons";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import type { EntityStateIconDialogParams } from "./show-dialog-entity-state-icon";
@customElement("dialog-entity-state-icon")
class DialogEntityStateIcon extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _params?: EntityStateIconDialogParams;
@state() private _submitting = false;
@state() private _config?: EntityRegistryIcon;
public async showDialog(params: EntityStateIconDialogParams): Promise<void> {
this._params = params;
const icon = this._params.icon;
this._config = typeof icon === "object" ? icon : {};
await this.updateComplete;
}
public closeDialog(): void {
this._params = undefined;
this._config = undefined;
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() {
if (!this._params || !this._config) {
return nothing;
}
const stateObj = this.hass.states[this._params.entry.entity_id];
const domain = computeStateDomain(stateObj);
return html`
<ha-dialog
open
@closed=${this.closeDialog}
.heading=${"Entity state icon"}
>
${this._states.map((s) => {
const value = this._config?.state?.[s];
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
slot="secondaryAction"
@click=${this.closeDialog}
.disabled=${this._submitting}
>
${this.hass.localize("ui.common.cancel")}
</ha-button>
<ha-button
slot="primaryAction"
@click=${this._updateEntry}
.disabled=${this._submitting}
>
${this.hass.localize("ui.dialogs.device-registry-detail.update")}
</ha-button>
</ha-dialog>
`;
}
private _iconChanged(ev): void {
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> {
this._submitting = true;
try {
const icon =
!this._config || Object.keys(this._config).length === 0
? null
: this._config;
this._params!.updateIcon(icon);
this.closeDialog();
} catch (err: any) {
// eslint-disable-next-line no-console
console.error(err);
} finally {
this._submitting = false;
}
}
static get styles(): CSSResultGroup {
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;
}
`,
];
}
}
declare global {
interface HTMLElementTagNameMap {
"dialog-entity-state-icon": DialogEntityStateIcon;
}
}

View File

@@ -0,0 +1,25 @@
import { fireEvent } from "../../../../common/dom/fire_event";
import type {
EntityRegistryEntry,
EntityRegistryIcon,
} from "../../../../data/entity_registry";
export interface EntityStateIconDialogParams {
entry: EntityRegistryEntry;
icon: string | EntityRegistryIcon;
updateIcon: (icons: EntityRegistryIcon | null) => Promise<unknown>;
}
export const loadEntityStateIconDialog = () =>
import("./dialog-entity-state-icon");
export const showEntityStateIconDialog = (
element: HTMLElement,
params: EntityStateIconDialogParams
): void => {
fireEvent(element, "show-dialog", {
dialogTag: "dialog-entity-state-icon",
dialogImport: loadEntityStateIconDialog,
dialogParams: params,
});
};

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 { 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";
@@ -25,13 +25,13 @@ import "../../../components/ha-area-picker";
import "../../../components/ha-icon"; import "../../../components/ha-icon";
import "../../../components/ha-icon-button-next"; import "../../../components/ha-icon-button-next";
import "../../../components/ha-icon-picker"; import "../../../components/ha-icon-picker";
import "../../../components/ha-labels-picker";
import "../../../components/ha-list-item"; import "../../../components/ha-list-item";
import "../../../components/ha-radio"; import "../../../components/ha-radio";
import "../../../components/ha-select"; import "../../../components/ha-select";
import "../../../components/ha-settings-row"; import "../../../components/ha-settings-row";
import "../../../components/ha-state-icon"; import "../../../components/ha-state-icon";
import "../../../components/ha-switch"; import "../../../components/ha-switch";
import "../../../components/ha-labels-picker";
import type { HaSwitch } from "../../../components/ha-switch"; import type { HaSwitch } from "../../../components/ha-switch";
import "../../../components/ha-textfield"; import "../../../components/ha-textfield";
import { import {
@@ -56,6 +56,7 @@ import type {
AlarmControlPanelEntityOptions, AlarmControlPanelEntityOptions,
EntityRegistryEntry, EntityRegistryEntry,
EntityRegistryEntryUpdateParams, EntityRegistryEntryUpdateParams,
EntityRegistryIcon,
ExtEntityRegistryEntry, ExtEntityRegistryEntry,
LockEntityOptions, LockEntityOptions,
SensorEntityOptions, SensorEntityOptions,
@@ -91,6 +92,7 @@ import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types"; import type { HomeAssistant } from "../../../types";
import { showToast } from "../../../util/toast"; import { showToast } from "../../../util/toast";
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail"; import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";
import { showEntityStateIconDialog } from "./dialogs/show-dialog-entity-state-icon";
const OVERRIDE_DEVICE_CLASSES = { const OVERRIDE_DEVICE_CLASSES = {
cover: [ cover: [
@@ -149,7 +151,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
@state() private _name!: string; @state() private _name!: string;
@state() private _icon!: string; @state() private _icon!: string | EntityRegistryIcon;
@state() private _entityId!: string; @state() private _entityId!: string;
@@ -349,6 +351,53 @@ export class EntityRegistrySettingsEditor extends LitElement {
} }
} }
private _renderIconPicker(stateObj: HassEntity) {
const useStateIcon = typeof this._icon === "object";
const value = useStateIcon ? "Custom state icons" : this._icon;
const placeholder =
this.entry.original_icon ||
(stateObj && until(entityIcon(this.hass, stateObj))) ||
until(entryIcon(this.hass, this.entry));
return html`
<div class="icon-container">
<ha-icon-picker
.hass=${this.hass}
.value=${value}
@value-changed=${this._iconChanged}
.label=${this.hass.localize("ui.dialogs.entity_registry.editor.icon")}
.placeholder=${placeholder}
.disabled=${this.disabled}
.forceFallbackIcon=${useStateIcon}
>
${useStateIcon
? html`
<ha-state-icon
slot="fallback"
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-icon>
`
: !this._icon && !stateObj?.attributes.icon && stateObj
? html`
<ha-state-icon
slot="fallback"
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-icon>
`
: nothing}
</ha-icon-picker>
<ha-icon-button
.path=${mdiBrush}
@click=${this._openEntityStateIcon}
></ha-icon-button>
</div>
`;
}
protected render() { protected render() {
if (this.entry.entity_id !== this._origEntityId) { if (this.entry.entity_id !== this._origEntityId) {
return nothing; return nothing;
@@ -380,33 +429,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
.placeholder=${this.entry.original_name} .placeholder=${this.entry.original_name}
@input=${this._nameChanged} @input=${this._nameChanged}
></ha-textfield>`} ></ha-textfield>`}
${this.hideIcon ${this.hideIcon ? nothing : this._renderIconPicker(stateObj)}
? nothing
: html`
<ha-icon-picker
.hass=${this.hass}
.value=${this._icon}
@value-changed=${this._iconChanged}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.icon"
)}
.placeholder=${this.entry.original_icon ||
stateObj?.attributes.icon ||
(stateObj && until(entityIcon(this.hass, stateObj))) ||
until(entryIcon(this.hass, this.entry))}
.disabled=${this.disabled}
>
${!this._icon && !stateObj?.attributes.icon && stateObj
? html`
<ha-state-icon
slot="fallback"
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-icon>
`
: nothing}
</ha-icon-picker>
`}
${domain === "switch" ${domain === "switch"
? html`<ha-select ? html`<ha-select
.label=${this.hass.localize( .label=${this.hass.localize(
@@ -1025,7 +1048,8 @@ export class EntityRegistrySettingsEditor extends LitElement {
const params: Partial<EntityRegistryEntryUpdateParams> = { const params: Partial<EntityRegistryEntryUpdateParams> = {
name: this._name.trim() || null, name: this._name.trim() || null,
icon: this._icon.trim() || null, icon:
typeof this._icon === "string" ? this._icon.trim() : this._icon || null,
area_id: this._areaId || null, area_id: this._areaId || null,
labels: this._labels || [], labels: this._labels || [],
new_entity_id: this._entityId.trim(), new_entity_id: this._entityId.trim(),
@@ -1455,6 +1479,17 @@ export class EntityRegistrySettingsEditor extends LitElement {
}); });
} }
private _openEntityStateIcon() {
showEntityStateIconDialog(this, {
entry: this.entry!,
icon: this._icon,
updateIcon: async (icon) => {
this._icon = icon || "";
fireEvent(this, "change");
},
});
}
private _handleVoiceAssistantsClicked() { private _handleVoiceAssistantsClicked() {
showVoiceAssistantsView( showVoiceAssistantsView(
this, this,
@@ -1549,6 +1584,18 @@ export class EntityRegistrySettingsEditor extends LitElement {
overflow: hidden; overflow: hidden;
--mdc-list-side-padding: 13px; --mdc-list-side-padding: 13px;
} }
.icon-container {
display: flex;
flex-direction: row;
align-items: center;
gap: 8px;
}
.icon-container ha-button-icon {
flex: none;
}
.icon-container ha-icon-picker {
flex: 1;
}
`, `,
]; ];
} }