Advertisement
energywave

Home Assistant - set_state python script

Nov 30th, 2020
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #--------------------------------------------------------------------------------------------------
  2. # Set the state or other attributes for the entity specified in the Automation Action
  3. #--------------------------------------------------------------------------------------------------
  4.  
  5. inputEntity = data.get('entity_id')
  6. if inputEntity is None:
  7.     logger.warning("===== entity_id is required if you want to set something.")
  8. elif hass.states.get(inputEntity) is None:
  9.     logger.warning("===== unknown entity_id: %s", inputEntity)
  10. else:
  11.     inputStateObject = hass.states.get(inputEntity)
  12.     inputState = inputStateObject.state
  13.     inputAttributesObject = inputStateObject.attributes.copy()
  14.  
  15.     for item in data:
  16.         newAttribute = data.get(item)
  17.         logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
  18.         if item == 'entity_id':
  19.             continue            # already handled
  20.         elif item == 'state':
  21.             inputState = newAttribute
  22.         else:
  23.             inputAttributesObject[item] = newAttribute
  24.        
  25.     hass.states.set(inputEntity, inputState, inputAttributesObject)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement