Fix floor creation in floor picker (#20276)

This commit is contained in:
Paul Bottein 2024-03-29 18:05:06 +01:00 committed by GitHub
parent f539516252
commit 1dba049038
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -38,10 +38,14 @@ import "./ha-list-item";
type ScorableFloorRegistryEntry = ScorableTextItem & FloorRegistryEntry; type ScorableFloorRegistryEntry = ScorableTextItem & FloorRegistryEntry;
const ADD_NEW_ID = "___ADD_NEW___";
const NO_FLOORS_ID = "___NO_FLOORS___";
const ADD_NEW_SUGGESTION_ID = "___ADD_NEW_SUGGESTION___";
const rowRenderer: ComboBoxLitRenderer<FloorRegistryEntry> = (item) => const rowRenderer: ComboBoxLitRenderer<FloorRegistryEntry> = (item) =>
html`<ha-list-item html`<ha-list-item
graphic="icon" graphic="icon"
class=${classMap({ "add-new": item.floor_id === "add_new" })} class=${classMap({ "add-new": item.floor_id === ADD_NEW_ID })}
> >
<ha-floor-icon slot="graphic" .floor=${item}></ha-floor-icon> <ha-floor-icon slot="graphic" .floor=${item}></ha-floor-icon>
${item.name} ${item.name}
@ -146,18 +150,6 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
noAdd: this["noAdd"], noAdd: this["noAdd"],
excludeFloors: this["excludeFloors"] excludeFloors: this["excludeFloors"]
): FloorRegistryEntry[] => { ): FloorRegistryEntry[] => {
if (!floors.length) {
return [
{
floor_id: "no_floors",
name: this.hass.localize("ui.components.floor-picker.no_floors"),
icon: null,
level: 0,
aliases: [],
},
];
}
let deviceEntityLookup: DeviceEntityDisplayLookup = {}; let deviceEntityLookup: DeviceEntityDisplayLookup = {};
let inputDevices: DeviceRegistryEntry[] | undefined; let inputDevices: DeviceRegistryEntry[] | undefined;
let inputEntities: EntityRegistryDisplayEntry[] | undefined; let inputEntities: EntityRegistryDisplayEntry[] | undefined;
@ -297,10 +289,10 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
if (!outputFloors.length) { if (!outputFloors.length) {
outputFloors = [ outputFloors = [
{ {
floor_id: "no_floors", floor_id: NO_FLOORS_ID,
name: this.hass.localize("ui.components.floor-picker.no_match"), name: this.hass.localize("ui.components.floor-picker.no_floors"),
icon: null, icon: null,
level: 0, level: null,
aliases: [], aliases: [],
}, },
]; ];
@ -311,10 +303,10 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
: [ : [
...outputFloors, ...outputFloors,
{ {
floor_id: "add_new", floor_id: ADD_NEW_ID,
name: this.hass.localize("ui.components.floor-picker.add_new"), name: this.hass.localize("ui.components.floor-picker.add_new"),
icon: "mdi:plus", icon: "mdi:plus",
level: 0, level: null,
aliases: [], aliases: [],
}, },
]; ];
@ -341,7 +333,7 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
this.excludeFloors this.excludeFloors
).map((floor) => ({ ).map((floor) => ({
...floor, ...floor,
strings: [floor.floor_id, floor.name], // ...floor.aliases strings: [floor.floor_id, floor.name, ...floor.aliases],
})); }));
this.comboBox.items = floors; this.comboBox.items = floors;
this.comboBox.filteredItems = floors; this.comboBox.filteredItems = floors;
@ -385,20 +377,36 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
const filteredItems = fuzzyFilterSort<ScorableFloorRegistryEntry>( const filteredItems = fuzzyFilterSort<ScorableFloorRegistryEntry>(
filterString, filterString,
target.items || [] target.items?.filter(
(item) => ![NO_FLOORS_ID, ADD_NEW_ID].includes(item.label_id)
) || []
); );
if (!this.noAdd && filteredItems?.length === 0) { if (filteredItems.length === 0) {
this._suggestion = filterString; if (this.noAdd) {
this.comboBox.filteredItems = [ this.comboBox.filteredItems = [
{ {
floor_id: "add_new_suggestion", floor_id: NO_FLOORS_ID,
name: this.hass.localize( name: this.hass.localize("ui.components.floor-picker.no_floors"),
"ui.components.floor-picker.add_new_sugestion", icon: null,
{ name: this._suggestion } level: null,
), aliases: [],
picture: null, },
}, ] as FloorRegistryEntry[];
]; } else {
this._suggestion = filterString;
this.comboBox.filteredItems = [
{
floor_id: ADD_NEW_SUGGESTION_ID,
name: this.hass.localize(
"ui.components.floor-picker.add_new_sugestion",
{ name: this._suggestion }
),
icon: "mdi:plus",
level: null,
aliases: [],
},
] as FloorRegistryEntry[];
}
} else { } else {
this.comboBox.filteredItems = filteredItems; this.comboBox.filteredItems = filteredItems;
} }
@ -416,11 +424,13 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
ev.stopPropagation(); ev.stopPropagation();
let newValue = ev.detail.value; let newValue = ev.detail.value;
if (newValue === "no_floors") { if (newValue === NO_FLOORS_ID) {
newValue = ""; newValue = "";
this.comboBox.setInputValue("");
return;
} }
if (!["add_new_suggestion", "add_new"].includes(newValue)) { if (![ADD_NEW_SUGGESTION_ID, ADD_NEW_ID].includes(newValue)) {
if (newValue !== this._value) { if (newValue !== this._value) {
this._setValue(newValue); this._setValue(newValue);
} }
@ -438,7 +448,7 @@ export class HaFloorPicker extends SubscribeMixin(LitElement) {
"ui.components.floor-picker.add_dialog.name" "ui.components.floor-picker.add_dialog.name"
), ),
defaultValue: defaultValue:
newValue === "add_new_suggestion" ? this._suggestion : undefined, newValue === ADD_NEW_SUGGESTION_ID ? this._suggestion : undefined,
confirm: async (name) => { confirm: async (name) => {
if (!name) { if (!name) {
return; return;