mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Use 1 element for all group previews (#17693)
This commit is contained in:
parent
b4975344a1
commit
fc1782e676
@ -17,6 +17,11 @@ export interface GroupEntity extends HassEntityBase {
|
|||||||
attributes: GroupEntityAttributes;
|
attributes: GroupEntityAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GroupPreview {
|
||||||
|
state: string;
|
||||||
|
attributes: Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
export const computeGroupDomain = (
|
export const computeGroupDomain = (
|
||||||
stateObj: GroupEntity
|
stateObj: GroupEntity
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
@ -27,35 +32,15 @@ export const computeGroupDomain = (
|
|||||||
return uniqueDomains.length === 1 ? uniqueDomains[0] : undefined;
|
return uniqueDomains.length === 1 ? uniqueDomains[0] : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const subscribePreviewGroupSensor = (
|
export const subscribePreviewGroup = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
flow_id: string,
|
flow_id: string,
|
||||||
flow_type: "config_flow" | "options_flow",
|
flow_type: "config_flow" | "options_flow",
|
||||||
user_input: Record<string, any>,
|
user_input: Record<string, any>,
|
||||||
callback: (preview: {
|
callback: (preview: GroupPreview) => void
|
||||||
state: string;
|
|
||||||
attributes: Record<string, any>;
|
|
||||||
}) => void
|
|
||||||
): Promise<UnsubscribeFunc> =>
|
): Promise<UnsubscribeFunc> =>
|
||||||
hass.connection.subscribeMessage(callback, {
|
hass.connection.subscribeMessage(callback, {
|
||||||
type: "group/sensor/start_preview",
|
type: "group/start_preview",
|
||||||
flow_id,
|
|
||||||
flow_type,
|
|
||||||
user_input,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const subscribePreviewGroupBinarySensor = (
|
|
||||||
hass: HomeAssistant,
|
|
||||||
flow_id: string,
|
|
||||||
flow_type: "config_flow" | "options_flow",
|
|
||||||
user_input: Record<string, any>,
|
|
||||||
callback: (preview: {
|
|
||||||
state: string;
|
|
||||||
attributes: Record<string, any>;
|
|
||||||
}) => void
|
|
||||||
): Promise<UnsubscribeFunc> =>
|
|
||||||
hass.connection.subscribeMessage(callback, {
|
|
||||||
type: "group/binary_sensor/start_preview",
|
|
||||||
flow_id,
|
flow_id,
|
||||||
flow_type,
|
flow_type,
|
||||||
user_input,
|
user_input,
|
||||||
|
@ -2,19 +2,20 @@ import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
|
|||||||
import { LitElement, html } from "lit";
|
import { LitElement, html } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { FlowType } from "../../../data/data_entry_flow";
|
import { FlowType } from "../../../data/data_entry_flow";
|
||||||
import { subscribePreviewGroupBinarySensor } from "../../../data/group";
|
import { GroupPreview, subscribePreviewGroup } from "../../../data/group";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import "./entity-preview-row";
|
import "./entity-preview-row";
|
||||||
|
import { debounce } from "../../../common/util/debounce";
|
||||||
|
|
||||||
@customElement("flow-preview-group_binary_sensor")
|
@customElement("flow-preview-group")
|
||||||
class FlowPreviewGroupBinarySensor extends LitElement {
|
class FlowPreviewGroup extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public flowType!: FlowType;
|
@property() public flowType!: FlowType;
|
||||||
|
|
||||||
public handler!: string;
|
public handler!: string;
|
||||||
|
|
||||||
public stepId!: string;
|
@property() public stepId!: string;
|
||||||
|
|
||||||
@property() public flowId!: string;
|
@property() public flowId!: string;
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ class FlowPreviewGroupBinarySensor extends LitElement {
|
|||||||
|
|
||||||
willUpdate(changedProps) {
|
willUpdate(changedProps) {
|
||||||
if (changedProps.has("stepData")) {
|
if (changedProps.has("stepData")) {
|
||||||
this._subscribePreview();
|
this._debouncedSubscribePreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,13 +46,10 @@ class FlowPreviewGroupBinarySensor extends LitElement {
|
|||||||
></entity-preview-row>`;
|
></entity-preview-row>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _setPreview = (preview: {
|
private _setPreview = (preview: GroupPreview) => {
|
||||||
state: string;
|
|
||||||
attributes: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
this._preview = {
|
this._preview = {
|
||||||
entity_id: "binary_sensor.flow_preview",
|
entity_id: `${this.stepId}.flow_preview`,
|
||||||
last_changed: now,
|
last_changed: now,
|
||||||
last_updated: now,
|
last_updated: now,
|
||||||
context: { id: "", parent_id: null, user_id: null },
|
context: { id: "", parent_id: null, user_id: null },
|
||||||
@ -59,6 +57,10 @@ class FlowPreviewGroupBinarySensor extends LitElement {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private _debouncedSubscribePreview = debounce(() => {
|
||||||
|
this._subscribePreview();
|
||||||
|
}, 250);
|
||||||
|
|
||||||
private async _subscribePreview() {
|
private async _subscribePreview() {
|
||||||
if (this._unsub) {
|
if (this._unsub) {
|
||||||
(await this._unsub)();
|
(await this._unsub)();
|
||||||
@ -68,7 +70,7 @@ class FlowPreviewGroupBinarySensor extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this._unsub = subscribePreviewGroupBinarySensor(
|
this._unsub = subscribePreviewGroup(
|
||||||
this.hass,
|
this.hass,
|
||||||
this.flowId,
|
this.flowId,
|
||||||
this.flowType,
|
this.flowType,
|
||||||
@ -83,6 +85,6 @@ class FlowPreviewGroupBinarySensor extends LitElement {
|
|||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HTMLElementTagNameMap {
|
interface HTMLElementTagNameMap {
|
||||||
"flow-preview-group_binary_sensor": FlowPreviewGroupBinarySensor;
|
"flow-preview-group": FlowPreviewGroup;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,92 +0,0 @@
|
|||||||
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
|
|
||||||
import { LitElement, html } from "lit";
|
|
||||||
import { customElement, property, state } from "lit/decorators";
|
|
||||||
import { FlowType } from "../../../data/data_entry_flow";
|
|
||||||
import { subscribePreviewGroupSensor } from "../../../data/group";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
|
||||||
import "./entity-preview-row";
|
|
||||||
|
|
||||||
@customElement("flow-preview-group_sensor")
|
|
||||||
class FlowPreviewGroupSensor extends LitElement {
|
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
|
||||||
|
|
||||||
@property() public flowType!: FlowType;
|
|
||||||
|
|
||||||
public handler!: string;
|
|
||||||
|
|
||||||
public stepId!: string;
|
|
||||||
|
|
||||||
@property() public flowId!: string;
|
|
||||||
|
|
||||||
@property() public stepData!: Record<string, any>;
|
|
||||||
|
|
||||||
@state() private _preview?: HassEntity;
|
|
||||||
|
|
||||||
private _unsub?: Promise<UnsubscribeFunc>;
|
|
||||||
|
|
||||||
disconnectedCallback(): void {
|
|
||||||
super.disconnectedCallback();
|
|
||||||
if (this._unsub) {
|
|
||||||
this._unsub.then((unsub) => unsub());
|
|
||||||
this._unsub = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
willUpdate(changedProps) {
|
|
||||||
if (changedProps.has("stepData")) {
|
|
||||||
this._subscribePreview();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render() {
|
|
||||||
return html`<entity-preview-row
|
|
||||||
.hass=${this.hass}
|
|
||||||
.stateObj=${this._preview}
|
|
||||||
></entity-preview-row>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
private _setPreview = (preview: {
|
|
||||||
state: string;
|
|
||||||
attributes: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const now = new Date().toISOString();
|
|
||||||
this._preview = {
|
|
||||||
entity_id: "sensor.flow_preview",
|
|
||||||
last_changed: now,
|
|
||||||
last_updated: now,
|
|
||||||
context: { id: "", parent_id: null, user_id: null },
|
|
||||||
...preview,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
private async _subscribePreview() {
|
|
||||||
if (this._unsub) {
|
|
||||||
(await this._unsub)();
|
|
||||||
this._unsub = undefined;
|
|
||||||
}
|
|
||||||
if (this.flowType === "repair_flow") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!this.stepData.type) {
|
|
||||||
this._preview = undefined;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this._unsub = subscribePreviewGroupSensor(
|
|
||||||
this.hass,
|
|
||||||
this.flowId,
|
|
||||||
this.flowType,
|
|
||||||
this.stepData,
|
|
||||||
this._setPreview
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
this._preview = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
interface HTMLElementTagNameMap {
|
|
||||||
"flow-preview-group_sensor": FlowPreviewGroupSensor;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user