Skip to content

Commit

Permalink
Handle proper state display of datetime entities (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
benct committed Mar 29, 2021
1 parent 9838e33 commit 6b596f4
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/entity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computeEntity, computeStateDomain } from 'custom-card-helpers';
import { computeEntity, computeStateDomain, formatDate, formatDateTime, formatTime } from 'custom-card-helpers';
import { isObject, isUnavailable } from './util';

export const checkEntity = (config) => {
Expand Down Expand Up @@ -46,6 +46,35 @@ export const entityStateDisplay = (hass, stateObj, config) => {
}

const domain = computeStateDomain(stateObj);

if (domain === 'input_datetime') {
let date;
if (!stateObj.attributes.has_time) {
date = new Date(stateObj.attributes.year, stateObj.attributes.month - 1, stateObj.attributes.day);
return formatDate(date, hass.language);
}
if (!stateObj.attributes.has_date) {
const now = new Date();
date = new Date(
now.getFullYear(),
now.getMonth(),
now.getDay(),
stateObj.attributes.hour,
stateObj.attributes.minute
);
return formatTime(date, hass.language);
}

date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day,
stateObj.attributes.hour,
stateObj.attributes.minute
);
return formatDateTime(date, hass.language);
}

return (
(stateObj.attributes.device_class &&
hass.localize(`component.${domain}.state.${stateObj.attributes.device_class}.${stateObj.state}`)) ||
Expand Down

0 comments on commit 6b596f4

Please sign in to comment.