forked from home-assistant/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preview support to binary sensor group (home-assistant#17682)
- Loading branch information
1 parent
5ce31f3
commit ed92958
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/dialogs/config-flow/previews/flow-preview-group_binary_sensor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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 { subscribePreviewGroupBinarySensor } from "../../../data/group"; | ||
import { HomeAssistant } from "../../../types"; | ||
import "./entity-preview-row"; | ||
|
||
@customElement("flow-preview-group_binary_sensor") | ||
class FlowPreviewGroupBinarySensor 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: "binary_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; | ||
} | ||
try { | ||
this._unsub = subscribePreviewGroupBinarySensor( | ||
this.hass, | ||
this.flowId, | ||
this.flowType, | ||
this.stepData, | ||
this._setPreview | ||
); | ||
} catch (err) { | ||
this._preview = undefined; | ||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"flow-preview-group_binary_sensor": FlowPreviewGroupBinarySensor; | ||
} | ||
} |