Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

${card.querySelector()} not working on initial page load, but works after a state change on any bubble card #975

Open
quincarter opened this issue Dec 1, 2024 · 16 comments
Labels
bug Something isn't working Confirmed One of the collaberators confirmed this bug.

Comments

@quincarter
Copy link

quincarter commented Dec 1, 2024

For any feature request you can open a new discussion here:
https://github.com/Clooos/Bubble-Card/discussions/categories/feature-requests

For any question you can open a new discussion here:
https://github.com/Clooos/Bubble-Card/discussions/categories/q-a

Describe the bug
When the same hass.states[].state is used multiple times in a style template, the second time doesn't always resolve properly. For example when I try to listen to the do not disturb sensor on my phone, i want to change the bubble button color and set the text for the bubble name to say Quin is on DND - This works if i am already on the home assistant dashboard then it works fine, but when i reload the page, the color resolves but the text does not, and it shows my default name variable.

*****EDIT --- I think the bug has to do with the card.querySelector() and when it is going off. The element isn't selectable on the initial page load. That's why the colors are working but not the nested JS stuff.

Expected behavior
*.querySelector() should work on initial load so that button states and names can be resolved for anything custom that is being overwritten for customization

Screenshots
Screenshot 2024-12-01 at 6 43 17 PM

YAML

.bubble-button-background {
    opacity: 0.5 !important;
    background-color: ${state === 'home' ? 'green' : 'red'} !important;
  }

.bubble-button-background {
    opacity: 0.5 !important;
    background-color: ${hass.states['sensor.quin_s_pixel_9_pro_xl_do_not_disturb_sensor'].state === 'priority_only' ? 'lightgrey' : ''} !important;
  }

.bubble-button-background {
    opacity: 0.5 !important;
    background-color: ${hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state === 'on' ? 'rebeccapurple' : ''} !important;
  }

.bubble-sub-button {
    background-color: transparent !important;
  }

.bubble-sub-button-1 {
    display: ${(hass.states['sensor.quin_s_pixel_9_pro_xl_battery_state'].state === 'charging' || hass.states['sensor.quin_s_pixel_9_pro_xl_battery_state'].state === 'full')  && hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state !== 'on' ? '' : 'none'} !important;
  }

.bubble-sub-button-2 {
    display: ${hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state === 'on' ? '' : 'none'} !important;
  }

/* this one doesn't ever fire on initial load unless another change is fired */
${hass.states['sensor.quin_s_pixel_9_pro_xl_do_not_disturb_sensor'].state === 'priority_only' ? card.querySelector('.bubble-name').innerText = "Quin is on DND" : card.querySelector('.bubble-name').innerText = "Quin"}

Full button code

type: custom:bubble-card
card_type: button
button_type: state
entity: person.quin_carter
styles: >-
  .bubble-button-background {
      opacity: 0.5 !important;
      background-color: ${state === 'home' ? 'green' : 'red'} !important;
    }

  .bubble-button-background {
      opacity: 0.5 !important;
      background-color: ${hass.states['sensor.quin_s_pixel_9_pro_xl_do_not_disturb_sensor'].state === 'priority_only' ? 'lightgrey' : ''} !important;
    }

  .bubble-button-background {
      opacity: 0.5 !important;
      background-color: ${hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state === 'on' ? 'rebeccapurple' : ''} !important;
    }

  .bubble-sub-button {
      background-color: transparent !important;
    }

  .bubble-sub-button-1 {
      display: ${(hass.states['sensor.quin_s_pixel_9_pro_xl_battery_state'].state === 'charging' || hass.states['sensor.quin_s_pixel_9_pro_xl_battery_state'].state === 'full')  && hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state !== 'on' ? '' : 'none'} !important;
    }

  .bubble-sub-button-2 {
      display: ${hass.states['binary_sensor.quin_s_pixel_9_pro_xl_android_auto'].state === 'on' ? '' : 'none'} !important;
    }

  ${hass.states['sensor.quin_s_pixel_9_pro_xl_do_not_disturb_sensor'].state ===
  'priority_only' ? card.querySelector('.bubble-name').innerText = "Quin is on
  DND" : card.querySelector('.bubble-name').innerText = "Quin"}
name: ""
show_attribute: false
show_last_changed: true
sub_button:
  - entity: sensor.quin_s_pixel_9_pro_xl_battery_level
    show_state: true
    show_attribute: false
    show_last_changed: false
    show_name: false
    show_icon: true
    icon: ""
  - entity: binary_sensor.quin_s_pixel_9_pro_xl_android_auto
    icon: mdi:car
button_action:
  tap_action:
    action: navigate
    navigation_path: "#person-quin"
card_layout: large

Informations (please complete the following information):

  • OS: Android and MacOS on Chrome
  • Browser/App: Chrome
  • Bubble Card version: 2.3.0
  • Home Assistant versions
Module Version
Core 2024.11.3
Supervisor 2024.11.4
Operating System 13.2
Frontend 20241106.2
@quincarter quincarter added the bug Something isn't working label Dec 1, 2024
@quincarter
Copy link
Author

Followup question. Can i set JS variables in the style template and reuse them?

@quincarter
Copy link
Author

quincarter commented Dec 2, 2024

If i manually fire off a state change of some kind after the elements are on the screen, the items update. I think there is something wrong with how the styles are appended and looking for the card.querySelector() - i think it is returning null or undefined because the element isn't loaded quite yet. In LitElement i would just do everything in firstUpdated() and wait for the items to be appended to the screen. But looks like everything here is in connectedCallback because it is just an HTMLElement. So i think the solution would be to wait for the element to be available in the shadowDom for the querySelector to work properly on the initial load.

@quincarter
Copy link
Author

@quincarter quincarter changed the title Using the same hass.states[] multiple times doesn't work on initial load ${card.querySelector()} not working on initial page load, but works after a state change on any bubble card Dec 2, 2024
@quincarter quincarter changed the title ${card.querySelector()} not working on initial page load, but works after a state change on any bubble card ${card.querySelector()} not working on initial page load, but works after a state change on any bubble card Dec 2, 2024
@quincarter
Copy link
Author

quincarter commented Dec 2, 2024

Update - when i only try to update the .bubble-name innerText on a single test button, i get this error:

image

So i know it is completely unrelated to my css stuff.

My YAML

type: custom:bubble-card
card_type: button
entity: light.my_custom_lamp
icon: mdi:lamp
show_state: true
show_last_changed: true
styles: |
  ${card.querySelector('.button-name').innerText = 'Testing123'}

@quincarter
Copy link
Author

Another video showing the delay/timeout it takes for the name to update. Not sure why this is happening, honestly. I have the code pulled down but i don't know how to test it in HA to get an idea of how to fix it.

https://drive.google.com/file/d/1gQdOixLgXnOGnwQxbnQahRJqh4JPL6Tq/view?usp=sharing

@Clooos
Copy link
Owner

Clooos commented Dec 3, 2024

Hi! For your error in your previous comment, you don't use the correct class, it's .bubble-name and not .button-name.

I will check if I can reproduce your initial issue.

@Clooos
Copy link
Owner

Clooos commented Dec 3, 2024

I was able to reproduce it and indeed it was only an issue when card.querySelector('.bubble-name').innerText was used, I say "was" because this is now fixed on my side, just wait for the next release now 🙂

@quincarter
Copy link
Author

Hi! For your error in your previous comment, you don't use the correct class, it's .bubble-name and not .button-name.

I will check if I can reproduce your initial issue.

Yeah I actually solved the class error.

@quincarter
Copy link
Author

I was able to reproduce it and indeed it was only an issue when card.querySelector('.bubble-name').innerText was used, I say "was" because this is now fixed on my side, just wait for the next release now 🙂

Nice! Can you possibly reference this issue so I can review what you did update it? I'm really interested how you handled that with the HTMLElement inside of HA.

Also, can you add contribution instructions to your readme? I would love to be able to contribute to the project, but I've never built a front-end plug-in for home assistant before. So I don't even know how to get started with contribution or testing in my setup. I'm a frontend developer and would really like to be able to contribute to a cool project in FOSS and this one is pretty cool.

@Clooos
Copy link
Owner

Clooos commented Dec 3, 2024

Indeed I should clarify this, in fact you "just" need to install Webpack to build the sources, there is a config file at the root of the GitHub repo, just use it then you're all good. I've configured it so that it send the updated js file to my HA server each time that I save, I've also disabled the cache in my browser for Home Assistant.

And your help would be more than welcome, working on this project just by myself is not always the easiest task! 😃

@Clooos
Copy link
Owner

Clooos commented Dec 3, 2024

And this is the only solution I found to fix this 👀

image

I also tried updating the custom styles before the name but that don't work for some reason 🤔

@quincarter
Copy link
Author

You may want to variabilize that class name or the selector name that you're passing into the query selector function. Then it'll work for whatever you pass into it maybe?

@Clooos
Copy link
Owner

Clooos commented Dec 3, 2024

I don't know, I will not try other ways for now (I'm truly tired, and I will have less time for some days), but don't hesitate to give it a look! If you have any questions, don't hesitate to ask them! 😄

And to be honest I've started Bubble Card as a way to learn JS, I'm not an experienced developer even if I've learned a lot by myself in a year with this project. So some of my approaches are probably not the most optimized ones.

@quincarter
Copy link
Author

I will take a look! You've done a great job!

@MrBearPresident MrBearPresident added Bug-Fix 🥳 Confirmed One of the collaberators confirmed this bug. labels Dec 10, 2024
Clooos added a commit that referenced this issue Dec 12, 2024
- [x] Fan modes button error when HVAC is in Dry Mode #987
- [x] Enhancement: Add Box-Shadow Customization to Bubble Card. Fix added for PR #1009 by @flobiwankenobi
- [x] Better color handling for lights, now if the light doesn’t supports RGB it will take the accent color instead like the default Home Assistant behavior. #692
- [x] Templating .bubble-name is now instant. #975
- [x] Fixed an issue where some entity_picture url were not displayed.
- [x] New pop-up trigger system based on the same conditional system that Home Assistant use. Not to be confused with the visibility conditions. Note: This is NOT a breaking change, if you previously added a trigger in a pop-up, it will still works, but the editor will not display it anymore and it will be visible in YAML mode only. Then if you decide to use the new system, it will be used instead of your previous trigger. This new feature is based on the code from @MrBearPresident, so thanks again to him for this great new possibility! #332
@Clooos
Copy link
Owner

Clooos commented Dec 12, 2024

The new release is here! Tell me if everything works as it should now 🙂

https://github.com/Clooos/Bubble-Card/releases/tag/v2.3.1

@Clooos
Copy link
Owner

Clooos commented Dec 22, 2024

Hi! I had to remove my fix in the new beta as I suspect that it was causing performance issues, I definitely need to find a better fix! Unless you want to help me on this 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Confirmed One of the collaberators confirmed this bug.
Projects
None yet
Development

No branches or pull requests

3 participants