mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-30 16:27:21 +00:00
Add switch as x to entity settings (#12161)
Co-authored-by: Zack <zackbarett@hey.com>
This commit is contained in:
parent
bad776b979
commit
c2c09b1284
@ -28,6 +28,11 @@ import {
|
|||||||
deleteConfigEntry,
|
deleteConfigEntry,
|
||||||
getConfigEntries,
|
getConfigEntries,
|
||||||
} from "../../../data/config_entries";
|
} from "../../../data/config_entries";
|
||||||
|
import {
|
||||||
|
createConfigFlow,
|
||||||
|
handleConfigFlowStep,
|
||||||
|
} from "../../../data/config_flow";
|
||||||
|
import { DataEntryFlowStepCreateEntry } from "../../../data/data_entry_flow";
|
||||||
import {
|
import {
|
||||||
DeviceRegistryEntry,
|
DeviceRegistryEntry,
|
||||||
subscribeDeviceRegistry,
|
subscribeDeviceRegistry,
|
||||||
@ -36,9 +41,11 @@ import {
|
|||||||
import {
|
import {
|
||||||
EntityRegistryEntryUpdateParams,
|
EntityRegistryEntryUpdateParams,
|
||||||
ExtEntityRegistryEntry,
|
ExtEntityRegistryEntry,
|
||||||
|
fetchEntityRegistry,
|
||||||
removeEntityRegistryEntry,
|
removeEntityRegistryEntry,
|
||||||
updateEntityRegistryEntry,
|
updateEntityRegistryEntry,
|
||||||
} from "../../../data/entity_registry";
|
} from "../../../data/entity_registry";
|
||||||
|
import { domainToName } from "../../../data/integration";
|
||||||
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
|
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
|
||||||
import {
|
import {
|
||||||
showAlertDialog,
|
showAlertDialog,
|
||||||
@ -48,6 +55,7 @@ import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
|||||||
import { haStyle } from "../../../resources/styles";
|
import { haStyle } from "../../../resources/styles";
|
||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";
|
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";
|
||||||
|
import { showEntityEditorDialog } from "./show-dialog-entity-editor";
|
||||||
|
|
||||||
const OVERRIDE_DEVICE_CLASSES = {
|
const OVERRIDE_DEVICE_CLASSES = {
|
||||||
cover: [
|
cover: [
|
||||||
@ -88,6 +96,8 @@ const OVERRIDE_SENSOR_UNITS = {
|
|||||||
pressure: ["hPa", "Pa", "kPa", "bar", "cbar", "mbar", "mmHg", "inHg", "psi"],
|
pressure: ["hPa", "Pa", "kPa", "bar", "cbar", "mbar", "mmHg", "inHg", "psi"],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const SWITCH_AS_DOMAINS = ["light", "lock", "cover", "fan", "siren"];
|
||||||
|
|
||||||
@customElement("entity-registry-settings")
|
@customElement("entity-registry-settings")
|
||||||
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -102,6 +112,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@state() private _deviceClass?: string;
|
@state() private _deviceClass?: string;
|
||||||
|
|
||||||
|
@state() private _switchAs = "switch";
|
||||||
|
|
||||||
@state() private _areaId?: string | null;
|
@state() private _areaId?: string | null;
|
||||||
|
|
||||||
@state() private _disabledBy!: string | null;
|
@state() private _disabledBy!: string | null;
|
||||||
@ -263,7 +275,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
>
|
>
|
||||||
${this._deviceClassOptions[0].map(
|
${this._deviceClassOptions[0].map(
|
||||||
(deviceClass: string) => html`
|
(deviceClass: string) => html`
|
||||||
<mwc-list-item .value=${deviceClass} test=${deviceClass}>
|
<mwc-list-item .value=${deviceClass}>
|
||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
|
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
|
||||||
)}
|
)}
|
||||||
@ -273,7 +285,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
<li divider role="separator"></li>
|
<li divider role="separator"></li>
|
||||||
${this._deviceClassOptions[1].map(
|
${this._deviceClassOptions[1].map(
|
||||||
(deviceClass: string) => html`
|
(deviceClass: string) => html`
|
||||||
<mwc-list-item .value=${deviceClass} test=${deviceClass}>
|
<mwc-list-item .value=${deviceClass}>
|
||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
|
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
|
||||||
)}
|
)}
|
||||||
@ -307,6 +319,28 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
</ha-select>
|
</ha-select>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
|
${domain === "switch"
|
||||||
|
? html`<ha-select
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.dialogs.entity_registry.editor.device_class"
|
||||||
|
)}
|
||||||
|
naturalMenuWidth
|
||||||
|
fixedMenuPosition
|
||||||
|
@selected=${this._switchAsChanged}
|
||||||
|
@closed=${stopPropagation}
|
||||||
|
>
|
||||||
|
<mwc-list-item value="switch" selected>
|
||||||
|
${domainToName(this.hass.localize, "switch")}</mwc-list-item
|
||||||
|
>
|
||||||
|
${SWITCH_AS_DOMAINS.map(
|
||||||
|
(as_domain) => html`
|
||||||
|
<mwc-list-item .value=${as_domain}>
|
||||||
|
${domainToName(this.hass.localize, as_domain)}
|
||||||
|
</mwc-list-item>
|
||||||
|
`
|
||||||
|
)}
|
||||||
|
</ha-select>`
|
||||||
|
: ""}
|
||||||
<ha-textfield
|
<ha-textfield
|
||||||
error-message="Domain needs to stay the same"
|
error-message="Domain needs to stay the same"
|
||||||
.value=${this._entityId}
|
.value=${this._entityId}
|
||||||
@ -512,6 +546,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
this._unit_of_measurement = ev.target.value;
|
this._unit_of_measurement = ev.target.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _switchAsChanged(ev): void {
|
||||||
|
if (ev.target.value === "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._switchAs = ev.target.value;
|
||||||
|
}
|
||||||
|
|
||||||
private _areaPicked(ev: CustomEvent) {
|
private _areaPicked(ev: CustomEvent) {
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
this._areaId = ev.detail.value;
|
this._areaId = ev.detail.value;
|
||||||
@ -545,6 +586,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
private async _updateEntry(): Promise<void> {
|
private async _updateEntry(): Promise<void> {
|
||||||
this._submitting = true;
|
this._submitting = true;
|
||||||
|
|
||||||
|
const parent = (this.getRootNode() as ShadowRoot).host as HTMLElement;
|
||||||
|
|
||||||
const params: Partial<EntityRegistryEntryUpdateParams> = {
|
const params: Partial<EntityRegistryEntryUpdateParams> = {
|
||||||
name: this._name.trim() || null,
|
name: this._name.trim() || null,
|
||||||
icon: this._icon.trim() || null,
|
icon: this._icon.trim() || null,
|
||||||
@ -604,6 +648,46 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
} finally {
|
} finally {
|
||||||
this._submitting = false;
|
this._submitting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._switchAs !== "switch") {
|
||||||
|
if (
|
||||||
|
!(await showConfirmationDialog(this, {
|
||||||
|
text: this.hass!.localize(
|
||||||
|
"ui.dialogs.entity_registry.editor.switch_as_x_confirm",
|
||||||
|
"domain",
|
||||||
|
this._switchAs
|
||||||
|
),
|
||||||
|
}))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const configFlow = await createConfigFlow(this.hass, "switch_as_x");
|
||||||
|
const result = (await handleConfigFlowStep(
|
||||||
|
this.hass,
|
||||||
|
configFlow.flow_id,
|
||||||
|
{
|
||||||
|
entity_id: this._entityId.trim(),
|
||||||
|
target_domain: this._switchAs,
|
||||||
|
}
|
||||||
|
)) as DataEntryFlowStepCreateEntry;
|
||||||
|
if (!result.result?.entry_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const unsub = await this.hass.connection.subscribeEvents(() => {
|
||||||
|
unsub();
|
||||||
|
fetchEntityRegistry(this.hass.connection).then((entityRegistry) => {
|
||||||
|
const entity = entityRegistry.find(
|
||||||
|
(reg) => reg.config_entry_id === result.result!.entry_id
|
||||||
|
);
|
||||||
|
if (!entity) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
showEntityEditorDialog(parent, {
|
||||||
|
entity_id: entity.entity_id,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, "entity_registry_updated");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _confirmDeleteEntry(): Promise<void> {
|
private async _confirmDeleteEntry(): Promise<void> {
|
||||||
|
@ -842,6 +842,7 @@
|
|||||||
"hidden_cause": "Hidden by {cause}.",
|
"hidden_cause": "Hidden by {cause}.",
|
||||||
"device_disabled": "The device of this entity is disabled.",
|
"device_disabled": "The device of this entity is disabled.",
|
||||||
"open_device_settings": "Open device settings",
|
"open_device_settings": "Open device settings",
|
||||||
|
"switch_as_x_confirm": "This switch will be hidden and a new {domain} will be added. Your existing configurations using the switch will continue to work.",
|
||||||
"enabled_description": "Disabled entities will not be added to Home Assistant.",
|
"enabled_description": "Disabled entities will not be added to Home Assistant.",
|
||||||
"enabled_delay_confirm": "The enabled entities will be added to Home Assistant in {delay} seconds",
|
"enabled_delay_confirm": "The enabled entities will be added to Home Assistant in {delay} seconds",
|
||||||
"enabled_restart_confirm": "Restart Home Assistant to finish enabling the entities",
|
"enabled_restart_confirm": "Restart Home Assistant to finish enabling the entities",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user