From 27ce63bb0c8321df85a958a1b51fa950925c54e5 Mon Sep 17 00:00:00 2001 From: Lukas Barth Date: Wed, 25 Oct 2017 08:17:26 +0200 Subject: [PATCH] WIP: add an input_datetime (#418) * Initial work on input_datetime * Properly format date and time * Linting * Use dom-if * Converting to Polymer 2 class * Some linting * Cache domain, linting * More linting * Use on-value-changed instead of value observer. Also, fix off-by-one b/c of 0-indexed months. Thanks, JavaScript. --- bower.json | 1 + src/more-infos/more-info-content.html | 1 + src/more-infos/more-info-input_datetime.html | 168 +++++++++++++++++++ src/util/hass-util.html | 29 +++- 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 src/more-infos/more-info-input_datetime.html diff --git a/bower.json b/bower.json index 3eec549893..ac76883bce 100644 --- a/bower.json +++ b/bower.json @@ -46,6 +46,7 @@ "paper-spinner": "PolymerElements/paper-spinner#^2.0.0", "paper-styles": "PolymerElements/paper-styles#^2.0.0", "paper-tabs": "PolymerElements/paper-tabs#^2.0.0", + "paper-time-input": "ryanburns23/paper-time-input#^2.0.4", "paper-toast": "PolymerElements/paper-toast#^2.0.0", "paper-toggle-button": "PolymerElements/paper-toggle-button#^2.0.0", "polymer": "^2.1.1", diff --git a/src/more-infos/more-info-content.html b/src/more-infos/more-info-content.html index 781bc633de..1a95bc542f 100644 --- a/src/more-infos/more-info-content.html +++ b/src/more-infos/more-info-content.html @@ -17,6 +17,7 @@ + diff --git a/src/util/hass-util.html b/src/util/hass-util.html index 1af801ec1e..be5c8bf225 100644 --- a/src/util/hass-util.html +++ b/src/util/hass-util.html @@ -26,7 +26,7 @@ window.hassUtil.DOMAINS_WITH_CARD = [ window.hassUtil.DOMAINS_WITH_MORE_INFO = [ 'alarm_control_panel', 'automation', 'camera', 'climate', 'configurator', 'cover', 'fan', 'group', 'history_graph', 'light', 'lock', 'media_player', 'script', - 'sun', 'updater', 'vacuum', + 'sun', 'updater', 'vacuum', 'input_datetime', ]; window.hassUtil.DOMAINS_WITH_NO_HISTORY = ['camera', 'configurator', 'history_graph', 'scene']; @@ -467,11 +467,12 @@ window.hassUtil.sortByName = function (entityA, entityB) { window.hassUtil.computeStateState = function (stateObj) { if (!stateObj._stateDisplay) { stateObj._stateDisplay = stateObj.state.replace(/_/g, ' '); + const domain = window.hassUtil.computeDomain(stateObj); if (stateObj.attributes.unit_of_measurement) { stateObj._stateDisplay += ' ' + stateObj.attributes.unit_of_measurement; } - if (window.hassUtil.computeDomain(stateObj) === 'binary_sensor') { + if (domain === 'binary_sensor') { switch (stateObj.attributes.device_class) { case 'moisture': stateObj._stateDisplay = (stateObj._stateDisplay === 'off') ? 'dry' : 'wet'; @@ -498,6 +499,30 @@ window.hassUtil.computeStateState = function (stateObj) { case 'power': default: } + } else if (domain === 'input_datetime') { + let date; + if (!stateObj.attributes.has_time) { + date = new Date( + stateObj.attributes.year, + stateObj.attributes.month - 1, + stateObj.attributes.day + ); + stateObj._stateDisplay = window.hassUtil.formatDate(date); + } else if (!stateObj.attributes.has_date) { + date = new Date( + 1970, 0, 1, + stateObj.attributes.hour, + stateObj.attributes.minute + ); + stateObj._stateDisplay = window.hassUtil.formatTime(date); + } else { + date = new Date( + stateObj.attributes.year, stateObj.attributes.month - 1, + stateObj.attributes.day, stateObj.attributes.hour, + stateObj.attributes.minute + ); + stateObj._stateDisplay = window.hassUtil.formatDateTime(date); + } } }