Skip to content

Commit

Permalink
Merge pull request #555 from dmamelin/master
Browse files Browse the repository at this point in the history
add virtual attribute StateVal.entity_id
  • Loading branch information
craigbarratt authored Jun 19, 2024
2 parents 9ba1f43 + 6e9029a commit 8302c13
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
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

0 comments on commit 8302c13

Please sign in to comment.