Skip to content

Commit 3f4c8fb

Browse files
authored
Collapsible sections in entity configuration (#177)
1 parent 0b4d348 commit 3f4c8fb

File tree

3 files changed

+77
-27
lines changed

3 files changed

+77
-27
lines changed

src/components/knx-configure-entity-options.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,26 @@ export const renderConfigureEntityCard = (
6363
></ha-selector-text>
6464
</ha-settings-row>
6565
<ha-expansion-panel .header=${"Advanced"} outlined>
66-
<ha-settings-row narrow>
67-
<div slot="heading">Entity settings</div>
68-
<div slot="description">Description</div>
69-
<ha-selector-select
70-
.hass=${hass}
71-
.label=${"Entity category"}
72-
.helper=${"Leave empty for standard behaviour."}
73-
.required=${false}
74-
.selector=${{
75-
select: {
76-
multiple: false,
77-
custom_value: false,
78-
mode: "dropdown",
79-
options: [
80-
{ value: "config", label: "Config" },
81-
{ value: "diagnostic", label: "Diagnostic" },
82-
],
83-
},
84-
}}
85-
.key=${"entity_category"}
86-
.value=${config.entity_category}
87-
@value-changed=${updateConfig}
88-
></ha-selector-select>
89-
</ha-settings-row>
66+
<ha-selector-select
67+
.hass=${hass}
68+
.label=${"Entity category"}
69+
.helper=${"Leave empty for standard behaviour."}
70+
.required=${false}
71+
.selector=${{
72+
select: {
73+
multiple: false,
74+
custom_value: false,
75+
mode: "dropdown",
76+
options: [
77+
{ value: "config", label: "Config" },
78+
{ value: "diagnostic", label: "Diagnostic" },
79+
],
80+
},
81+
}}
82+
.key=${"entity_category"}
83+
.value=${config.entity_category}
84+
@value-changed=${updateConfig}
85+
></ha-selector-select>
9086
</ha-expansion-panel>
9187
</ha-card>
9288
`;

src/components/knx-configure-entity.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import "./knx-sync-state-selector-row";
1717
import { renderConfigureEntityCard } from "./knx-configure-entity-options";
1818
import { KNXLogger } from "../tools/knx-logger";
1919
import { extractValidationErrors } from "../utils/validation";
20-
import type { EntityData, ErrorDescription } from "../types/entity_data";
20+
import type { EntityData, ErrorDescription, KnxEntityData } from "../types/entity_data";
2121
import type { KNX } from "../types/knx";
2222
import type { PlatformInfo } from "../utils/common";
23-
import type { SettingsGroup, SelectorSchema, GroupSelect } from "../utils/schema";
23+
import type { SettingsGroup, SelectorSchema, GroupSelect, GASchema } from "../utils/schema";
2424

2525
const logger = new KNXLogger("knx-configure-entity");
2626

@@ -91,13 +91,53 @@ export class KNXConfigureEntity extends LitElement {
9191
}
9292

9393
_generateSettingsGroup(group: SettingsGroup, errors?: ErrorDescription[]) {
94+
if (group.collapsible === true) {
95+
return html` <ha-expansion-panel
96+
outlined
97+
.header=${group.heading}
98+
.secondary=${group.description}
99+
.expanded=${this._groupHasGroupAddressInConfig(group)}
100+
>${this._generateItems(group.selectors, errors)}
101+
</ha-expansion-panel>`;
102+
}
94103
return html` <ha-settings-row narrow>
95104
<div slot="heading">${group.heading}</div>
96105
<div slot="description">${group.description}</div>
97106
${this._generateItems(group.selectors, errors)}
98107
</ha-settings-row>`;
99108
}
100109

110+
_groupHasGroupAddressInConfig(group: SettingsGroup) {
111+
if (this.config === undefined) {
112+
return false;
113+
}
114+
return group.selectors.some((selector) => {
115+
if (selector.type === "group_address")
116+
return this._hasGroupAddressInConfig(selector, this.config!.knx);
117+
if (selector.type === "group_select")
118+
return selector.options.some((options) =>
119+
options.schema.some((schema) => {
120+
if (schema.type === "settings_group") return this._groupHasGroupAddressInConfig(schema);
121+
if (schema.type === "group_address")
122+
return this._hasGroupAddressInConfig(schema, this.config!.knx);
123+
return false;
124+
}),
125+
);
126+
return false;
127+
});
128+
}
129+
130+
_hasGroupAddressInConfig(ga_selector: GASchema, knxData: KnxEntityData) {
131+
if (!(ga_selector.name in knxData)) return false;
132+
133+
const knxEntry = knxData[ga_selector.name];
134+
if (knxEntry.write !== undefined) return true;
135+
if (knxEntry.state !== undefined) return true;
136+
if (knxEntry.passive?.length) return true;
137+
138+
return false;
139+
}
140+
101141
_generateItems(selectors: SelectorSchema[], errors?: ErrorDescription[]) {
102142
return html`${selectors.map((selector: SelectorSchema) =>
103143
this._generateItem(selector, errors),
@@ -245,7 +285,18 @@ export class KNXConfigureEntity extends LitElement {
245285
}
246286
}
247287
288+
ha-expansion-panel {
289+
margin-bottom: 16px;
290+
}
291+
ha-expansion-panel > :first-child {
292+
margin-top: 16px;
293+
}
294+
ha-expansion-panel > ha-settings-row:first-child {
295+
border: 0;
296+
}
297+
248298
ha-settings-row {
299+
margin-bottom: 16px;
249300
padding: 0;
250301
}
251302
ha-control-select {

src/utils/schema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type SettingsGroup = {
77
description: string;
88
selectors: SelectorSchema[];
99
advanced?: boolean;
10+
collapsible?: boolean;
1011
};
1112

1213
export type SelectorSchema =
@@ -22,7 +23,7 @@ export type SelectorSchema =
2223
helper?: string;
2324
};
2425

25-
type GASchema = {
26+
export type GASchema = {
2627
name: string;
2728
type: "group_address";
2829
label?: string;
@@ -141,6 +142,7 @@ export const lightSchema: SettingsGroup[] = [
141142
type: "settings_group",
142143
heading: "Color temperature",
143144
description: "Control the lights color temperature.",
145+
collapsible: true,
144146
selectors: [
145147
{
146148
name: "ga_color_temp",
@@ -206,6 +208,7 @@ export const lightSchema: SettingsGroup[] = [
206208
type: "settings_group",
207209
heading: "Color",
208210
description: "Control the light color.",
211+
collapsible: true,
209212
selectors: [
210213
{
211214
type: "group_select",

0 commit comments

Comments
 (0)