From 32761d762a5d99e967606a2b5a0ad024c5804f15 Mon Sep 17 00:00:00 2001 From: SublimePotato Date: Fri, 6 Dec 2024 17:59:26 +0100 Subject: [PATCH] refactor: lowered battery warning threshold --- dist/better-thermostat-ui-card.js | 2 +- src/better-thermostat-ui.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dist/better-thermostat-ui-card.js b/dist/better-thermostat-ui-card.js index 27dcbd3..c169011 100644 --- a/dist/better-thermostat-ui-card.js +++ b/dist/better-thermostat-ui-card.js @@ -552,7 +552,7 @@ const lo=270;const co=new Set(["ArrowRight","ArrowUp","ArrowLeft","ArrowDown","P ${W} ${q} - `}}connectedCallback(){super.connectedCallback()}disconnectedCallback(){super.disconnectedCallback()}static async getConfigElement(){return await Promise.resolve().then((function(){return ko})),document.createElement("better-thermostat-ui-card-editor")}static async getStubConfig(t){const e=Object.keys(t.states).filter((t=>["climate"].includes(t.split(".")[0]))),i=e.filter((e=>{var i;return null===(i=t.states[e].attributes)||void 0===i?void 0:i.call_for_heat}));return{type:"custom:better-thermostat-ui-card",entity:i[0]||e[0]}}_highChanged(t){const e=t.detail.value;if(isNaN(e))return;const i=t.type.replace("-changed","");this.value=Object.assign(Object.assign({},this.value),{[i]:e}),this._selectTargetTemperature=i,this._callService(i)}_highChanging(t){const e=t.detail.value;if(isNaN(e))return;const i=t.type.replace("-changing","");this.value=Object.assign(Object.assign({},this.value),{[i]:e}),this._selectTargetTemperature=i,this._updateDisplay(),this._vibrate(20)}_callService(t){"high"!==t&&"low"!==t?this.hass.callService("climate","set_temperature",{entity_id:this.stateObj.entity_id,temperature:this.value.value}):this.hass.callService("climate","set_temperature",{entity_id:this.stateObj.entity_id,target_temp_low:this.value.low,target_temp_high:this.value.high})}_handleButton(t){var e;const i=t.currentTarget.target,n=t.currentTarget.step,r="high"===i?this.max:this.min;let o=null!==(e=this.value[i])&&void 0!==e?e:r;o+=n,o=Mi(o,this.min,this.max),"high"===i&&null!=this.value.low&&(o=Mi(o,this.value.low,this.max)),"low"===i&&null!=this.value.high&&(o=Mi(o,this.min,this.value.high)),this.value=Object.assign(Object.assign({},this.value),{[i]:o}),this._updateDisplay(),this._vibrate(40),this._debouncedCallService(i)}_handleSelectTemp(t){const e=t.currentTarget.target;this._selectTargetTemperature=e,this._updateDisplay(),this._vibrate(40)}setConfig(t){this._config=Object.assign({tap_action:{action:"toggle"},hold_action:{action:"more-info"}},t)}getCardSize(){return 1}_vibrate(t){try{navigator.vibrate(t)}catch(t){}}firstUpdated(t){this._init=!1}shouldUpdate(t){return void 0!==t.has("_config")&&void 0!==t.get("_config")&&(this._hasSummer=!1,this._hasWindow=!1,this.current_humidity=0),void 0!==t.get("hass")&&(this._init=!1),!0}updated(t){var e,i;super.updated(t),this._firstRender=!1,null===(i=null===(e=null==this?void 0:this.shadowRoot)||void 0===e?void 0:e.querySelector(".low_battery"))||void 0===i||i.addEventListener("click",(()=>{var t,e;null===(e=null===(t=null==this?void 0:this.shadowRoot)||void 0===t?void 0:t.querySelector(".low_battery"))||void 0===e||e.remove(),this._vibrate(2)}))}willUpdate(t){var e,i;if(!this.hass||!this._config||!t.has("hass")&&!t.has("_config"))return;const n=this._config.entity,r=this.hass.states[n];if(!r)return;const o=t.get("hass");if(!o||o.states[n]!==r){if(!this._config||!this.hass||!this._config.entity)return;this.stateObj=r;const t=this.stateObj.attributes,n=this.stateObj.state;this.mode=n||"off",t.hvac_modes&&(this.modes=Object.values(t.hvac_modes)),this.value={value:(null==t?void 0:t.temperature)||0,low:(null==t?void 0:t.target_temp_low)||null,high:(null==t?void 0:t.target_temp_high)||null},t.target_temp_step&&(this.step=t.target_temp_step),t.min_temp&&(this.min=t.min_temp),t.max_temp&&(this.max=t.max_temp),t.current_temperature&&(this.current=t.current_temperature);const o=null!==(e=t.current_humidity)&&void 0!==e?e:null==t?void 0:t.humidity;if(void 0!==o&&(this.current_humidity=parseFloat(o.toString())),void 0!==(null==t?void 0:t.window_open)&&(this._hasWindow=!0,this.window=t.window_open),void 0!==(null==t?void 0:t.call_for_heat)&&(this._hasSummer=!0,this.summer=!t.call_for_heat),void 0===(null==t?void 0:t.batteries)||(null===(i=null==this?void 0:this._config)||void 0===i?void 0:i.disable_battery_warning))this.lowBattery=void 0;else{const e=Object.entries(JSON.parse(t.batteries)).map((t=>({name:t[0],battery:"on"===t[1].battery?5:"off"===t[1].battery?100:parseFloat(t[1].battery)}))).filter((t=>t.battery<10));this.lowBattery=e[0]}if(void 0!==(null==t?void 0:t.errors)){const e=JSON.parse(t.errors);e.length>0?this.error=e[0]:this.error=[]}else this.error=[];this._updateDisplay()}}_updateDisplay(){var t;(null===(t=null==this?void 0:this._config)||void 0===t?void 0:t.set_current_as_main)?(this._display_bottom=this._getCurrentSetpoint(),this._display_top=this.current):(this._display_bottom=this.current,this._display_top=this._getCurrentSetpoint())}_handleAction(t){var e,i,n,r,o;if("eco"===t.currentTarget.mode){null===((null===(i=null===(e=null==this?void 0:this.stateObj)||void 0===e?void 0:e.attributes)||void 0===i?void 0:i.saved_temperature)||null)?this.hass.callService("better_thermostat","set_temp_target_temperature",{entity_id:this._config.entity,temperature:(null===(n=this._config)||void 0===n?void 0:n.eco_temperature)||18}):this.hass.callService("better_thermostat","restore_saved_target_temperature",{entity_id:this._config.entity})}else{null!==((null===(o=null===(r=null==this?void 0:this.stateObj)||void 0===r?void 0:r.attributes)||void 0===o?void 0:o.saved_temperature)||null)&&this.hass.callService("better_thermostat","restore_saved_target_temperature",{entity_id:this._config.entity}),this.hass.callService("climate","set_hvac_mode",{entity_id:this._config.entity,hvac_mode:t.currentTarget.mode})}}_setTemperature(){this.hass.callService("climate","set_temperature",{entity_id:this._config.entity,temperature:this.value})}_getCurrentSetpoint(){var t,e,i,n,r,o,a,s;return null!==(null===(t=null==this?void 0:this.value)||void 0===t?void 0:t.high)&&null!==(null===(e=null==this?void 0:this.value)||void 0===e?void 0:e.low)?((null===(i=null==this?void 0:this.value)||void 0===i?void 0:i.low)||0)>=this.current?(null===(n=null==this?void 0:this.value)||void 0===n?void 0:n.low)||0:((null===(r=null==this?void 0:this.value)||void 0===r?void 0:r.high)||0)<=this.current?(null===(o=null==this?void 0:this.value)||void 0===o?void 0:o.high)||0:(null===(a=null==this?void 0:this.value)||void 0===a?void 0:a.low)||0:(null===(s=null==this?void 0:this.value)||void 0===s?void 0:s.value)||0}_renderHVACAction(t=!1){var e,i,n,r,o;const a="heating"===(null===(e=this.stateObj)||void 0===e?void 0:e.attributes.hvac_action)&&"off"!==this.mode,s="cooling"===(null===(i=this.stateObj)||void 0===i?void 0:i.attributes.hvac_action)&&"off"!==this.mode,l=t?"translate(-3,-3.5) scale(0.25)":"translate(5,-4) scale(0.25)",c="#9d9d9d";if(void 0!==(null===(n=null==this?void 0:this.value)||void 0===n?void 0:n.high)&&null!==(null===(r=null==this?void 0:this.value)||void 0===r?void 0:r.high)&&(null===(o=null==this?void 0:this.value)||void 0===o?void 0:o.high)<=this.current){const t=xi(s?{hass:this.hass,string:"extra_states.cooling"}:{hass:this.hass,string:"extra_states.cooling_off"});return Z`${t}`}const u=xi(a?{hass:this.hass,string:"extra_states.heating"}:{hass:this.hass,string:"extra_states.heating_off"});return Z`${u}`}_renderHVACIcon(t){var e,i;return((null===(e=null==this?void 0:this.value)||void 0===e?void 0:e.low)||0)>=this.current?this._renderIcon("heat",t):((null===(i=null==this?void 0:this.value)||void 0===i?void 0:i.high)||0)<=this.current?this._renderIcon("cool",t):this._renderIcon("ok",t)}_renderIcon(t,e){if(!mo[t])return U``;const i=this.hass.localize(`component.climate.state._.${t}`)||xi({hass:this.hass,string:`extra_states.${t}`});return U` + `}}connectedCallback(){super.connectedCallback()}disconnectedCallback(){super.disconnectedCallback()}static async getConfigElement(){return await Promise.resolve().then((function(){return ko})),document.createElement("better-thermostat-ui-card-editor")}static async getStubConfig(t){const e=Object.keys(t.states).filter((t=>["climate"].includes(t.split(".")[0]))),i=e.filter((e=>{var i;return null===(i=t.states[e].attributes)||void 0===i?void 0:i.call_for_heat}));return{type:"custom:better-thermostat-ui-card",entity:i[0]||e[0]}}_highChanged(t){const e=t.detail.value;if(isNaN(e))return;const i=t.type.replace("-changed","");this.value=Object.assign(Object.assign({},this.value),{[i]:e}),this._selectTargetTemperature=i,this._callService(i)}_highChanging(t){const e=t.detail.value;if(isNaN(e))return;const i=t.type.replace("-changing","");this.value=Object.assign(Object.assign({},this.value),{[i]:e}),this._selectTargetTemperature=i,this._updateDisplay(),this._vibrate(20)}_callService(t){"high"!==t&&"low"!==t?this.hass.callService("climate","set_temperature",{entity_id:this.stateObj.entity_id,temperature:this.value.value}):this.hass.callService("climate","set_temperature",{entity_id:this.stateObj.entity_id,target_temp_low:this.value.low,target_temp_high:this.value.high})}_handleButton(t){var e;const i=t.currentTarget.target,n=t.currentTarget.step,r="high"===i?this.max:this.min;let o=null!==(e=this.value[i])&&void 0!==e?e:r;o+=n,o=Mi(o,this.min,this.max),"high"===i&&null!=this.value.low&&(o=Mi(o,this.value.low,this.max)),"low"===i&&null!=this.value.high&&(o=Mi(o,this.min,this.value.high)),this.value=Object.assign(Object.assign({},this.value),{[i]:o}),this._updateDisplay(),this._vibrate(40),this._debouncedCallService(i)}_handleSelectTemp(t){const e=t.currentTarget.target;this._selectTargetTemperature=e,this._updateDisplay(),this._vibrate(40)}setConfig(t){this._config=Object.assign({tap_action:{action:"toggle"},hold_action:{action:"more-info"}},t)}getCardSize(){return 1}_vibrate(t){try{navigator.vibrate(t)}catch(t){}}firstUpdated(t){this._init=!1}shouldUpdate(t){return void 0!==t.has("_config")&&void 0!==t.get("_config")&&(this._hasSummer=!1,this._hasWindow=!1,this.current_humidity=0),void 0!==t.get("hass")&&(this._init=!1),!0}updated(t){var e,i;super.updated(t),this._firstRender=!1,null===(i=null===(e=null==this?void 0:this.shadowRoot)||void 0===e?void 0:e.querySelector(".low_battery"))||void 0===i||i.addEventListener("click",(()=>{var t,e;null===(e=null===(t=null==this?void 0:this.shadowRoot)||void 0===t?void 0:t.querySelector(".low_battery"))||void 0===e||e.remove(),this._vibrate(2)}))}willUpdate(t){var e,i;if(!this.hass||!this._config||!t.has("hass")&&!t.has("_config"))return;const n=this._config.entity,r=this.hass.states[n];if(!r)return;const o=t.get("hass");if(!o||o.states[n]!==r){if(!this._config||!this.hass||!this._config.entity)return;this.stateObj=r;const t=this.stateObj.attributes,n=this.stateObj.state;this.mode=n||"off",t.hvac_modes&&(this.modes=Object.values(t.hvac_modes)),this.value={value:(null==t?void 0:t.temperature)||0,low:(null==t?void 0:t.target_temp_low)||null,high:(null==t?void 0:t.target_temp_high)||null},t.target_temp_step&&(this.step=t.target_temp_step),t.min_temp&&(this.min=t.min_temp),t.max_temp&&(this.max=t.max_temp),t.current_temperature&&(this.current=t.current_temperature);const o=null!==(e=t.current_humidity)&&void 0!==e?e:null==t?void 0:t.humidity;if(void 0!==o&&(this.current_humidity=parseFloat(o.toString())),void 0!==(null==t?void 0:t.window_open)&&(this._hasWindow=!0,this.window=t.window_open),void 0!==(null==t?void 0:t.call_for_heat)&&(this._hasSummer=!0,this.summer=!t.call_for_heat),void 0===(null==t?void 0:t.batteries)||(null===(i=null==this?void 0:this._config)||void 0===i?void 0:i.disable_battery_warning))this.lowBattery=void 0;else{const e=5,i=Object.entries(JSON.parse(t.batteries)).map((t=>({name:t[0],battery:"on"===t[1].battery?e-1:"off"===t[1].battery?100:parseFloat(t[1].battery)}))).filter((t=>t.battery0?this.error=e[0]:this.error=[]}else this.error=[];this._updateDisplay()}}_updateDisplay(){var t;(null===(t=null==this?void 0:this._config)||void 0===t?void 0:t.set_current_as_main)?(this._display_bottom=this._getCurrentSetpoint(),this._display_top=this.current):(this._display_bottom=this.current,this._display_top=this._getCurrentSetpoint())}_handleAction(t){var e,i,n,r,o;if("eco"===t.currentTarget.mode){null===((null===(i=null===(e=null==this?void 0:this.stateObj)||void 0===e?void 0:e.attributes)||void 0===i?void 0:i.saved_temperature)||null)?this.hass.callService("better_thermostat","set_temp_target_temperature",{entity_id:this._config.entity,temperature:(null===(n=this._config)||void 0===n?void 0:n.eco_temperature)||18}):this.hass.callService("better_thermostat","restore_saved_target_temperature",{entity_id:this._config.entity})}else{null!==((null===(o=null===(r=null==this?void 0:this.stateObj)||void 0===r?void 0:r.attributes)||void 0===o?void 0:o.saved_temperature)||null)&&this.hass.callService("better_thermostat","restore_saved_target_temperature",{entity_id:this._config.entity}),this.hass.callService("climate","set_hvac_mode",{entity_id:this._config.entity,hvac_mode:t.currentTarget.mode})}}_setTemperature(){this.hass.callService("climate","set_temperature",{entity_id:this._config.entity,temperature:this.value})}_getCurrentSetpoint(){var t,e,i,n,r,o,a,s;return null!==(null===(t=null==this?void 0:this.value)||void 0===t?void 0:t.high)&&null!==(null===(e=null==this?void 0:this.value)||void 0===e?void 0:e.low)?((null===(i=null==this?void 0:this.value)||void 0===i?void 0:i.low)||0)>=this.current?(null===(n=null==this?void 0:this.value)||void 0===n?void 0:n.low)||0:((null===(r=null==this?void 0:this.value)||void 0===r?void 0:r.high)||0)<=this.current?(null===(o=null==this?void 0:this.value)||void 0===o?void 0:o.high)||0:(null===(a=null==this?void 0:this.value)||void 0===a?void 0:a.low)||0:(null===(s=null==this?void 0:this.value)||void 0===s?void 0:s.value)||0}_renderHVACAction(t=!1){var e,i,n,r,o;const a="heating"===(null===(e=this.stateObj)||void 0===e?void 0:e.attributes.hvac_action)&&"off"!==this.mode,s="cooling"===(null===(i=this.stateObj)||void 0===i?void 0:i.attributes.hvac_action)&&"off"!==this.mode,l=t?"translate(-3,-3.5) scale(0.25)":"translate(5,-4) scale(0.25)",c="#9d9d9d";if(void 0!==(null===(n=null==this?void 0:this.value)||void 0===n?void 0:n.high)&&null!==(null===(r=null==this?void 0:this.value)||void 0===r?void 0:r.high)&&(null===(o=null==this?void 0:this.value)||void 0===o?void 0:o.high)<=this.current){const t=xi(s?{hass:this.hass,string:"extra_states.cooling"}:{hass:this.hass,string:"extra_states.cooling_off"});return Z`${t}`}const u=xi(a?{hass:this.hass,string:"extra_states.heating"}:{hass:this.hass,string:"extra_states.heating_off"});return Z`${u}`}_renderHVACIcon(t){var e,i;return((null===(e=null==this?void 0:this.value)||void 0===e?void 0:e.low)||0)>=this.current?this._renderIcon("heat",t):((null===(i=null==this?void 0:this.value)||void 0===i?void 0:i.high)||0)<=this.current?this._renderIcon("cool",t):this._renderIcon("ok",t)}_renderIcon(t,e){if(!mo[t])return U``;const i=this.hass.localize(`component.climate.state._.${t}`)||xi({hass:this.hass,string:`extra_states.${t}`});return U` ); - const parsedBatteries = batteries.map((data) => ({ "name": data[0], "battery": data[1].battery === "on" ? 5 : data[1].battery === "off" ? 100 : parseFloat(data[1].battery) })); - const lowBatteries = parsedBatteries.filter((entity) => entity.battery < 10); + const parsedBatteries = batteries.map((data) => ({ "name": data[0], "battery": data[1].battery === "on" ? showLowBatteryWarningWhenPercentageLowerThan - 1 : data[1].battery === "off" ? 100 : parseFloat(data[1].battery) })); + const lowBatteries = parsedBatteries.filter((entity) => entity.battery < showLowBatteryWarningWhenPercentageLowerThan); this.lowBattery = lowBatteries[0]; } else { this.lowBattery = undefined;