Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #
  2. # Script to manage a light using standard controls from a Lutron Pico Remote
  3. # This will translate the standard button meanings on a Pico to any light controlled
  4. # by Home Assistant.
  5. # The "favourite" button is presumed to be 50% brightness (but can be overriden).
  6. # Expected data packet is: { new_state: X, entity_id: entity_id, fav_brightness: [0-100] }
  7. # new_state: REQUIRED. The pico value that triggered the event.
  8. # entity_id: REQUIRED. The entity_id of the light to control
  9. # fav_brightness: OPTIONAL. What brightness percentage to set when the favourite button is pressed (int 1 - 100).
  10. #
  11. # Example call from automations.yaml:
  12. # trigger:
  13. # - platform: state
  14. # entity_id: sensor.PICO_REMOTE_ID
  15. # from: '0'
  16. # action:
  17. # - data_template:
  18. # entity_id: light.LIGHT_TO_CONTROL_ID
  19. # new_state: "{{ trigger.to_state.state }}"
  20. # service: python_script.pico_light_switch_std
  21. #
  22. # Note: In order to work best with LIFX bulbs, pressing ON has two modes. If the bulb is not already on, then
  23. # ON will turn on the light at the previously set brightness. However, if the light is already on, then
  24. # ON will set brightness to maximum (255)
  25. #
  26.  
  27. # Function Definitions
  28. def get_fav_brightness(data):
  29. try:
  30. fav_percent = int(data.get('fav_brightness', 50))
  31. return int(255 * fav_percent / 100)
  32. except ValueError:
  33. return 50
  34.  
  35. # Init
  36. BUTTON_ON = 1
  37. BUTTON_FAV = 2
  38. BUTTON_OFF = 4
  39. BUTTON_RAISE = 8
  40. BUTTON_LOWER = 16
  41. button_values = [BUTTON_ON, BUTTON_FAV, BUTTON_OFF, BUTTON_RAISE, BUTTON_LOWER]
  42. BRIGHTNESS_STEP = 5 # Brightness value to step up/down
  43. BRIGHTNESS_MAX = 255
  44.  
  45. entity_id = data.get('entity_id')
  46. button = int(data.get('new_state'))
  47. brightness = None
  48. action = 'turn_on' # Most buttons require turning on
  49. action_data = { "entity_id" : entity_id }
  50.  
  51. # Validation
  52. valid = True
  53. if entity_id is None:
  54. logger.warning("No entity ID provided to pico_light_switch_std script")
  55. valid = False
  56. if button is None or button not in button_values:
  57. logger.warning("Invalid or missing new_state value provided to pico_light_switch_std script")
  58. valid = False
  59.  
  60. if valid:
  61. try:
  62. cur_entity_state = hass.states.get(entity_id)
  63. cur_brightness = cur_entity_state.attributes['brightness']
  64. except KeyError:
  65. cur_brightness = 0
  66.  
  67. logger.debug("Payload received: {}".format(data))
  68. logger.debug("Current brightness: {}".format(cur_brightness))
  69.  
  70. # Build Service Call Payload
  71. if button == BUTTON_OFF:
  72. action = 'turn_off'
  73. elif button == BUTTON_RAISE:
  74. brightness = cur_brightness + BRIGHTNESS_STEP
  75. if brightness > 255: brightness = 255 # sigh: apparantly min/max fn's are not considered "safe"
  76. elif button == BUTTON_LOWER:
  77. brightness = cur_brightness - BRIGHTNESS_STEP
  78. if brightness < 0: brightness = 0
  79. elif button == BUTTON_FAV:
  80. brightness = get_fav_brightness(data)
  81. elif button == BUTTON_ON:
  82. if cur_entity_state.state == 'on':
  83. brightness = BRIGHTNESS_MAX
  84.  
  85. if brightness is not None:
  86. action_data['brightness'] = brightness
  87.  
  88. # Make service call to light
  89. logger.info("Making '%s' call with payload: %s", action, action_data)
  90. hass.services.call('light', action, action_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement