mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-26 06:17:20 +00:00
Replace createValidEntityId with slugify (#6505)
This commit is contained in:
parent
a2153bc6aa
commit
0d515e2303
@ -2,11 +2,3 @@ const validEntityId = /^(\w+)\.(\w+)$/;
|
|||||||
|
|
||||||
export const isValidEntityId = (entityId: string) =>
|
export const isValidEntityId = (entityId: string) =>
|
||||||
validEntityId.test(entityId);
|
validEntityId.test(entityId);
|
||||||
|
|
||||||
export const createValidEntityId = (input: string) =>
|
|
||||||
input
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/\s|'|\./g, "_") // replace spaces, points and quotes with underscore
|
|
||||||
.replace(/\W/g, "") // remove not allowed chars
|
|
||||||
.replace(/_{2,}/g, "_") // replace multiple underscores with 1
|
|
||||||
.replace(/_$/, ""); // remove underscores at the end
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
|
// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
|
||||||
export const slugify = (value: string, delimiter = "-") => {
|
export const slugify = (value: string, delimiter = "_") => {
|
||||||
const a =
|
const a =
|
||||||
"àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;";
|
"àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;";
|
||||||
const b = `aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}`;
|
const b = `aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}`;
|
||||||
|
@ -13,7 +13,6 @@ import { ifDefined } from "lit-html/directives/if-defined";
|
|||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
|
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
|
||||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||||
import { createValidEntityId } from "../../../common/entity/valid_entity_id";
|
|
||||||
import { compare } from "../../../common/string/compare";
|
import { compare } from "../../../common/string/compare";
|
||||||
import "../../../components/entity/ha-battery-icon";
|
import "../../../components/entity/ha-battery-icon";
|
||||||
import "../../../components/ha-icon-next";
|
import "../../../components/ha-icon-next";
|
||||||
@ -44,6 +43,7 @@ import { configSections } from "../ha-panel-config";
|
|||||||
import "./device-detail/ha-device-entities-card";
|
import "./device-detail/ha-device-entities-card";
|
||||||
import "./device-detail/ha-device-info-card";
|
import "./device-detail/ha-device-info-card";
|
||||||
import { showDeviceAutomationDialog } from "./device-detail/show-dialog-device-automation";
|
import { showDeviceAutomationDialog } from "./device-detail/show-dialog-device-automation";
|
||||||
|
import { slugify } from "../../../common/string/slugify";
|
||||||
|
|
||||||
export interface EntityRegistryStateEntry extends EntityRegistryEntry {
|
export interface EntityRegistryStateEntry extends EntityRegistryEntry {
|
||||||
stateName?: string | null;
|
stateName?: string | null;
|
||||||
@ -556,11 +556,11 @@ export class HaConfigDevicePage extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (renameEntityid) {
|
if (renameEntityid) {
|
||||||
const oldSearch = createValidEntityId(oldDeviceName);
|
const oldSearch = slugify(oldDeviceName);
|
||||||
if (entity.entity_id.includes(oldSearch)) {
|
if (entity.entity_id.includes(oldSearch)) {
|
||||||
newEntityId = entity.entity_id.replace(
|
newEntityId = entity.entity_id.replace(
|
||||||
oldSearch,
|
oldSearch,
|
||||||
createValidEntityId(newDeviceName)
|
slugify(newDeviceName)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -571,7 +571,6 @@ export class HaConfigDevicePage extends LitElement {
|
|||||||
|
|
||||||
return updateEntityRegistryEntry(this.hass!, entity.entity_id, {
|
return updateEntityRegistryEntry(this.hass!, entity.entity_id, {
|
||||||
name: newName || name,
|
name: newName || name,
|
||||||
disabled_by: entity.disabled_by,
|
|
||||||
new_entity_id: newEntityId || entity.entity_id,
|
new_entity_id: newEntityId || entity.entity_id,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -29,11 +29,11 @@ import {
|
|||||||
EntityRegistryEntry,
|
EntityRegistryEntry,
|
||||||
updateEntityRegistryEntry,
|
updateEntityRegistryEntry,
|
||||||
} from "../../../../../data/entity_registry";
|
} from "../../../../../data/entity_registry";
|
||||||
import { createValidEntityId } from "../../../../../common/entity/valid_entity_id";
|
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { EntityRegistryStateEntry } from "../../../devices/ha-config-device-page";
|
import { EntityRegistryStateEntry } from "../../../devices/ha-config-device-page";
|
||||||
import { compare } from "../../../../../common/string/compare";
|
import { compare } from "../../../../../common/string/compare";
|
||||||
import { getIeeeTail } from "./functions";
|
import { getIeeeTail } from "./functions";
|
||||||
|
import { slugify } from "../../../../../common/string/slugify";
|
||||||
|
|
||||||
@customElement("zha-device-card")
|
@customElement("zha-device-card")
|
||||||
class ZHADeviceCard extends SubscribeMixin(LitElement) {
|
class ZHADeviceCard extends SubscribeMixin(LitElement) {
|
||||||
@ -142,8 +142,8 @@ class ZHADeviceCard extends SubscribeMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
const entities = this._deviceEntities(device.device_reg_id, this._entities);
|
const entities = this._deviceEntities(device.device_reg_id, this._entities);
|
||||||
|
|
||||||
const oldDeviceEntityId = createValidEntityId(oldDeviceName);
|
const oldDeviceEntityId = slugify(oldDeviceName);
|
||||||
const newDeviceEntityId = createValidEntityId(newDeviceName);
|
const newDeviceEntityId = slugify(newDeviceName);
|
||||||
const ieeeTail = getIeeeTail(device.ieee);
|
const ieeeTail = getIeeeTail(device.ieee);
|
||||||
|
|
||||||
const updateProms = entities.map((entity) => {
|
const updateProms = entities.map((entity) => {
|
||||||
|
@ -253,7 +253,7 @@ export class DialogLovelaceDashboardDetail extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const slugifyTitle = slugify(this._title);
|
const slugifyTitle = slugify(this._title, "-");
|
||||||
this._urlPath = slugifyTitle.includes("-")
|
this._urlPath = slugifyTitle.includes("-")
|
||||||
? slugifyTitle
|
? slugifyTitle
|
||||||
: `lovelace-${slugifyTitle}`;
|
: `lovelace-${slugifyTitle}`;
|
||||||
|
@ -319,7 +319,7 @@ export class HaScriptEditor extends LitElement {
|
|||||||
if (this.scriptEntityId || this._entityId) {
|
if (this.scriptEntityId || this._entityId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const aliasSlugify = slugify((ev.target as any).value, "_");
|
const aliasSlugify = slugify((ev.target as any).value);
|
||||||
let id = aliasSlugify;
|
let id = aliasSlugify;
|
||||||
let i = 2;
|
let i = 2;
|
||||||
while (this.hass.states[`script.${id}`]) {
|
while (this.hass.states[`script.${id}`]) {
|
||||||
|
@ -175,7 +175,10 @@ export class HuiViewEditor extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = { ...this._config, path: slugify(ev.currentTarget.value) };
|
const config = {
|
||||||
|
...this._config,
|
||||||
|
path: slugify(ev.currentTarget.value, "-"),
|
||||||
|
};
|
||||||
fireEvent(this, "view-config-changed", { config });
|
fireEvent(this, "view-config-changed", { config });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user