custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

add virtual attribute StateVal.entity_id #555

Closed dmamelin closed 3 months ago

dmamelin commented 10 months ago

Modifying StateVal according to homeassistant.core.State. Useful in complex logic: no need to store and pass entity_id. Simplified example:

def func(state):
    num_seconds_ago = (dt.now(tz=timezone.utc) - state.last_changed).total_seconds()
    if num_seconds_ago > 10:
        light.toggle(entity_id=state.entity_id)
        log.info(f"toggle {state.entity_id}")
ALERTua commented 8 months ago

@craigbarratt, ping :)

dmamelin commented 8 months ago

ooops... I wanted to make a separate PR for #581, but forgot about this PR and when I pushed to my fork, the changes were added here.

craigbarratt commented 3 months ago

Thanks for the PR. Sorry about the delay in following up.