Skip to content

Entity.is_on(), Entity.is_state() don't work in async callbacks #2671

Description

@canton7

What happened?

It looks like Entity.is_on() and Entity.is_state() don't work correctly in async callbacks.

def initialize():
    self._entity = self.get_entity("...")
    self._entity.listen_state(self._state_changed)

async def _state_changed(self, entity: str, attribute: str, old: Any, new: Any, **kwargs: Any) -> None:
    self.log(f"State: {self._entity.state}; is_state: {self._entity.is_state('on')}; is_on: {self._entity.is_on()}")

Outputs:

State: on; is_state: False; is_on: False

I suspect this is because is_state calls entity_state = self.get_state(copy=False). get_state returns a Coroutine in async contexts, but this isn't awaited by is_state.

Version

b8b06ba

Installation type

Python virtual environment

Relevant log output

Relevant code in the app or config file that caused the issue

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    issueSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions