Skip to content

Commit b3fd79c

Browse files
authored
Merge pull request #3 from strhwste/develop
Refactor HausgeistCardEditor to replace ha-entity-combo-box with ha-s…
2 parents 4240500 + 5a9620c commit b3fd79c

File tree

6 files changed

+197
-165
lines changed

6 files changed

+197
-165
lines changed

dist-ts/src/hausgeist-card-editor.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { LitElement } from 'lit';
22
import '@material/mwc-select';
33
import '@material/mwc-list/mwc-list-item';
4-
import './ha-entity-combo-box';
54
export declare class HausgeistCardEditor extends LitElement {
65
config: {
76
debug?: boolean;
@@ -23,7 +22,7 @@ export declare class HausgeistCardEditor extends LitElement {
2322
rulesJson: string;
2423
notify: boolean;
2524
highThreshold: number;
26-
private _hasHaEntityComboBox;
25+
private _selectorReady;
2726
private _lastAreas;
2827
private _autodetect;
2928
private _renderWeatherInfo;
@@ -40,7 +39,7 @@ export declare class HausgeistCardEditor extends LitElement {
4039
handleThresholdChange(e: any): void;
4140
private _onAreaEnabledChange;
4241
render(): import("lit-html").TemplateResult<1>;
43-
private _ensureHaEntityComboBox;
4442
private _renderEntityPicker;
4543
private _createOptionsForDomains;
44+
private _ensureSelectorSystem;
4645
}

dist-ts/src/hausgeist-card-editor.js

Lines changed: 62 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist-ts/src/hausgeist-card-editor.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"homeassistant": "2023.12.0",
2121
"country": "DE, EN",
2222
"default_branch": "main",
23-
"branches": ["main", "dev", "beta"],
23+
"branches": ["main", "develop", "beta"],
2424
"hide_default_branch": false
2525
}

hausgeist-card.js

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7640,7 +7640,7 @@ let HausgeistCardEditor = class HausgeistCardEditor extends i$x {
76407640
this.rulesJson = '';
76417641
this.notify = false;
76427642
this.highThreshold = 2000;
7643-
this._hasHaEntityComboBox = !!customElements.get('ha-entity-combo-box');
7643+
this._selectorReady = false;
76447644
this._lastAreas = [];
76457645
// Use arrow function to auto-bind 'this'
76467646
this._onDebugChange = (e) => {
@@ -7715,16 +7715,12 @@ let HausgeistCardEditor = class HausgeistCardEditor extends i$x {
77157715
}
77167716
set hass(hass) {
77177717
this._hass = hass;
7718+
this._ensureSelectorSystem();
77187719
this.requestUpdate();
77197720
}
77207721
connectedCallback() {
77217722
super.connectedCallback();
7722-
this._ensureHaEntityComboBox();
7723-
customElements.whenDefined('ha-entity-combo-box').then(() => {
7724-
if (!this.isConnected)
7725-
return;
7726-
this._hasHaEntityComboBox = true;
7727-
});
7723+
this._ensureSelectorSystem();
77287724
}
77297725
// Handle sensor selection change for a specific area and type
77307726
_onAreaSensorChange(areaId, type, value) {
@@ -8065,59 +8061,41 @@ let HausgeistCardEditor = class HausgeistCardEditor extends i$x {
80658061
</div>
80668062
`;
80678063
}
8068-
async _ensureHaEntityComboBox() {
8069-
if (this._hasHaEntityComboBox) {
8070-
return;
8071-
}
8072-
const helpersLoader = window?.loadCardHelpers;
8073-
if (typeof helpersLoader === 'function') {
8074-
try {
8075-
const helpers = await helpersLoader();
8076-
const glanceCard = await helpers?.createCardElement?.({ type: 'glance' });
8077-
await glanceCard?.getConfigElement?.();
8078-
}
8079-
catch (error) {
8080-
if (this.config?.debug) {
8081-
console.warn('Failed to load card helpers for ha-entity-combo-box', error);
8082-
}
8083-
}
8084-
}
8085-
const glanceCtor = customElements.get('hui-glance-card');
8086-
if (glanceCtor?.getConfigElement) {
8087-
try {
8088-
await glanceCtor.getConfigElement();
8089-
}
8090-
catch (error) {
8091-
if (this.config?.debug) {
8092-
console.warn('Failed to load hui-glance-card editor', error);
8093-
}
8094-
}
8095-
}
8096-
this._hasHaEntityComboBox = !!customElements.get('ha-entity-combo-box');
8097-
}
80988064
_renderEntityPicker({ value, includeDomains, area, options, placeholder, onChange, }) {
8099-
if (this._hasHaEntityComboBox) {
8065+
const optionList = options?.length
8066+
? [...options]
8067+
: this._createOptionsForDomains(includeDomains, area);
8068+
if (value && !optionList.some(option => option.entity_id === value)) {
8069+
optionList.unshift({ entity_id: value, label: value });
8070+
}
8071+
const selectorConfig = optionList.length
8072+
? { entity: { entity_id: optionList.map(option => option.entity_id) } }
8073+
: includeDomains?.length
8074+
? { entity: { domain: includeDomains.length === 1 ? includeDomains[0] : includeDomains } }
8075+
: { entity: {} };
8076+
if (this._selectorReady && customElements.get('ha-selector')) {
81008077
return x$6 `
8101-
<ha-entity-combo-box
8078+
<ha-selector
81028079
.hass=${this.hass}
8080+
.selector=${selectorConfig}
81038081
.value=${value || ''}
8104-
.includeDomains=${includeDomains}
8105-
.area=${area}
8106-
@value-changed=${(e) => onChange(e.detail?.value ?? '')}
8107-
></ha-entity-combo-box>
8082+
.label=${placeholder || undefined}
8083+
@value-changed=${(e) => onChange((e.detail && 'value' in e.detail) ? e.detail.value || '' : '')}
8084+
></ha-selector>
81088085
`;
81098086
}
8110-
const fallbackOptions = options?.length
8111-
? options
8112-
: this._createOptionsForDomains(includeDomains, area);
81138087
return x$6 `
8114-
<select
8088+
<mwc-select
8089+
outlined
81158090
.value=${value || ''}
8116-
@change=${(event) => onChange(event.target.value)}
8091+
@selected=${(event) => {
8092+
const select = event.target;
8093+
onChange(select?.value || '');
8094+
}}
81178095
>
8118-
<option value="">${placeholder || 'Select entity'}</option>
8119-
${fallbackOptions.map(option => x$6 `<option value=${option.entity_id}>${option.label}</option>`)}
8120-
</select>
8096+
<mwc-list-item value="">${placeholder || 'Keine Auswahl'}</mwc-list-item>
8097+
${optionList.map(option => x$6 `<mwc-list-item value=${option.entity_id}>${option.label}</mwc-list-item>`)}
8098+
</mwc-select>
81218099
`;
81228100
}
81238101
_createOptionsForDomains(includeDomains, area) {
@@ -8151,6 +8129,38 @@ let HausgeistCardEditor = class HausgeistCardEditor extends i$x {
81518129
options.sort((a, b) => a.label.localeCompare(b.label));
81528130
return options;
81538131
}
8132+
async _ensureSelectorSystem() {
8133+
if (this._selectorReady) {
8134+
return;
8135+
}
8136+
const helpersLoader = window?.loadCardHelpers;
8137+
if (typeof helpersLoader === 'function') {
8138+
try {
8139+
const helpers = await helpersLoader();
8140+
await helpers?.loadHaForm?.();
8141+
}
8142+
catch (error) {
8143+
if (this.config?.debug) {
8144+
console.warn('Failed to load selector helpers', error);
8145+
}
8146+
}
8147+
}
8148+
if (customElements.get('ha-selector')) {
8149+
this._selectorReady = true;
8150+
return;
8151+
}
8152+
try {
8153+
await customElements.whenDefined('ha-selector');
8154+
if (this.isConnected) {
8155+
this._selectorReady = true;
8156+
}
8157+
}
8158+
catch (error) {
8159+
if (this.config?.debug) {
8160+
console.warn('ha-selector never became available', error);
8161+
}
8162+
}
8163+
}
81548164
};
81558165
__decorate$1([
81568166
n$H({ type: Object })
@@ -8169,7 +8179,7 @@ __decorate$1([
81698179
], HausgeistCardEditor.prototype, "highThreshold", void 0);
81708180
__decorate$1([
81718181
r$l()
8172-
], HausgeistCardEditor.prototype, "_hasHaEntityComboBox", void 0);
8182+
], HausgeistCardEditor.prototype, "_selectorReady", void 0);
81738183
HausgeistCardEditor = __decorate$1([
81748184
t$p('hausgeist-card-editor')
81758185
], HausgeistCardEditor);

0 commit comments

Comments
 (0)