mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 09:46:36 +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>
|
||||
`
|
||||
: ""}
|
||||
<slot name="icons"></slot>
|
||||
</div>
|
||||
<slot name="icons"></slot>
|
||||
</div>
|
||||
<div
|
||||
class="container ${classMap({ expanded: this.expanded })}"
|
||||
|
@ -44,6 +44,7 @@ import "./ha-service-picker";
|
||||
import "./ha-settings-row";
|
||||
import "./ha-yaml-editor";
|
||||
import type { HaYamlEditor } from "./ha-yaml-editor";
|
||||
import "./ha-service-section-icon";
|
||||
|
||||
const attributeFilter = (values: any[], attribute: any) => {
|
||||
if (typeof attribute === "object") {
|
||||
@ -496,12 +497,18 @@ export class HaServiceControl extends LitElement {
|
||||
) ||
|
||||
dataField.name ||
|
||||
dataField.key}
|
||||
>
|
||||
${this._renderSectionDescription(
|
||||
.secondary=${this._getSectionDescription(
|
||||
dataField,
|
||||
domain,
|
||||
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]) =>
|
||||
this._renderField(
|
||||
{ key, ...field },
|
||||
@ -522,20 +529,14 @@ export class HaServiceControl extends LitElement {
|
||||
)} `;
|
||||
}
|
||||
|
||||
private _renderSectionDescription(
|
||||
private _getSectionDescription(
|
||||
dataField: ExtHassService["fields"][number],
|
||||
domain: string | undefined,
|
||||
serviceName: string | undefined
|
||||
) {
|
||||
const description = this.hass!.localize(
|
||||
return this.hass!.localize(
|
||||
`component.${domain}.services.${serviceName}.sections.${dataField.key}.description`
|
||||
);
|
||||
|
||||
if (!description) {
|
||||
return nothing;
|
||||
}
|
||||
|
||||
return html`<p>${description}</p>`;
|
||||
}
|
||||
|
||||
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 {
|
||||
[service: string]: string;
|
||||
[service: string]: { service: string; sections?: { [name: string]: string } };
|
||||
}
|
||||
|
||||
export type IconCategory = "entity" | "entity_component" | "services";
|
||||
@ -288,7 +288,8 @@ export const serviceIcon = async (
|
||||
const serviceName = computeObjectId(service);
|
||||
const serviceIcons = await getServiceIcons(hass, domain);
|
||||
if (serviceIcons) {
|
||||
icon = serviceIcons[serviceName] as string;
|
||||
const srvceIcon = serviceIcons[serviceName] as ServiceIcons[string];
|
||||
icon = srvceIcon?.service;
|
||||
}
|
||||
if (!icon) {
|
||||
icon = await domainIcon(hass, domain);
|
||||
@ -296,6 +297,21 @@ export const serviceIcon = async (
|
||||
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 (
|
||||
hass: HomeAssistant,
|
||||
domain: string,
|
||||
|
Loading…
x
Reference in New Issue
Block a user