nielsfaber / scheduler-card

HA Lovelace card for control of scheduler entities
GNU General Public License v3.0
908 stars 115 forks source link

Card disappears when clicking on "New item" #158

Closed mycortex closed 4 years ago

mycortex commented 4 years ago

Version: 1.8.2

When loading my dashboard the card shows all my schedules in the card and it looks like it should. But as soon as I click the NEW ITEM-button the card completely disappears from the dashboard. When reloading the page it comes back showing my schedules as it was before clicking NEW ITEM.

Config:

type: 'custom:scheduler-card'
include:
  - fan
  - light
  - switch
  - climate
groups:
  - name: 'Light/switch'
    include:
      - light
      - switch
  - name: 'Climate/fan'
    include:
      - fan
      - climate
issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.97. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

nielsfaber commented 4 years ago

Can you check your browser logs? There should be an error somewhere, else it shouldn't dissapear completely.

mycortex commented 4 years ago

scheduler-card.js:187 Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined at scheduler-card.js:187 at qs (scheduler-card.js:187) at na (scheduler-card.js:187) at oa (scheduler-card.js:187) at scheduler-card.js:387 at Array.filter () at HTMLElement.getGroups (scheduler-card.js:387) at HTMLElement.render (scheduler-card.js:387) at HTMLElement.update (scheduler-card.js:187) at HTMLElement.performUpdate (scheduler-card.js:148)

nielsfaber commented 4 years ago
  getGroups() {
    if (!this.hass || !this.config) return [];

    const entities = Object.values(this.hass.states)
      .filter(e => entityFilter(e, this.config!, { actions: true }))
      .map(e => e.entity_id);

Looks like hass.states object is empty or something. weird. could you maybe click the link with the error? It should bring you to the part of the code with the problem. Maybe you can share that part/section?

mycortex commented 4 years ago

Hi! Thank you for taking time to check this out :)

I'm not sure what you exactly need, but this is a copy of the whole line behind the point clicking the error-link takes me to:

filter(e=>!["off","heat","cool","heat_cool"].includes(e)),a=zs({field:"temperature",min:e.attributes.min_temp,max:e.attributes.max_temp,step:.5});let n=[];return i.includes("off")?n.push({service:"set_hvac_mode",service_data:{hvac_mode:"off"},icon:"power"}):n.push($s),i.includes("heat")&&1&t&&n.push({service:"set_temperature",service_data:{hvac_mode:"heat"},variable:a,icon:Ns.heat}),i.includes("cool")&&1&t&&n.push({service:"set_temperature",service_data:{hvac_mode:"cool"},variable:a,icon:Ns.cool}),!i.includes("heat")&&!i.includes("cool")&&1&t&&n.push({service:"set_temperature",variable:a,icon:"thermometer"}),i.length>1&&s.length&&n.push({service:"set_hvac_mode",variable:As({field:"hvac_mode",options:s.map(e=>Ts(e,{icons:Ns}))}),icon:"cog-transfer-outline"}),r&&r.length&&16&t&&n.push({service:"set_preset_mode",variable:As({field:"preset_mode",options:r.map(e=>Ts(e))}),icon:"cloud-download-outline"}),n}(e);case"cover":return function(e){const t=e.attributes.supported_features;let r=[{service:"open_cover",icon:Fs(e)},{service:"close_cover",icon:Rs(e)}];return 4&t&&r.push({supported_feature:4,service:"set_cover_position",variable:zs({field:"position",min:0,max:100,unit:"%"}),icon:"ray-vertex"}),r}(e);case"fan":return function(e){const t=e.attributes.supported_features,r=e.attributes.speed_list;let i=[xs,$s];return 1&t&&r&&r.length>1&&i.push({service:"set_speed",variable:As({field:"speed",options:r.map(e=>Ts(e,{icons:Ms}))}),icon:"weather-windy"}),2&t&&i.push({service:"oscillate",variable:As({field:"oscillating",options:[{value:"True"},{value:"False"}]}),icon:"arrow-left-right"}),4&t&&i.push({service:"set_direction",variable:As({field:"direction",options:[{value:"forward"},{value:"reverse"}]}),icon:"cog-clockwise"}),i}(e);case"group":return function(e){return e.attributes.entity_id,[xs,$s]}(e);case"humidifer":return function(e){const t=e.attributes.supported_features,r=e.attributes.available_modes,i=zs({field:"humidity",min:e.attributes.min_humidity,max:e.attributes.max_humidity,step:1,unit:"%"});let s=[];return s.push({service:"set_humidity",variable:i}),r&&r.length>1&&1&t&&s.push({service:"set_mode",variable:As({field:"mode",options:r.map(e=>Ts(e,{icons:Ls}))})}),s}(e);case"input_boolean":return[Object.assign(Object.assign({},xs),{icon:"flash"}),Object.assign(Object.assign({},$s),{icon:"flash-off"})];case"input_number":return function(e){return[{service:"set_value",variable:zs({field:"value",min:Number(e.attributes.min),max:Number(e.attributes.max),step:Number(e.attributes.step)}),icon:"counter"}]}(e);case"input_select":return function(e){const t=e.attributes.options;return t.length<2?[]:[{service:"select_option",variable:As({field:"option",options:t.map(e=>({value:e}))}),icon:"counter"}]}(e);case"light":return function(e){const t=e.attributes.supported_features,r=zs({field:"brightness",min:0,max:255,unit:"%",optional:!0});let i=[];return i.push(Object.assign(Object.assign({},$s),{icon:"lightbulb-off"})),1&t?i.push(Object.assign(Object.assign({},xs),{icon:"lightbulb",variable:r})):i.push(Object.assign(Object.assign({},xs),{icon:"lightbulb"})),i}(e);case"lock":return Vs;case"media_player":return function(e){const t=Array(e.attributes.source_list),r=e.attributes.supported_features;let i=[];return 128&r&&i.push(xs),256&r&&i.push($s),2048&r&&t.length>1&&i.push({service:"select_source",variable:As({field:"source",options:t.map(e=>({value:e}))}),icon:"music-box-multiple-outline"}),i}(e);case"scene":return[Object.assign(Object.assign({},xs),{icon:"play"})];case"script":return function(e){let t=[xs,$s];return t.push({service:ke(e.entity_id),icon:"play",name:ls("services.run_script")}),t}(e);case"switch":return[Object.assign(Object.assign({},xs),{icon:"flash"}),Object.assign(Object.assign({},$s),{icon:"flash-off"})];case"vacuum":return function(e){const t=e.attributes.supported_features;let r=[];return 1&t&&r.push(xs),8192&t&&r.push({service:"start",icon:"play-circle-outline"}),4&t&&r.push({service:"start_pause",icon:"play-circle-outline"}),r}(e);case"water_heater":return function(e){const t=e.attributes.supported_features,r=e.attributes.operation_list,i=zs({field:"temperature",min:e.attributes.min_temp,max:e.attributes.max_temp,step:.5});let s=[];return 1&t&&s.push({service:"set_temperature",variable:i,icon:"thermometer"}),2&t&&s.push({service:"set_operation_mode",variable:As({field:"operation_mode",options:r.map(e=>({value:e}))}),icon:"cog-transfer-outline"}),4&t&&s.push({service:"set_awaymode",variable:As({field:"mode",options:[{value:"on",icon:"toggle-switch-outline"},{value:"off",icon:"toggle-switch-off-outline"}]}),icon:"car-traction-control"}),s}(e);default:return[]}}function Hs(e,t){let r=!1;if(e.match(/^[a-z0-9.]+$/))r=e.includes(".")?e==t:e==Se(t);else try{if(e.startsWith("/")&&e.endsWith("/")||-1!==e.indexOf("")){e.startsWith("/")||(e=`/^${e=e.replace(/./g,".").replace(/*/g,".")}$/`);r=new RegExp(e.slice(1,-1)).test(t)}}catch(e){}return r}function Qs(e,t){return!(!t.include||!t.include.length)&&(!(t.include&&!t.include.find(t=>Hs(t,e)))&&(!t.exclude||!t.exclude.find(t=>Hs(t,e))))}const Bs={alarm_control_panel:"alarm-light-outline",automation:"playlist-play",binary_sensor:"radiobox-blank",camera:"camera",climate:"home-thermometer-outline",cover:"window-shutter",device_tracker:"account",fan:"fan",group:"google-circles-communities",humidifier:"air-humidifier",input_boolean:"drawing",input_number:"ray-vertex",input_select:"format-list-bulleted",input_text:"textbox",light:"lightbulb-outline",lock:"lock-open-outline",media_player:"cast-connected",notify:"comment-alert",person:"account-outline",remote:"remote",scene:"palette-outline",script:"file-document",sensor:"eye",switch:"flash",timer:"timer",vacuum:"robot-vacuum",water_heater:"water-boiler"},Ks=e=>{const t=Se(e.entity_id);switch(t){case"binary_sensor":return(e=>{switch(e.attributes.device_class){case"battery":return"battery-outline";case"cold":return"snowflake";case"connectivity":return"server-network";case"door":return"door-closed";case"garage_door":return"hass:garage";case"gas":case"power":case"problem":case"safety":case"smoke":return"shield-check";case"heat":return"fire";case"light":return"brightness-5";case"lock":return"lock";case"moisture":return"water";case"motion":return"walk";case"occupancy":case"presence":return"home-outline";case"opening":return"hass:square";case"plug":return"hass:power-plug-outline";case"sound":return"music-note";case"vibration":return"vibrate";case"window":return"window-closed";default:return"radiobox-blank"}})(e);case"cover":return Rs(e);case"sensor":return(e=>{switch(e.attributes.device_class){case"humidity":return"water-percent";case"illuminance":return"brightness-5";case"temperature":return"thermometer";case"power":return"flash";case"pressure":return"gauge";case"signal_strength":return"wifi";default:return"°C"==e.attributes.unit_of_measurement||"°F"==e.attributes.unit_of_measurement?"thermometer":"eye"}})(e);default:return t in Bs?Bs[t]:"folder-outline"}},Gs=e=>{switch(Se(e.entity_id)){case"alarm_control_panel":return Ds;case"binary_sensor":case"cover":return Is;case"input_boolean":case"switch":return["on","off"];case"lock":return Us;case"person":return["home","nothome"];default:return}};function Ws(e,t){return e?Object.entries(e).filter(([e])=>t.includes(e)).reduce((e,[t,r])=>Object.assign(e,{[t]:r}),{}):{}}function Ys(e,t){return e?Object.entries(e).filter(([e])=>!t.includes(e)).reduce((e,[t,r])=>Object.assign(e,{[t]:r}),{}):{}}function Js(e){return e.charAt(0).toUpperCase()+e.slice(1)}function Zs(e){return"string"!=typeof e&&(e=String(e)),Js(e.replace(//g," "))}function Xs(e){if(e)return"string"!=typeof e&&(e=String(e)),e.match(/^[a-z]+:[a-z0-9-]+$/i)?e:"hass:"+e}function ea(e){const t=e=>Object.entries(e).sort((e,t)=>e[0]>t[0]?1:-1).map(([e,r])=>[e,"object"==typeof r&&null!==r?t(r):r]).reduce((e,[t,r])=>Object.assign(e,{[t]:r}),{});let r=Ws(e,["service","servicedata"]);r=t(r);return Object.values(r).map(e=>JSON.stringify(e).replace(/[\W]/g," ").split(" ").filter(e=>" "!=e&&""!=e).join("")).join("_")}function ta(e){const t=e.service;let r={id:"",name:e.name,icon:e.icon||"flash",service:t};return e.service_data&&Object.keys(e.service_data).length&&Object.assign(r,{service_data:e.service_data}),e.variable&&("options"in e.variable?Object.assign(r,{variable:As(e.variable)}):Object.assign(r,{variable:zs(e.variable)})),Object.assign(r,{id:ea(r)}),r}function ra(e){let t=e.entity,r=e.service;const i=Se(t),s=Se(r);i&&s?i==s&&(r=ke(r)):i||(t=s+"."+t,r=ke(r));let a=Ys(e,["service","entity","service_data"]),n={entity:t,service:r};return a=Object.assign(Object.assign({},a),e.service_data),Object.keys(a).length&&(n=Object.assign(Object.assign({},n),{service_data:a})),n}function ia(e,t){const r=e.actions.map(ta),i=ea(Ys(t,["entity"]));return r.map(e=>{if(e.id==i)return!0;if(e.variable&&t.service_data&&Object.keys(t.service_data).includes(e.variable.field)){let r=Object.entries(t.service_data).filter(([t])=>t!=e.variable.field).reduce((e,[t,r])=>Object.assign(e,{[t]:r}),{}),i=Object.keys(r).length?Object.assign(Object.assign({},t),{service_data:r}):Ys(t,["servicedata"]);return e.id==ea(i)}return!1}).map((e,t)=>e?t:null).filter(e=>null!==e)}function sa(e,t){const r=ia(e,t);return r.length?ta(e.actions[r[0]]):ta(Ys(t,["entity"]))}function aa(e){return e.match(/^switch.schedule[0-9a-f]{6}$/)}function na(e,t){if(!e)return;const r="string"==typeof e?e:e.entity_id;let i={id:r,name:e.attributes.friendly_name||ke(r),icon:"folder-outline",actions:[]};if((void 0===t.standard_configuration||t.standard_configuration)&&(i=Object.assign(Object.assign({},i),{actions:[...qs(e)],icon:Ks(e),states:Gs(e)})),i=Object.assign(Object.assign({},i),{icon:e.attributes.icon||i.icon}),t.customize){Object.entries(t.customize).filter(([e])=>Hs(e,r)).map(([,e])=>e).forEach(e=>{if(i=Object.assign(Object.assign({},i),Ys(e,["actions","exclude_actions"])),e.actions&&e.actions.forEach(e=>{const t=ia(i,e);let r=i.actions;t.length?r=i.actions.map((r,i)=>t.includes(i)?Object.assign(r,e):r):i.actions.push(e),i=Object.assign(Object.assign({},i),{actions:r})}),e.exclude_actions){let t=e.excludeactions.map(e=>e.replace(//g," ").trim().toLowerCase());i.exclude_actions&&(t=t.concat(i.exclude_actions).filter((e,t,r)=>r.indexOf(e)===t)),i=Object.assign(Object.assign({},i),{exclude_actions:t})}})}return i}function oa(e,t,r){const i="object"==typeof e?e.entity_id:e;if(aa(i))return!1;if(!(Qs(i,t)||t.groups&&t.groups.some(e=>Qs(i,e))))return!1;if(r&&"object"==typeof e){const i=na(e,t);if(!i)return!1;if(r.states&&!i.states)return!1;if(r.actions&&!i.actions.length)return!1}return!0}"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;function ca(e,t,r){return e(r={path:t,exports:{},require:function(e,t){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==t&&r.path)}},r.exports),r.exports}var la=ca((function(e,t)

nielsfaber commented 4 years ago

I would propose to update the card to v1.8.4 and try again. Not that i found the problem, but some changes may already have impacted this issue. Please check in the browser that v1.8.4 is really loaded (old one could still be in your browser cache). If so, then send again the error description + part of the code.

Your previous error description said

at qs (scheduler-card.js:187)

but the snippit you send did not contain a function qs() (only a Qs() but this is not the same). So at this point i have no idea still..

I could also send you a non-minified version of the card, which makes the code a lot more readable. Then we will find the problem for sure..

mycortex commented 4 years ago

OK. Running version 1.8.4 now. Same problem...

This is all of the code from after clicking the Add item-button. If this isn't enought I guess we have to try the non-minified version. Had to put the code in a file as it's too long to paste in here.

Console: scheduler-card.js:186 Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined at scheduler-card.js:186 at Zs (scheduler-card.js:186) at aa (scheduler-card.js:186) at na (scheduler-card.js:186) at scheduler-card.js:389 at Array.filter () at HTMLElement.getGroups (scheduler-card.js:389) at HTMLElement.render (scheduler-card.js:389) at HTMLElement.update (scheduler-card.js:186) at HTMLElement.performUpdate (scheduler-card.js:148)

Source: Source_SchedulerCard.txt

nielsfaber commented 4 years ago

Yeah... I couldnt make any sense of it all. Please run this one scheduler-card.zip And share the console output (should be enough).

mycortex commented 4 years ago

OK. Now using the version you sent.

Console: scheduler-card.js:186 Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined at scheduler-card.js:186 at Zs (scheduler-card.js:186) at aa (scheduler-card.js:186) at na (scheduler-card.js:186) at scheduler-card.js:389 at Array.filter () at HTMLElement.getGroups (scheduler-card.js:389) at HTMLElement.render (scheduler-card.js:389) at HTMLElement.update (scheduler-card.js:186) at HTMLElement.performUpdate (scheduler-card.js:148) (anonymous) @ scheduler-card.js:186 Zs @ scheduler-card.js:186 aa @ scheduler-card.js:186 na @ scheduler-card.js:186 (anonymous) @ scheduler-card.js:389 getGroups @ scheduler-card.js:389 render @ scheduler-card.js:389 update @ scheduler-card.js:186 performUpdate @ scheduler-card.js:148 _enqueueUpdate @ scheduler-card.js:148 async function (async) _enqueueUpdate @ scheduler-card.js:148 requestUpdateInternal @ scheduler-card.js:148 initialize @ scheduler-card.js:148 initialize @ scheduler-card.js:186 W @ scheduler-card.js:148 ie @ scheduler-card.js:186 co @ scheduler-card.js:389 _clone @ scheduler-card.js:56 __commitTemplateResult @ scheduler-card.js:82 commit @ scheduler-card.js:82 (anonymous) @ scheduler-card.js:186 ie.render @ scheduler-card.js:186 update @ scheduler-card.js:186 performUpdate @ scheduler-card.js:148 _enqueueUpdate @ scheduler-card.js:148 async function (async) _enqueueUpdate @ scheduler-card.js:148 requestUpdateInternal @ scheduler-card.js:148 set @ scheduler-card.js:148 _addItemClick @ scheduler-card.js:1376 handleEvent @ scheduler-card.js:82 boundHandleEvent @ scheduler-card.js:82 newItemClick @ scheduler-card.js:364 handleEvent @ scheduler-card.js:82 boundHandleEvent @ scheduler-card.js:82

nielsfaber commented 4 years ago

This console log output does not match with the code I sent you (output is still from the minified version). You should refresh your browser cache before..

mycortex commented 4 years ago

Correct now? Had to completely remove the HACS version to load the new one

Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined scheduler-card.js:5720 at climateActions (scheduler-card.js:5720) at standardActions (scheduler-card.js:6302) at entityConfig (scheduler-card.js:6528) at entityFilter (scheduler-card.js:6566) at scheduler-card.js:14522 at Array.filter () at HTMLElement.getGroups (scheduler-card.js:14522) at HTMLElement.render (scheduler-card.js:14555) at HTMLElement.update (scheduler-card.js:2617) at HTMLElement.performUpdate (scheduler-card.js:2131) climateActions @ scheduler-card.js:5720 standardActions @ scheduler-card.js:6302 entityConfig @ scheduler-card.js:6528 entityFilter @ scheduler-card.js:6566 (anonymous) @ scheduler-card.js:14522 getGroups @ scheduler-card.js:14522 render @ scheduler-card.js:14555 update @ scheduler-card.js:2617 performUpdate @ scheduler-card.js:2131 _enqueueUpdate @ scheduler-card.js:2084 async function (async) _enqueueUpdate @ scheduler-card.js:2078 requestUpdateInternal @ scheduler-card.js:2050 initialize @ scheduler-card.js:1902 initialize @ scheduler-card.js:2547 UpdatingElement @ scheduler-card.js:1666 LitElement @ scheduler-card.js:2479 SchedulerEditorCard @ scheduler-card.js:14517 _clone @ scheduler-card.js:516 __commitTemplateResult @ scheduler-card.js:936 commit @ scheduler-card.js:878 render @ scheduler-card.js:1230 render$1 @ scheduler-card.js:1541 update @ scheduler-card.js:2622 performUpdate @ scheduler-card.js:2131 _enqueueUpdate @ scheduler-card.js:2084 async function (async) _enqueueUpdate @ scheduler-card.js:2078 requestUpdateInternal @ scheduler-card.js:2050 set @ scheduler-card.js:1780 _addItemClick @ scheduler-card.js:16854 handleEvent @ scheduler-card.js:1131 EventPart.boundHandleEvent @ scheduler-card.js:1097 newItemClick @ scheduler-card.js:14385 handleEvent @ scheduler-card.js:1131 EventPart.boundHandleEvent @ scheduler-card.js:1097

nielsfaber commented 4 years ago

Yup, thanks. Found the problem. Looks like you have a climate entity that does not have the property 'hvac_modes' defined. Which is weird, since it is a mandatory property.

According to the developer docs, it should even result in a failure of the climate component if it is not present.

Anyway, i will fix it, no problem. Took much longer to find the issue than solving it 👍

mycortex commented 4 years ago

OK. That's strange... I have no error related to climate entities or lack of hvac_mode in components in my log.

But I am running a custom climate component that I had to refactor when the "Climate 2.0" breaking change came in 0.94. It's possible there are some leftovers or something, but everything is running smooth in my system and there is no errors or warnings.

Anyways, thank for taking time to fix this 👍

Will get back to the HACS-version now and wait for an update. Thank you for this great component and card 👍

mycortex commented 4 years ago

Since you found that a climate entity was the cause I just removed Climate from Include-filter and it works fine

nielsfaber commented 4 years ago

Think its solved in v1.9.0 👍 Put some extra checks in it.

nielsfaber commented 4 years ago

Time to clean up old issues.. Please re-open if the issue is not resolved.

mycortex commented 4 years ago

Now running version 1.9.3 and still the same problem. Works fine without Climate included, but with Climate included it disappears when clicking Add. Will use it without adding Climate for now. Hope you find a fix for it.

mycortex commented 4 years ago

Turns out that the problem was old, unavailable and disabled climate entities. Removed them and now no problem.

massive commented 3 years ago

I have this same issue. I have no unavailable climate entities.

The version is 1.9.7 and this is the stacktrace:

Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined
    at Ii (scheduler-card.js:286)
    at scheduler-card.js:286
    at Array.map (<anonymous>)
    at Ii (scheduler-card.js:286)
    at Ri (scheduler-card.js:286)
    at Bi (scheduler-card.js:286)
    at scheduler-card.js:1510
    at Array.filter (<anonymous>)
    at HTMLElement.getDomainSwitches (scheduler-card.js:1510)
    at HTMLElement.render (scheduler-card.js:1508)

If the pretty-printed line numbers (by Chrome) tell the truth, the problem happens "groups" handler. The last line in this snippet.

        case "group":
            const e = (a && a.attributes.entity_id && Array.isArray(a.attributes.entity_id) ? a.attributes.entity_id : []).map(e=>Ii(e, t));
            return o = e,
            ((n = a) && n.attributes.entity_id && Array.isArray(n.attributes.entity_id) ? n.attributes.entity_id : []).map(e=>_i(e)).filter((e,t,i)=>i.indexOf(e) === t).length > 1 && (o = o.map(e=>e.map(e=>"turn_on" == vi(e.service) || "turn_off" == vi(e.service) ? Object.assign(Object.assign({}, e), {
                service: "homeassistant." + vi(e.service),
                icon: "turn_on" == vi(e.service) ? "flash" : "flash-off"
            }) : e))),
            o[0].filter(e=>o.every(t=>t.map(e=>Mi(e)).includes(Mi(e))));