Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to dynamically create a group? #278

Answered by dlashua
wsw70 asked this question in General
Discussion options

Instead of updating configuration.yaml with an ever-growing set of groups I was wondering whether it would be possible to do it from within pyscript?

If so, when a script starts, it would create/recreate/update the groups relevant to its operations.

You must be logged in to vote

It is possible (I think, though I've not actually tried it with group, however, it works fine in other domains) to create a group from pyscript though it may be more complicated than it is worth.

Once a group is created, you can change its entities using the group.set service call.

With that being said, if your intended use case of these groups is all within Pyscript, you don't actually need a Home Assistant Group to do this. You could create a variable in pyscript that maintained the list of entities that should be in the group and refer to the variable whenever you needed to access them.

Replies: 1 comment 3 replies

Comment options

It is possible (I think, though I've not actually tried it with group, however, it works fine in other domains) to create a group from pyscript though it may be more complicated than it is worth.

Once a group is created, you can change its entities using the group.set service call.

With that being said, if your intended use case of these groups is all within Pyscript, you don't actually need a Home Assistant Group to do this. You could create a variable in pyscript that maintained the list of entities that should be in the group and refer to the variable whenever you needed to access them.

You must be logged in to vote
3 replies
Comment options

It is possible (I think, though I've not actually tried it with group, however, it works fine in other domains) to create a group from pyscript though it may be more complicated than it is worth.

Would you have some feedback about why it is difficult? (I do not know where to start for now :))

You could create a variable in pyscript that maintained the list of entities

I already do that for "groups" I use within the script. I would need a real group, though, to access it via Lovelace.

Comment options

Would you have some feedback about why it is difficult?

The trouble is that "groups" do things other than collect a list of entities. A group of binary_sensors is "on" when any of its sensors is "on" and "off" otherwise. The group component takes care of all of this logic.

So your options are:

  1. Have "dumb" groups that are just a collection of entities with out a dynamic state of their own. For this, I think you can just use state.set() in pyscript, though there may be nuances that I'm not aware of.
  2. Have your pyscript code actually create configuration.yaml for these groups and then call the reload service after an update. If you go this route, you probably want to split the pyscript generated groups into their own config file and add the proper YAML in configuration.yaml to read from that file so you don't have to deal with overwriting manually defined groups and config. You also need to make sure you don't call reload until all group changes are made. Which means you'll need one pyscript to handle all the groups, or you'll have to get really clever with how you link it all together.
  3. The difficult way. Use the hass variable to access the entity registry. Create "group" classes that implement all the logic for handling state and accepting service calls for that group. Instantiate the class for every group you want to create. Load those objects into the entity registry. Etc, etc. You're basically writing an integration at this point.
  4. There is probably a way to instantiate the existing, native Home Assistant group classes. You'll have to dig through the group source to figure out how they work and perform many of the same steps as option 3. You'll also have to deal with your code breaking if Home Assistant changes how the existing group classes work. But, you'll avoid having to reimplement or copy/paste the existing group logic for your pyscript versions of the same thing.
Comment options

Thank you for the detailed answer.

I was thinking about that in the meantime and will probably end with solution 2 I also had in mind. Probably with a lock to ensure synchronization.
I will see how it goes, thanks a lot as usual!

Answer selected by wsw70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /