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:
Bram Kragten 2024-08-28 14:44:08 +02:00 committed by GitHub
parent 93ee5de1b4
commit 9568677926
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 83 additions and 13 deletions

View File

@ -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 })}"

View File

@ -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 = (

View 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;
}
}

View File

@ -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,