Skip to content

Commit 6b596f4

Browse files
committed
Handle proper state display of datetime entities (#140)
1 parent 9838e33 commit 6b596f4

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/entity.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computeEntity, computeStateDomain } from 'custom-card-helpers';
1+
import { computeEntity, computeStateDomain, formatDate, formatDateTime, formatTime } from 'custom-card-helpers';
22
import { isObject, isUnavailable } from './util';
33

44
export const checkEntity = (config) => {
@@ -46,6 +46,35 @@ export const entityStateDisplay = (hass, stateObj, config) => {
4646
}
4747

4848
const domain = computeStateDomain(stateObj);
49+
50+
if (domain === 'input_datetime') {
51+
let date;
52+
if (!stateObj.attributes.has_time) {
53+
date = new Date(stateObj.attributes.year, stateObj.attributes.month - 1, stateObj.attributes.day);
54+
return formatDate(date, hass.language);
55+
}
56+
if (!stateObj.attributes.has_date) {
57+
const now = new Date();
58+
date = new Date(
59+
now.getFullYear(),
60+
now.getMonth(),
61+
now.getDay(),
62+
stateObj.attributes.hour,
63+
stateObj.attributes.minute
64+
);
65+
return formatTime(date, hass.language);
66+
}
67+
68+
date = new Date(
69+
stateObj.attributes.year,
70+
stateObj.attributes.month - 1,
71+
stateObj.attributes.day,
72+
stateObj.attributes.hour,
73+
stateObj.attributes.minute
74+
);
75+
return formatDateTime(date, hass.language);
76+
}
77+
4978
return (
5079
(stateObj.attributes.device_class &&
5180
hass.localize(`component.${domain}.state.${stateObj.attributes.device_class}.${stateObj.state}`)) ||

0 commit comments

Comments
 (0)