mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add support for service section icons (#21806)
* Add support for service section icons * remove backwards compatibility core handles it * Update icons.ts
This commit is contained in:
parent
93ee5de1b4
commit
9568677926
@ -68,8 +68,8 @@ export class HaExpansionPanel extends LitElement {
|
|||||||
></ha-svg-icon>
|
></ha-svg-icon>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
|
<slot name="icons"></slot>
|
||||||
</div>
|
</div>
|
||||||
<slot name="icons"></slot>
|
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
class="container ${classMap({ expanded: this.expanded })}"
|
class="container ${classMap({ expanded: this.expanded })}"
|
||||||
|
@ -44,6 +44,7 @@ import "./ha-service-picker";
|
|||||||
import "./ha-settings-row";
|
import "./ha-settings-row";
|
||||||
import "./ha-yaml-editor";
|
import "./ha-yaml-editor";
|
||||||
import type { HaYamlEditor } from "./ha-yaml-editor";
|
import type { HaYamlEditor } from "./ha-yaml-editor";
|
||||||
|
import "./ha-service-section-icon";
|
||||||
|
|
||||||
const attributeFilter = (values: any[], attribute: any) => {
|
const attributeFilter = (values: any[], attribute: any) => {
|
||||||
if (typeof attribute === "object") {
|
if (typeof attribute === "object") {
|
||||||
@ -496,12 +497,18 @@ export class HaServiceControl extends LitElement {
|
|||||||
) ||
|
) ||
|
||||||
dataField.name ||
|
dataField.name ||
|
||||||
dataField.key}
|
dataField.key}
|
||||||
>
|
.secondary=${this._getSectionDescription(
|
||||||
${this._renderSectionDescription(
|
|
||||||
dataField,
|
dataField,
|
||||||
domain,
|
domain,
|
||||||
serviceName
|
serviceName
|
||||||
)}
|
)}
|
||||||
|
>
|
||||||
|
<ha-service-section-icon
|
||||||
|
slot="icons"
|
||||||
|
.hass=${this.hass}
|
||||||
|
.service=${this._value!.action}
|
||||||
|
.section=${dataField.key}
|
||||||
|
></ha-service-section-icon>
|
||||||
${Object.entries(dataField.fields).map(([key, field]) =>
|
${Object.entries(dataField.fields).map(([key, field]) =>
|
||||||
this._renderField(
|
this._renderField(
|
||||||
{ key, ...field },
|
{ key, ...field },
|
||||||
@ -522,20 +529,14 @@ export class HaServiceControl extends LitElement {
|
|||||||
)} `;
|
)} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _renderSectionDescription(
|
private _getSectionDescription(
|
||||||
dataField: ExtHassService["fields"][number],
|
dataField: ExtHassService["fields"][number],
|
||||||
domain: string | undefined,
|
domain: string | undefined,
|
||||||
serviceName: string | undefined
|
serviceName: string | undefined
|
||||||
) {
|
) {
|
||||||
const description = this.hass!.localize(
|
return this.hass!.localize(
|
||||||
`component.${domain}.services.${serviceName}.sections.${dataField.key}.description`
|
`component.${domain}.services.${serviceName}.sections.${dataField.key}.description`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!description) {
|
|
||||||
return nothing;
|
|
||||||
}
|
|
||||||
|
|
||||||
return html`<p>${description}</p>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _renderField = (
|
private _renderField = (
|
||||||
|
53
src/components/ha-service-section-icon.ts
Normal file
53
src/components/ha-service-section-icon.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { html, LitElement, nothing } from "lit";
|
||||||
|
import { customElement, property } from "lit/decorators";
|
||||||
|
import { until } from "lit/directives/until";
|
||||||
|
import { HomeAssistant } from "../types";
|
||||||
|
import "./ha-icon";
|
||||||
|
import "./ha-svg-icon";
|
||||||
|
import { serviceSectionIcon } from "../data/icons";
|
||||||
|
|
||||||
|
@customElement("ha-service-section-icon")
|
||||||
|
export class HaServiceSectionIcon extends LitElement {
|
||||||
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
|
@property() public service?: string;
|
||||||
|
|
||||||
|
@property() public section?: string;
|
||||||
|
|
||||||
|
@property() public icon?: string;
|
||||||
|
|
||||||
|
protected render() {
|
||||||
|
if (this.icon) {
|
||||||
|
return html`<ha-icon .icon=${this.icon}></ha-icon>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.service || !this.section) {
|
||||||
|
return nothing;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.hass) {
|
||||||
|
return this._renderFallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
const icon = serviceSectionIcon(this.hass, this.service, this.section).then(
|
||||||
|
(icn) => {
|
||||||
|
if (icn) {
|
||||||
|
return html`<ha-icon .icon=${icn}></ha-icon>`;
|
||||||
|
}
|
||||||
|
return this._renderFallback();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return html`${until(icon)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _renderFallback() {
|
||||||
|
return nothing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-service-section-icon": HaServiceSectionIcon;
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,7 @@ export interface ComponentIcons {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ServiceIcons {
|
interface ServiceIcons {
|
||||||
[service: string]: string;
|
[service: string]: { service: string; sections?: { [name: string]: string } };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IconCategory = "entity" | "entity_component" | "services";
|
export type IconCategory = "entity" | "entity_component" | "services";
|
||||||
@ -288,7 +288,8 @@ export const serviceIcon = async (
|
|||||||
const serviceName = computeObjectId(service);
|
const serviceName = computeObjectId(service);
|
||||||
const serviceIcons = await getServiceIcons(hass, domain);
|
const serviceIcons = await getServiceIcons(hass, domain);
|
||||||
if (serviceIcons) {
|
if (serviceIcons) {
|
||||||
icon = serviceIcons[serviceName] as string;
|
const srvceIcon = serviceIcons[serviceName] as ServiceIcons[string];
|
||||||
|
icon = srvceIcon?.service;
|
||||||
}
|
}
|
||||||
if (!icon) {
|
if (!icon) {
|
||||||
icon = await domainIcon(hass, domain);
|
icon = await domainIcon(hass, domain);
|
||||||
@ -296,6 +297,21 @@ export const serviceIcon = async (
|
|||||||
return icon;
|
return icon;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const serviceSectionIcon = async (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
service: string,
|
||||||
|
section: string
|
||||||
|
): Promise<string | undefined> => {
|
||||||
|
const domain = computeDomain(service);
|
||||||
|
const serviceName = computeObjectId(service);
|
||||||
|
const serviceIcons = await getServiceIcons(hass, domain);
|
||||||
|
if (serviceIcons) {
|
||||||
|
const srvceIcon = serviceIcons[serviceName] as ServiceIcons[string];
|
||||||
|
return srvceIcon?.sections?.[section];
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export const domainIcon = async (
|
export const domainIcon = async (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
domain: string,
|
domain: string,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user