Skip to content

add virtual attribute StateVal.entity_id #555

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

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,8 +1310,8 @@ async def ast_with(self, arg, async_attr=""):
}
)
for ctx in ctx_list:
value = await self.call_func(ctx["enter"], enter_attr, ctx["manager"])
if ctx["target"]:
value = await self.call_func(ctx["enter"], enter_attr, ctx["manager"])
await self.recurse_assign(ctx["target"], value)
for arg1 in arg.body:
val = await self.aeval(arg1)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/pyscript/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

_LOGGER = logging.getLogger(LOGGER_PATH + ".state")

STATE_VIRTUAL_ATTRS = {"last_changed", "last_updated"}
STATE_VIRTUAL_ATTRS = {"entity_id", "last_changed", "last_updated"}


class StateVal(str):
Expand All @@ -23,6 +23,7 @@ def __new__(cls, state):
"""Create a new instance given a state variable."""
new_var = super().__new__(cls, state.state)
new_var.__dict__ = state.attributes.copy()
new_var.entity_id = state.entity_id
new_var.last_updated = state.last_updated
new_var.last_changed = state.last_changed
return new_var
Expand Down
3 changes: 2 additions & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ Here's an example using ``input_number``, assuming it has been configured to cre
input_number.test.set_value(value=13)
input_number.test.set_value(13)

Two additional virtual attribute values are available when you use a variable directly as
Three additional virtual attribute values are available when you use a variable directly as
``DOMAIN.entity.attr`` or call ``state.get("DOMAIN.entity.attr")``:

- ``entity_id`` is the DOMAIN.entity as string
- ``last_changed`` is the last UTC time the state value was changed (not the attributes)
- ``last_updated`` is the last UTC time the state entity was updated

Expand Down