-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frontend support for
date
platform (#14389)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
- Loading branch information
1 parent
59db31c
commit ecb2a73
Showing
5 changed files
with
91 additions
and
1 deletion.
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
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
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,14 @@ | ||
import { HassEntityBase } from "home-assistant-js-websocket"; | ||
import { HomeAssistant } from "../types"; | ||
|
||
export const stateToIsoDateString = (entityState: HassEntityBase) => | ||
`${entityState}T00:00:00`; | ||
|
||
export const setDateValue = ( | ||
hass: HomeAssistant, | ||
entityId: string, | ||
date: string | undefined = undefined | ||
) => { | ||
const param = { entity_id: entityId, date }; | ||
hass.callService(entityId.split(".", 1)[0], "set_value", param); | ||
}; |
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
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,72 @@ | ||
import { html, LitElement, nothing, PropertyValues, TemplateResult } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import "../../../components/ha-date-input"; | ||
import { isUnavailableState } from "../../../data/entity"; | ||
import { setDateValue, stateToIsoDateString } from "../../../data/date"; | ||
import type { HomeAssistant } from "../../../types"; | ||
import { hasConfigOrEntityChanged } from "../common/has-changed"; | ||
import "../components/hui-generic-entity-row"; | ||
import { createEntityNotFoundWarning } from "../components/hui-warning"; | ||
import type { EntityConfig, LovelaceRow } from "./types"; | ||
|
||
@customElement("hui-date-entity-row") | ||
class HuiDateEntityRow extends LitElement implements LovelaceRow { | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@state() private _config?: EntityConfig; | ||
|
||
public setConfig(config: EntityConfig): void { | ||
if (!config) { | ||
throw new Error("Invalid configuration"); | ||
} | ||
this._config = config; | ||
} | ||
|
||
protected shouldUpdate(changedProps: PropertyValues): boolean { | ||
return hasConfigOrEntityChanged(this, changedProps); | ||
} | ||
|
||
protected render(): TemplateResult | typeof nothing { | ||
if (!this._config || !this.hass) { | ||
return nothing; | ||
} | ||
|
||
const stateObj = this.hass.states[this._config.entity]; | ||
|
||
if (!stateObj) { | ||
return html` | ||
<hui-warning> | ||
${createEntityNotFoundWarning(this.hass, this._config.entity)} | ||
</hui-warning> | ||
`; | ||
} | ||
|
||
return html` | ||
<hui-generic-entity-row | ||
.hass=${this.hass} | ||
.config=${this._config} | ||
hideName="true" | ||
> | ||
<ha-date-input | ||
.locale=${this.hass.locale} | ||
.disabled=${isUnavailableState(stateObj.state)} | ||
.value=${stateToIsoDateString(stateObj)} | ||
@value-changed=${this._dateChanged} | ||
> | ||
</ha-date-input> | ||
</hui-generic-entity-row> | ||
`; | ||
} | ||
|
||
private _dateChanged(ev: CustomEvent<{ value: string }>): void { | ||
const stateObj = this.hass!.states[this._config!.entity]; | ||
|
||
setDateValue(this.hass!, stateObj.entity_id, ev.detail.value); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-date-entity-row": HuiDateEntityRow; | ||
} | ||
} |