mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add frontend support for datetime
platform (#14391)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
38cf774a0d
commit
3df7c50690
@ -211,6 +211,7 @@ export const DOMAINS_INPUT_ROW = [
|
|||||||
"button",
|
"button",
|
||||||
"cover",
|
"cover",
|
||||||
"date",
|
"date",
|
||||||
|
"datetime",
|
||||||
"fan",
|
"fan",
|
||||||
"group",
|
"group",
|
||||||
"humidifier",
|
"humidifier",
|
||||||
|
@ -117,59 +117,39 @@ export const computeStateDisplayFromEntityAttributes = (
|
|||||||
|
|
||||||
const domain = computeDomain(entityId);
|
const domain = computeDomain(entityId);
|
||||||
|
|
||||||
|
if (domain === "datetime") {
|
||||||
|
const time = new Date(state);
|
||||||
|
return formatDateTime(time, locale);
|
||||||
|
}
|
||||||
|
|
||||||
if (["date", "input_datetime", "time"].includes(domain)) {
|
if (["date", "input_datetime", "time"].includes(domain)) {
|
||||||
if (state !== undefined) {
|
// If trying to display an explicit state, need to parse the explicit state to `Date` then format.
|
||||||
// If trying to display an explicit state, need to parse the explicit state to `Date` then format.
|
// Attributes aren't available, we have to use `state`.
|
||||||
// Attributes aren't available, we have to use `state`.
|
try {
|
||||||
try {
|
const components = state.split(" ");
|
||||||
const components = state.split(" ");
|
if (components.length === 2) {
|
||||||
if (components.length === 2) {
|
// Date and time.
|
||||||
// Date and time.
|
return formatDateTime(new Date(components.join("T")), locale);
|
||||||
return formatDateTime(new Date(components.join("T")), locale);
|
}
|
||||||
|
if (components.length === 1) {
|
||||||
|
if (state.includes("-")) {
|
||||||
|
// Date only.
|
||||||
|
return formatDate(new Date(`${state}T00:00`), locale);
|
||||||
}
|
}
|
||||||
if (components.length === 1) {
|
if (state.includes(":")) {
|
||||||
if (state.includes("-")) {
|
// Time only.
|
||||||
// Date only.
|
const now = new Date();
|
||||||
return formatDate(new Date(`${state}T00:00`), locale);
|
return formatTime(
|
||||||
}
|
new Date(`${now.toISOString().split("T")[0]}T${state}`),
|
||||||
if (state.includes(":")) {
|
locale
|
||||||
// Time only.
|
);
|
||||||
const now = new Date();
|
|
||||||
return formatTime(
|
|
||||||
new Date(`${now.toISOString().split("T")[0]}T${state}`),
|
|
||||||
locale
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return state;
|
|
||||||
} catch (_e) {
|
|
||||||
// Formatting methods may throw error if date parsing doesn't go well,
|
|
||||||
// just return the state string in that case.
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If not trying to display an explicit state, create `Date` object from `stateObj`'s attributes then format.
|
|
||||||
let date: Date;
|
|
||||||
if (attributes.has_date && attributes.has_time) {
|
|
||||||
date = new Date(
|
|
||||||
attributes.year,
|
|
||||||
attributes.month - 1,
|
|
||||||
attributes.day,
|
|
||||||
attributes.hour,
|
|
||||||
attributes.minute
|
|
||||||
);
|
|
||||||
return formatDateTime(date, locale);
|
|
||||||
}
|
|
||||||
if (attributes.has_date) {
|
|
||||||
date = new Date(attributes.year, attributes.month - 1, attributes.day);
|
|
||||||
return formatDate(date, locale);
|
|
||||||
}
|
|
||||||
if (attributes.has_time) {
|
|
||||||
date = new Date();
|
|
||||||
date.setHours(attributes.hour, attributes.minute);
|
|
||||||
return formatTime(date, locale);
|
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
|
} catch (_e) {
|
||||||
|
// Formatting methods may throw error if date parsing doesn't go well,
|
||||||
|
// just return the state string in that case.
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,5 +10,5 @@ export const setDateValue = (
|
|||||||
date: string | undefined = undefined
|
date: string | undefined = undefined
|
||||||
) => {
|
) => {
|
||||||
const param = { entity_id: entityId, date };
|
const param = { entity_id: entityId, date };
|
||||||
hass.callService(entityId.split(".", 1)[0], "set_value", param);
|
hass.callService("date", "set_value", param);
|
||||||
};
|
};
|
||||||
|
12
src/data/datetime.ts
Normal file
12
src/data/datetime.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
|
export const setDateTimeValue = (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entityId: string,
|
||||||
|
datetime: Date
|
||||||
|
) => {
|
||||||
|
hass.callService("datetime", "set_value", {
|
||||||
|
entity_id: entityId,
|
||||||
|
datetime: datetime.toISOString(),
|
||||||
|
});
|
||||||
|
};
|
@ -38,7 +38,7 @@ export const setInputDateTimeValue = (
|
|||||||
date: string | undefined = undefined
|
date: string | undefined = undefined
|
||||||
) => {
|
) => {
|
||||||
const param = { entity_id: entityId, time, date };
|
const param = { entity_id: entityId, time, date };
|
||||||
hass.callService(entityId.split(".", 1)[0], "set_datetime", param);
|
hass.callService("input_datetime", "set_datetime", param);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const fetchInputDateTime = (hass: HomeAssistant) =>
|
export const fetchInputDateTime = (hass: HomeAssistant) =>
|
||||||
|
@ -6,5 +6,5 @@ export const setTimeValue = (
|
|||||||
time: string | undefined = undefined
|
time: string | undefined = undefined
|
||||||
) => {
|
) => {
|
||||||
const param = { entity_id: entityId, time: time };
|
const param = { entity_id: entityId, time: time };
|
||||||
hass.callService(entityId.split(".", 1)[0], "set_value", param);
|
hass.callService("time", "set_value", param);
|
||||||
};
|
};
|
||||||
|
@ -28,6 +28,7 @@ const LAZY_LOAD_TYPES = {
|
|||||||
"climate-entity": () => import("../entity-rows/hui-climate-entity-row"),
|
"climate-entity": () => import("../entity-rows/hui-climate-entity-row"),
|
||||||
"cover-entity": () => import("../entity-rows/hui-cover-entity-row"),
|
"cover-entity": () => import("../entity-rows/hui-cover-entity-row"),
|
||||||
"date-entity": () => import("../entity-rows/hui-date-entity-row"),
|
"date-entity": () => import("../entity-rows/hui-date-entity-row"),
|
||||||
|
"datetime-entity": () => import("../entity-rows/hui-datetime-entity-row"),
|
||||||
"group-entity": () => import("../entity-rows/hui-group-entity-row"),
|
"group-entity": () => import("../entity-rows/hui-group-entity-row"),
|
||||||
"input-button-entity": () =>
|
"input-button-entity": () =>
|
||||||
import("../entity-rows/hui-input-button-entity-row"),
|
import("../entity-rows/hui-input-button-entity-row"),
|
||||||
@ -63,6 +64,7 @@ const DOMAIN_TO_ELEMENT_TYPE = {
|
|||||||
climate: "climate",
|
climate: "climate",
|
||||||
cover: "cover",
|
cover: "cover",
|
||||||
date: "date",
|
date: "date",
|
||||||
|
datetime: "datetime",
|
||||||
fan: "toggle",
|
fan: "toggle",
|
||||||
group: "group",
|
group: "group",
|
||||||
humidifier: "humidifier",
|
humidifier: "humidifier",
|
||||||
|
120
src/panels/lovelace/entity-rows/hui-datetime-entity-row.ts
Normal file
120
src/panels/lovelace/entity-rows/hui-datetime-entity-row.ts
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
import {
|
||||||
|
css,
|
||||||
|
CSSResultGroup,
|
||||||
|
html,
|
||||||
|
LitElement,
|
||||||
|
nothing,
|
||||||
|
PropertyValues,
|
||||||
|
TemplateResult,
|
||||||
|
} from "lit";
|
||||||
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import "../../../components/ha-date-input";
|
||||||
|
import { format } from "date-fns";
|
||||||
|
import { isUnavailableState } from "../../../data/entity";
|
||||||
|
import { setDateTimeValue } from "../../../data/datetime";
|
||||||
|
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";
|
||||||
|
import "../../../components/ha-time-input";
|
||||||
|
|
||||||
|
@customElement("hui-datetime-entity-row")
|
||||||
|
class HuiInputDatetimeEntityRow 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>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dateObj = new Date(stateObj.state);
|
||||||
|
const time = format(dateObj, "HH:mm:ss");
|
||||||
|
const date = format(dateObj, "yyyy-MM-dd");
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<hui-generic-entity-row
|
||||||
|
.hass=${this.hass}
|
||||||
|
.config=${this._config}
|
||||||
|
hideName="true"
|
||||||
|
>
|
||||||
|
<ha-date-input
|
||||||
|
.locale=${this.hass.locale}
|
||||||
|
.value=${date}
|
||||||
|
.disabled=${isUnavailableState(stateObj.state)}
|
||||||
|
@value-changed=${this._dateChanged}
|
||||||
|
>
|
||||||
|
</ha-date-input>
|
||||||
|
<ha-time-input
|
||||||
|
.value=${time}
|
||||||
|
.disabled=${isUnavailableState(stateObj.state)}
|
||||||
|
.locale=${this.hass.locale}
|
||||||
|
@value-changed=${this._timeChanged}
|
||||||
|
@click=${this._stopEventPropagation}
|
||||||
|
></ha-time-input>
|
||||||
|
</hui-generic-entity-row>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _stopEventPropagation(ev: Event): void {
|
||||||
|
ev.stopPropagation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _timeChanged(ev: CustomEvent<{ value: string }>): void {
|
||||||
|
const stateObj = this.hass!.states[this._config!.entity];
|
||||||
|
const dateObj = new Date(stateObj.state);
|
||||||
|
const newTime = ev.detail.value.split(":").map(Number);
|
||||||
|
dateObj.setHours(newTime[0], newTime[1], newTime[2]);
|
||||||
|
|
||||||
|
setDateTimeValue(this.hass!, stateObj.entity_id, dateObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _dateChanged(ev: CustomEvent<{ value: string }>): void {
|
||||||
|
const stateObj = this.hass!.states[this._config!.entity];
|
||||||
|
const dateObj = new Date(stateObj.state);
|
||||||
|
const newDate = ev.detail.value.split("-").map(Number);
|
||||||
|
dateObj.setFullYear(newDate[0], newDate[1] - 1, newDate[2]);
|
||||||
|
|
||||||
|
setDateTimeValue(this.hass!, stateObj.entity_id, dateObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get styles(): CSSResultGroup {
|
||||||
|
return css`
|
||||||
|
ha-date-input + ha-time-input {
|
||||||
|
margin-left: 4px;
|
||||||
|
margin-inline-start: 4px;
|
||||||
|
margin-inline-end: initial;
|
||||||
|
direction: var(--direction);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"hui-datetime-entity-row": HuiInputDatetimeEntityRow;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user