Tasshack / dreame-vacuum

Home Assistant integration for Dreame robot vacuums with map support
https://community.home-assistant.io/t/custom-component-dreame-vacuum
MIT License
879 stars 110 forks source link

Obstacles Card in browsr mod popup #731

Closed 19Francesco81 closed 1 month ago

19Francesco81 commented 1 month ago

I used the obstacle card copied from the following link: https://github.com/yarikoptic/dreame-vacuum/blob/enh-codespell/docs/map.md#obstacles-card I inserted the code into a tile of the Xiaomi map card that calls a script in the browser mod popup Everything seems to work and I see the first photo, but when I go to select more the popup closes and doesn't let me select any more photos.

in card:

Script: alias: Ostacoli sequence:

Please help me! I'm going crazy :)

Tasshack commented 1 month ago

Problem is that dropdown menu emits a close event, popup gets that event and closes too. Same thing happens if you try to change selected obstacle from card configurator too.

I will fix that as soon as I am available.

Tasshack commented 1 month ago
type: custom:button-card
entity: camera. # Current map camera entity
label: Obstacles # Optional
title: null # Optional
icon: mdi:traffic-cone
entity_picture: >
  [[[ return function(t){const e=t._config;if(t.shadowRoot.querySelector("hui-error-card")&&!t._u)return t._u=!0,t.style.opacity=0,void setTimeout((function(){t.style.opacity=1,t.update()}),0);if(!t.__shouldUpdate){t._u=!1;const i=/v=([^&]*)/,o=/index=([^&]*)/;t.__shouldUpdate=t.shouldUpdate,t.shouldUpdate=function(t){const e=this;let o=e.__shouldUpdate(t);if(t&&t.has("_config"))return e.shouldUpdate=e.__shouldUpdate,e.__shouldUpdate=null,e._s=null,!0;const a=e._config,n=e._hass.states[a.entity],l=t.get("_hass"),s=l?l.states[a.entity]:null;if(o=l&&s!=n,!o)return!1;e._l&&(e._l.stateObj=n);let r=e.__items(e._hass);const c=e.__items(l)[0].join(",")!==r[0].join(",");if(e._i&&n&&!c){if(!c&&e._i&&n&&e._a){const t=n.attributes.access_token;if(t!=e._a&&(e._config.hold_action.url_path=e._i[1].replace(e._a,t),e._a=t,this._config.custom_fields))return!0}return e.hass=e._hass,!1}let d=0;const u=e._s;if(n){const t=n.attributes.access_token;if(!c&&t!=e._a){let i=n.attributes[a.picture_list+"_picture"];if(i)if("string"==typeof i||i instanceof String)i=i.replace(e._a,t);else for(let o=0;o<r[0].length;o++)i[r[0][o]]=i[r[0][o]].replace(e._a,t)}if(e._a=t,d=u&&""===u.value?0:parseInt(u.value),d<0||d>=r[0].length)d=0;else if(d>0&&r[0].length>0&&(d=0,e._i&&(d=r[0].indexOf(e._i[0]),-1===d))){d=0;let t=e._i[1].match(i)[1];for(let e=0;e<r[1].length;e++)if(t==r[1][e].match(i)[1]){d=e;break}}}return u.label=!(!a.label||!n)&&a.label,e.__populate(d,r[0],u),u.dispatchEvent(new Event("change")),o},t.__items=function(t){if(t){const e=t.states[this._config.entity];if(e&&e.attributes){const t=e.attributes[this._config.picture_list+"_picture"];if(t){if("string"==typeof t||t instanceof String)return[[""],[t]];{const e=Object.values(t);if(e.length)return[Object.keys(t),e]}}}}return[[],[]]},t.__populate=function(t,e,i){const o={"(Completed)":"✔️","(Interrupted)":"❌","(Manually Ignored)":"⛔","(Automatically Ignored)":"⭕","(Edited)":"📝","(Original)":"✅","(Backup)":"💾"},a=new RegExp(Object.keys(o).join("|").replaceAll(")","\\)").replaceAll("(","\\("),"g"),n=function(t){return t&&t.length?t.replace(a,(t=>o[t])):""};(t<0||t>=e.length)&&(t=0);let l="";for(let s=0;s<e.length;s++)l+='<mwc-list-item value="'+s+'"'+(t==s?" selected activated":"")+">"+n(e[s])+"</mwc-list-item>";i.innerHTML=l,i.value=t+"",i.selectedText=n(e[t])};const a="picture-selector",n=t.shadowRoot.querySelector("ha-card#"+a);n&&n.remove();const l=document.createElement("ha-card");let s;l.id=a,l.style.cssText="cursor:default;display:block;padding:12px 16px 16px;overflow:visible;border-bottom-left-radius:0;border-bottom-right-radius:0;border-bottom:none;",t.shadowRoot.children.length?t.shadowRoot.insertBefore(l,t.shadowRoot.children[0]):t.shadowRoot.appendChild(l),e.title&&e.title.length&&(" "!==e.title&&(s=document.createElement("h1"),s.className="card-header",s.style.padding="0 0 4px 0",s.innerHTML='<div class="name">'+e.title+"</div>",l.appendChild(s)),e.styles.grid||(e.styles.grid=[]),e.styles.grid=e.styles.grid.concat([{cursor:"default"},{display:"block"},{margin:"0 16px 16px"},{overflow:"hidden"},{"border-radius":"var(--ha-card-border-radius,12px)"},{"border-width":"var(--ha-card-border-width,1px)"},{"border-style":"solid"},{"border-color":"var(--ha-card-border-color,var(--divider-color,#e0e0e0))"},{background:"var(--input-disabled-fill-color);"}]));const r=t._hass.states[e.entity];t._a=r?r.attributes.access_token:"";const c=t.__items(t._hass),d=document.createElement("ha-select");if(d.style.cssText="width:auto;display:block;",e.icon&&e.icon.length){d.style.cssText+="margin:4px 0 0 40px;";const i=document.createElement("state-badge");i.stateObj=r,i.overrideIcon=e.icon,i.style.cssText="float:left;margin-top:12px;",l.appendChild(i),t._l=i}d.label=!(!e.label||!r)&&e.label,d.value="0",d.naturalMenuWidth=!0,1==c[0].length&&0==c[0][0].length&&(s?(d.style.display="none",t._l&&(t._l.style.display="none"),s.style.padding="0"):l.style.display="none"),t.__populate(0,c[0],d),l.appendChild(d),t._s=d;const u=function(e){const i=function(i){if(!i)return;const o=function(t){const e=t.target.parentElement.querySelectorAll("ha-circular-progress").forEach((t=>t.remove()));e&&e.remove(),t.target.style.cursor=""};i.onload=function(t){i.style.height="",t.target.style.opacity=1,o(t),t.target.onload=null},i.onerror=function(e){t._i=!1,o(e),t.update()},i.style.opacity=0,i.style.cursor="wait",e&&0==i.naturalHeight&&i.width&&(i.style.height=i.width+"px");const a=document.createElement("ha-circular-progress");a.active=!0,a.size="large",a.style.position="absolute",i.parentElement.appendChild(a)},o=t.shadowRoot.querySelector("#icon");o&&"IMG"==o.tagName?i(o):setTimeout((function(){const e=t.shadowRoot.querySelector("#icon");e&&0==e.naturalHeight&&e.width&&(e.style.height=e.width+"px"),i(e)}),0)};d.addEventListener('closed',function(e){e.stopPropagation()});if(d.onchange=function(e){if(""!=e.target.value){const a=t.__items(t._hass);if(0===a[1].length)return void(t._i=!1);const n=parseInt(e.target.value);if(n>=0&&n<a[1].length&&(!t._i||t._i[0]!=a[0][n]&&t._i[1]!=a[1][n])){if(t._i){if(t._i[0]=a[0][n],t._i[1].match(i)[1]===a[1][n].match(i)[1])return;t._i[1]=a[1][n]}else t._i=[a[0][n],a[1][n]];t._config.hold_action.url_path=t._i[1];const e=t._i[1].match(o);t._x=e&&e.length>1?e[1]:"",u(),t.update()}}},t._i=!1,t._config.hold_action={action:"url"},c[0].length){t._config.hold_action.url_path=c[1][0];const e=c[1][0].match(o);t._x=e&&e.length>1?e[1]:"";const i=t.shadowRoot.querySelector("#icon");i&&"IMG"==i.tagName?t._i=[c[0][0],c[1][0]]:(t.style.opacity=0,setTimeout((function(){t._i=[c[0][0],c[1][0]],u(!0),t.update(),t.style.opacity=1}),0))}}return t._i?t._i[1]+(e.extra_params&&e.extra_params.length?"&"+e.extra_params:""):void 0}(this) ]]]
picture_list: obstacle
size: 100%
show_entity_picture: true
show_name: false
custom_fields:
  buttons:
    card:
      type: custom:button-card
      name: >
        [[[ return (!this.__c || this.__c(this.__o()) ? 'Cancel Ignore' : 'Ignore Obstacle'); ]]]
      icon: >
        [[[ return (!this.__c || this.__c(this.__o()) ? 'mdi:eye' : 'mdi:eye-off'); ]]]
      confirmation: >
        [[[ return this.__c && !this.__c(this.__o()); ]]]
      size: 24px
      tap_action:
        action: call-service
        service: dreame_vacuum.vacuum_set_obstacle_ignore
        data: >
          [[[ return function(t){t.__o||(t.__o=function(){const n=t._hass.states[t._config.entity];return n&&n.attributes.obstacles&&n.attributes.obstacles[t._i?t._x:""]},t.__c=function(t){return t&&t.ignore_status&&"Not Ignored"!==t.ignore_status},t.__d=function(t){return t&&t.ignore_status&&t.ignore_status.indexOf("Automatically")>=0});const n=t.__o();if(n)return{entity_id:t._config.entity.replace("camera.","vacuum.").substring(0,t._config.entity.lastIndexOf("_map")),x:n.x,y:n.y,obstacle_ignored:!t.__c(n)}}(this); ]]]
      styles:
        grid:
          - display: contents
        img_cell:
          - display: contents
        name:
          - color: >
              [[[ return (!this.__d || this.__d(this.__o()) ? 'var(--state-icon-unavailable-color)' : 'var(--primary-text-color)'); ]]]
        icon:
          - color: >
              [[[ return (!this.__d || this.__d(this.__o()) ? 'var(--state-icon-unavailable-color)' : 'var(--primary-text-color)'); ]]]
          - margin: 0 6px 0 0
        card:
          - background-color: >
              [[[ return (!this.__d || this.__d(this.__o()) ? 'var(--state-unavailable-color)' : 'var(--primary-color)'); ]]]
          - pointer-events: >
              [[[ return (!this.__d || this.__d(this.__o()) ? 'none' : 'auto'); ]]]
          - border-radius: 100px
          - max-width: 200px
          - max-height: 48px
          - padding: 8px 24px
styles:
  custom_fields:
    buttons:
      - cursor: >
          [[[ return (!this.__d || this.__d(this.__o()) ? 'not-allowed' : 'default'); ]]]
      - margin: 16px
      - display: >
          [[[ const o = this.__o(); return (o && o.ignore_status ? 'inline-block': 'none'); ]]]
      - width: auto
  grid:
    - display: block
  icon:
    - transition: opacity 180ms ease-in-out 0s
  card:
    - '--mdc-ripple-color': rgba(0,0,0,0)
    - padding: 0
    - border-top-left-radius: 0
    - border-top-right-radius: 0
    - border-top: none
Tasshack commented 1 month ago

I will close the issue after the documentation has been updated.

19Francesco81 commented 1 month ago

I will close the issue after the documentation has been updated.

thx