-
Notifications
You must be signed in to change notification settings - Fork 58
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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_sensor
s 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:
- 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. - 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.
- 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. - 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 existinggroup
classes work. But, you'll avoid having to reimplement or copy/paste the existing group logic for your pyscript versions of the same thing.
Beta Was this translation helpful? Give feedback.
All reactions
-
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!
Beta Was this translation helpful? Give feedback.