parameters: (self, event: Event)
docstring: Track 'state_changed' events.
docstring_summary: Track 'state_changed' events.
docstring_tokens: Track state_changed events .
function: async def state_changed_event_listener(self, event: Event) -> None:
          """Track 'state_changed' events."""
          entity_id = event.data.get(ATTR_ENTITY_ID, "")
          if entity_id not in self.lights or entity_id.split(".")[0] != LIGHT_DOMAIN:
              return
  
          new_state = event.data.get("new_state")
          if new_state is not None and new_state.state == STATE_ON:
              _LOGGER.debug(
                  "Detected a '%s' 'state_changed' event: '%s' with context.id='%s'",
                  entity_id,
                  new_state.attributes,
                  new_state.context.id,
              )
  
          if (
              new_state is not None
              and new_state.state == STATE_ON
              and is_our_context(new_state.context)
          ):
              # It is possible to have multiple state change events with the same context.
              # This can happen because a `turn_on.light(brightness_pct=100, transition=30)`
              # event leads to an instant state change of
              # `new_state=dict(brightness=100, ...)`. However, after polling the light
              # could still only be `new_state=dict(brightness=50, ...)`.
              # We save all events because the first event change might indicate at what
              # settings the light will be later *or* the second event might indicate a
              # final state. The latter case happens for example when a light was
              # called with a color_temp outside of its range (and HA reports the
              # incorrect 'min_mireds' and 'max_mireds', which happens e.g., for
              # Philips Hue White GU10 Bluetooth lights).
              old_state: Optional[List[State]] = self.last_state_change.get(entity_id)
              if (
                  old_state is not None
                  and old_state[0].context.id == new_state.context.id
              ):
                  # If there is already a state change event from this event (with this
                  # context) then append it to the already existing list.
                  _LOGGER.debug(
                      "State change event of '%s' is already in 'self.last_state_change' (%s)"
                      " adding this state also",
                      entity_id,
                      new_state.context.id,
                  )
                  self.last_state_change[entity_id].append(new_state)
              else:
                  self.last_state_change[entity_id] = [new_state]
function_tokens: async def state_changed_event_listener ( self , event : Event ) -> None : entity_id = event . data . get ( ATTR_ENTITY_ID , "" ) if entity_id not in self . lights or entity_id . split ( "." ) [ 0 ] != LIGHT_DOMAIN : return new_state = event . data . get ( "new_state" ) if new_state is not None and new_state . state == STATE_ON : _LOGGER . debug ( "Detected a '%s' 'state_changed' event: '%s' with context.id='%s'" , entity_id , new_state . attributes , new_state . context . id , ) if ( new_state is not None and new_state . state == STATE_ON and is_our_context ( new_state . context ) ) : # It is possible to have multiple state change events with the same context. # This can happen because a `turn_on.light(brightness_pct=100, transition=30)` # event leads to an instant state change of # `new_state=dict(brightness=100, ...)`. However, after polling the light # could still only be `new_state=dict(brightness=50, ...)`. # We save all events because the first event change might indicate at what # settings the light will be later *or* the second event might indicate a # final state. The latter case happens for example when a light was # called with a color_temp outside of its range (and HA reports the # incorrect 'min_mireds' and 'max_mireds', which happens e.g., for # Philips Hue White GU10 Bluetooth lights). old_state : Optional [ List [ State ] ] = self . last_state_change . get ( entity_id ) if ( old_state is not None and old_state [ 0 ] . context . id == new_state . context . id ) : # If there is already a state change event from this event (with this # context) then append it to the already existing list. _LOGGER . debug ( "State change event of '%s' is already in 'self.last_state_change' (%s)" " adding this state also" , entity_id , new_state . context . id , ) self . last_state_change [ entity_id ] . append ( new_state ) else : self . last_state_change [ entity_id ] = [ new_state ]