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

no way to prevent state_set from removes existing attributes #9

Closed
fleXible opened this issue Sep 2, 2020 · 4 comments
Closed

no way to prevent state_set from removes existing attributes #9

fleXible opened this issue Sep 2, 2020 · 4 comments

Comments

@fleXible
Copy link
Contributor

fleXible commented Sep 2, 2020

Hi,

using state.set("domain.entity", "new state") removes existing attributes. It is possible to supply an attribute dict, but there was no way of collecting all existing attributes from a state. state.get() only supported pulling a single attribute by name.

Also state_get() would not return the full State object, but only the state as string or the attribute value.

As a possible way to fix this, I did some changes:

  • Add states object, known from template extensions to be the new way of simple state retrievals
  • Add new method get_new that returns the actual State object and remapped state.get() to it
  • Add new method set_new that preserves existing attributes by default, but also allowes them to be updated or removed

The PR will follow soon.

fleXible added a commit to fleXible-public/pyscript that referenced this issue Sep 2, 2020
@craigbarratt
Copy link
Member

Good suggestions- it would be good to have a clean way of providing those features. This is related to issues #11 and #12.

@craigbarratt
Copy link
Member

I just made a commit 712692f that makes state setting preserve the attributes. state.set() can optionally update or replace attributes. I also added state.get_attr() to get a dict of all attributes for a state variable.

@craigbarratt
Copy link
Member

This is released in 0.21.

If you can suggest additional features or changes, please re-open this issue or create a new one.

@fleXible
Copy link
Contributor Author

fleXible commented Sep 9, 2020

@craigbarratt
In my PR I also fancied the idea to have access to the full Stateobject, maybe to access any of the other available properties like last_changedor context.

And I suggested using the statesobjects from HAs template engine. Even though the way I implemented it, was quite ugly, still it lets you iterate over all states or thos from a specific domain and gives access to the full State object.
I liked it, because it works the same way as in templates, making it easier for people to adopt pyscript.

What do you think?

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

No branches or pull requests

2 participants