shannonhochkins/ha-component-kit

useEntity not working for light entities that are turned off

Closed this issue ยท 6 comments

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.

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!

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

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.

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 :)

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 :)

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 ;)