bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
937 stars 250 forks source link

Grouping entities with group or area. #358

Closed Julien-Decoen closed 2 years ago

Julien-Decoen commented 2 years ago

Hi,

Thanks a lot for your job, it's really impressive :)

It could be a good idear to group entities with group or area (today, le HUE intégration create an area with all lights in philips HUE group).

sensor:
  - platform: powercalc
    create_group: All hallway lights
    target : 
        group: group.allway_lights

or

sensor:
  - platform: powercalc
    create_group: All hallway lights
    target : 
        area: Hallway lights

In my case, I already group my room's lights with HA group integration. I'm fully use Philips HUE light.

Thanks for your time. Juicy

bramstroker commented 2 years ago

It's a cool idea, but not sure if it is possible at all.

The biggest issue with this approach is we cannot be certain every underlying entity can be setup and is supported for powercalc without any further configuration. These models are supported for LUT mode (so without any further user configuration) https://github.com/bramstroker/homeassistant-powercalc/blob/master/docs/supported_models.md.

Imagine you have a group or area with 6 lights, but only 3 of them are supported. The other 3 will create an error in the logs and power sensors will not be setup for those.

Julien-Decoen commented 2 years ago

As you already add documentation on supported models, it do not be a problems to return only supported models' data for my part.

And if you want to be more flexible, maybe we can imagine filters to add some specific configuration :

sensor:
  - platform: powercalc
    create_group: All hallway lights
    target : 
        area: Hallway lights
    filter:
      'light.nondimmabled_*':
        fixed:
          power: 20
      'climate.*': 
        fixed:
          power: 1000

If it's possible of course ! :)

bramstroker commented 2 years ago

Will give it a thought the coming days. Don't want to support too complex configuration, because it also need to be maintained long term and documented etc.

Julien-Decoen commented 2 years ago

I understand, no problem ! :) You already make a huuuuge work on it thanks a lot ! I'll only try to give you some input to improve it. Keep us in touch ;)

bramstroker commented 2 years ago

Already started some work on it #366. However a lot of things need to be refactored (changed) and lots of testing needs to be done before releasing this to public. Right now it's a work in progress. Will takes some days / weeks before I have this finished. If you are able to test when I release a beta version that would be great.

I planned to implement the functionality to skip the entities which can not be setup automatically. But it could be mixed with the entities configuration, something like this:

sensor:
  - platform: powercalc
    create_group: All hallway lights
    target : 
      area: Hallway lights
    entities:
      entity_id: special_light
      fixed:
        power: 1000

Not sure about target yet, maybe include is better. Or something else..

Julien-Decoen commented 2 years ago

Good news :) I'll be happy to test it ! I'm talking about "target" to be as close as possible to the service call logic.

bramstroker commented 2 years ago

For service calls it is a logical naming because it states which devices you want to "target" the call to.

On our use case you configure which entities you want to include in the group. So I think include will be better. But naming things is one of the most difficult things in software development ;-).

bramstroker commented 2 years ago

Got it working for area's. However struggling for a few hours with groups already. Seems like a bug in HA because I'm unable to retrieve the underlying child entity_id's of a group during setup of powercalc. I have asked on dev channel of the HA discord, hopefully someone has an idea to resolve.

Julien-Decoen commented 2 years ago

Good job ! :)

bramstroker commented 2 years ago

I'm afraid I cannot implement the idea for group. I don't have access to the child entity_id's of the group during setup of powercalc. Questions on discord did not get answered, and I digged a lot through the code to find a workaround or some way to do it, however it is just not possible.

The child entities are available in the attributes of the group, you can also see those on developer tools. However these are only set after Home Assistant has started. And that's too late, because powercalc needs to setup sensors during startup fase of home assistant.

Julien-Decoen commented 2 years ago

Hummm bad news ! But area will be already amazing ! Lets see later for groups :)

bramstroker commented 2 years ago

Just released v0.12.0-beta.1 which contains this functionality. Are you able to test?

You should be able to do the following now:

sensor:
  - platform: powercalc
    create_group: All hallway lights
    include:
      area: Hallway lights
Julien-Decoen commented 2 years ago

Unfortunately, I've got an issue with the integration but not related with your beta.

Philips hue make a long time to start and show this warning : Config entry 'Philips hue' for hue integration not ready yet: Error connecting to the Hue bridge at 192.168.1.11; Retrying in background

A second after the powercalc integration failed for hue sensor only with this Traceback :

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/powercalc/sensor.py", line 155, in async_setup_platform
    await create_individual_sensors(hass, merged_sensor_config)
  File "/config/custom_components/powercalc/sensor.py", line 251, in create_individual_sensors
    power_sensor = await create_power_sensor(hass, sensor_config, source_entity)
  File "/config/custom_components/powercalc/sensors/power.py", line 90, in create_power_sensor
    light_model = await get_light_model(hass, source_entity, sensor_config)
  File "/config/custom_components/powercalc/model_discovery.py", line 34, in get_light_model
    model_info = await autodiscover_model(hass, source_entity)
  File "/config/custom_components/powercalc/model_discovery.py", line 85, in autodiscover_model
    model_info = await autodiscover_from_hue_bridge(hass, entity_entry)
  File "/config/custom_components/powercalc/model_discovery.py", line 102, in autodiscover_from_hue_bridge
    light = await find_hue_light(hass, entity_entry)
  File "/config/custom_components/powercalc/model_discovery.py", line 118, in find_hue_light
    bridge = hass.data["hue"][entity_entry.config_entry_id]
KeyError: 'hue'

Have you ever seen something like this ?

bramstroker commented 2 years ago

This is because the Hue integration was not loaded correctly. I have added an extra check so powercalc just continue loading when this is the case and doesn't crash. v0.12.0-beta.3

Julien-Decoen commented 2 years ago

Thanks for the release ! :D Very happy to test it !

I tried to use this configuration :

- platform: powercalc
  create_group: Lumières du jardin Area
  include:
    area: Jardin

- platform: powercalc
  create_group: Lumières du jardin Area 2
  include:
    area: 78d8070d00ef41fca22aa39ccecdb058

- platform: powercalc
  create_group: Lumières de la véranda
  entities:
    - entity_id: light.entree
    - entity_id: light.chill

1/ I've got an error with the first area : No area with id 'Jardin' found in your HA instance.

2/ The second one with the id works but it takes all domains entities like input_select or other and return an error : input_select.lumieres_jardin: Cannot autodiscover model, manufacturer or model unknown from device registry

3/ When the integration trying to autodiscover, Philips Hue integration was not ready yet and return the same previous error, then it skip the entity and return en error for the group : Could not create group Lumières de la véranda, no entities resolved

4/ The anchor on the readme related to Area include was wrong : #include-are-entities instead of #include-area-entities.

5/ There is a duplicate example on the readme related to Include area entities

bramstroker commented 2 years ago

Thanks for the release ! :D Very happy to test it !

Thanks for testing!

1/ I've got an error with the first area : No area with id 'Jardin' found in your HA instance.

I have added support to also specify an area name, not only an ID. So this error should be resolved now.

2/ The second one with the id works but it takes all domains entities like input_select or other and return an error : input_select.lumieres_jardin: Cannot autodiscover model, manufacturer or model unknown from device registry

I have changed the code to only consider lights.

3/ When the integration trying to autodiscover, Philips Hue integration was not ready yet and return the same previous error, then it skip the entity and return en error for the group : Could not create group Lumières de la véranda, no entities resolved

This is not something I can fix. Because the Hue integration was not loaded correctly (or in time), powercalc has no way to know the manufacturer / model for the given entities. So ultimately no entities from the area could be discovered, and the area can therefore not be setup (because there are no entities). So you need to have a look into your issues with the Hue API. Maybe it's network related? Reset the bridge? etc.

4/ The anchor on the readme related to Area include was wrong : #include-are-entities instead of #include-area-entities.

Fixed

5/ There is a duplicate example on the readme related to Include area entities

Fixed

Julien-Decoen commented 2 years ago

I have added support to also specify an area name, not only an ID. So this error should be resolved now.

Works great :)

I have changed the code to only consider lights.

Works great :)

This is not something I can fix. Because the Hue integration was not loaded correctly (or in time), powercalc has no way to know the manufacturer / model for the given entities. So ultimately no entities from the area could be discovered, and the area can therefore not be setup (because there are no entities). So you need to have a look into your issues with the Hue API. Maybe it's network related? Reset the bridge? etc.

I've already try to reset my Philips Hue integration but I've got 44 devices on my bridge with custom name, so I can't reset my bridge... Is there a way to retry few times after a delay in case of autodiscover failed ? Or a service to start again startup ? :)

bramstroker commented 2 years ago

I've already try to reset my Philips Hue integration but I've got 44 devices on my bridge with custom name, so I can't reset my bridge... Is there a way to retry few times after a delay in case of autodiscover failed ? Or a service to start again startup ? :)

No I'm afraid not. The loading of components is not something I can control. My component is dependent on the hue component. This is all handled by HA core code. Normally powercalc waits for hue component to load fully and than start loading. There is some timeout in HA which starts loading powercalc anyway when it takes too long. Otherwise whole startup of HA would freeze.

By reset hue bridge, I meant restart the hue bridge (powercycle). Maybe you could enable debug logging for the hue component to see if this can give you some pointers. Else you would be better of asking for help on the HA forum of discord, as there are more people to help. As this issue is also not directly related to powercalc.

bramstroker commented 2 years ago

Has been released in version 0.12

bramstroker commented 2 years ago

I have good news. Managed to add support for groups and light groups for the include functionality. Has been merged into master. Will be in release v0.14

Julien-Decoen commented 2 years ago

Good job dude ! Looks good :)