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

useEntity not working for light entities that are turned off #102

Closed
rensknoors opened this issue Nov 18, 2023 · 6 comments
Closed

useEntity not working for light entities that are turned off #102

rensknoors opened this issue Nov 18, 2023 · 6 comments
Labels
good first issue Good for newcomers

Comments

@rensknoors
Copy link

rensknoors commented Nov 18, 2023

Describe the bug
When a light entity is turned off, it cannot read a value from hs_color because it's null. I believe this started happening after upgrading to Home Assistant OS 2023.11.0.
When the light entity is turned it, it works as expected.

To Reproduce
Steps to reproduce the behavior:

  1. Install Home Assistant OS 2023.11.0
  2. Use useEntity('light.living_room_group') or <ButtonCard entity='light.living_room_group' />
  3. See error "Cannot read properties of null (reading '0')"

Expected behavior
I expect the ButtonCard to display the light entity in the "off" state without giving an error.

Screenshots / code examples
image
image

System Info (please complete the following information):

  • Home Assistant OS 2023.11.0 and higher

Additional context
The error seems to be happening in colors.ts and might be fixed by using a fallback value.

@shannonhochkins
Copy link
Owner

Thanks for the details! Easy fix, is this error thrown by a custom use case or are you using both packages? Just odd I haven't seen this that's all!

@shannonhochkins
Copy link
Owner

Home assistant used to completely remove properties when the device is off, I'll have to take another look into it!

@rensknoors
Copy link
Author

I made my own custom card for the lights so at first I thought it had something to do with my own code but after some testing I noticed that it was an issue with useEntity.

I have both packages packages installed but I barely use any components from @hakit/components because I create my own custom cards.

It's weird that Home Assistant shows the properties even with null values. I haven't seen this change mentioned in the changelogs while upgrading. Perhaps it's an issue specific to my installation, but useEntity used to work just fine.

@shannonhochkins
Copy link
Owner

What version of @hakit are you using? I just checked my code and i must have run into something similar 2 weeks ago

function toRGB(entity: HassEntity): [number, number, number] | null {
  if (entity.attributes) {
    if ("hs_color" in entity.attributes && entity.attributes.hs_color !== null) {
      return hs2rgb([entity.attributes.hs_color[0], entity.attributes.hs_color[1] / 100]);
    }
    if ("color_temp_kelvin" in entity.attributes && entity.attributes.color_temp_kelvin !== null) {
      return temperature2rgb(entity.attributes.color_temp_kelvin);
    }
    if ("rgb_color" in entity.attributes && entity.attributes.rgb_color !== null) {
      return entity.attributes.rgb_color;
    }
    if ("rgbw_color" in entity.attributes && entity.attributes.rgbw_color !== null) {
      return rgbw2rgb(entity.attributes.rgbw_color);
    }
    if ("rgbww_color" in entity.attributes && entity.attributes.rgbww_color !== null) {
      return rgbww2rgb(entity.attributes.rgbww_color, entity.attributes.min_color_temp_kelvin, entity.attributes.max_color_temp_kelvin);
    }
  }

  return null;
}

This is the toRGB function on the latest version, i suspect you haven't upgraded the packages :)

@shannonhochkins
Copy link
Owner

It's also great to see that you've been designing something from scratch using the core! How's it been going? Had any other major issues? Would appreciate some feedback :)

@shannonhochkins shannonhochkins added the good first issue Good for newcomers label Nov 18, 2023
@rensknoors
Copy link
Author

What version of @hakit are you using? I just checked my code and i must have run into something similar 2 weeks ago

function toRGB(entity: HassEntity): [number, number, number] | null {
  if (entity.attributes) {
    if ("hs_color" in entity.attributes && entity.attributes.hs_color !== null) {
      return hs2rgb([entity.attributes.hs_color[0], entity.attributes.hs_color[1] / 100]);
    }
    if ("color_temp_kelvin" in entity.attributes && entity.attributes.color_temp_kelvin !== null) {
      return temperature2rgb(entity.attributes.color_temp_kelvin);
    }
    if ("rgb_color" in entity.attributes && entity.attributes.rgb_color !== null) {
      return entity.attributes.rgb_color;
    }
    if ("rgbw_color" in entity.attributes && entity.attributes.rgbw_color !== null) {
      return rgbw2rgb(entity.attributes.rgbw_color);
    }
    if ("rgbww_color" in entity.attributes && entity.attributes.rgbww_color !== null) {
      return rgbww2rgb(entity.attributes.rgbww_color, entity.attributes.min_color_temp_kelvin, entity.attributes.max_color_temp_kelvin);
    }
  }

  return null;
}

This is the toRGB function on the latest version, i suspect you haven't upgraded the packages :)

You're absolutely right, it was fixed after upgrading. I upgraded about 2 weeks ago and then started this issue (but never submitted it because I got distracted) and ran into the same issue again this week. This time I submitted it without checking if there was a new update.

No major issues and even if I encounter one, you apparantly fix them before I can submit a Github issue ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants