blueprint:
  name: Hook - Light
  description: "# Hook - Light\n\nHook automation which allows to control a light.
    Supports brightness and color control both for white temperature and rgb lights.
    Features may vary depending on the selected Controller.\n\nAn automation created
    with this blueprint must be linked to a [Controller](https://epmatt.github.io/awesome-ha-blueprints/blueprints/controllers)
    automation.\nControllers are blueprints which allow to easily integrate a wide
    range of controllers (wall switches, remotes, dimmers, etc.) and use them to run
    a set of actions when interacting with them.\nThey expose an abstract interface
    used by Hooks to create controller-based automations.\n\nA list of controllers
    supported by this hook is available [here](https://epmatt.github.io/awesome-ha-blueprints/docs/blueprints/hooks/light#supported-controllers).\n\n\U0001F4D5
    Full documentation regarding this blueprint is available [here](https://epmatt.github.io/awesome-ha-blueprints/docs/blueprints/hooks/light).\n\n\U0001F680
    This blueprint is part of the **[Awesome HA Blueprints](https://epmatt.github.io/awesome-ha-blueprints)
    project**.\n\nℹ️ Version 2022.07.30\n"
  source_url: https://raw.githubusercontent.com/EPMatt/awesome-ha-blueprints/main/blueprints/hooks/light/light.yaml
  domain: automation
  input:
    controller_device:
      name: (Required) Controller Device
      description: The controller device which will control the Light. Choose a value
        only if the integration used to connect the controller to Home Assistant exposes
        it as a Device. This value should match the one specified in the corresponding
        Controller automation.
      default: ''
      selector:
        device: {}
    controller_entity:
      name: (Required) Controller Entity
      description: The controller entity which will control the Light. Choose a value
        only if the integration used to connect the controller to Home Assistant exposes
        it as an Entity. This value should match the one specified in the corresponding
        Controller automation.
      default: ''
      selector:
        entity:
          domain:
          - sensor
          multiple: false
    controller_model:
      name: (Required) Controller model
      description: The model for the controller used in this automation. Choose a
        value from the list of supported controllers.
      selector:
        select:
          options:
          - IKEA E1524/E1810 TRÅDFRI Wireless 5-Button Remote
          - IKEA E1743 TRÅDFRI On/Off Switch & Dimmer
          - IKEA E1743 TRÅDFRI On/Off Switch & Dimmer (#2)
          - IKEA E1744 SYMFONISK Rotary Remote
          - IKEA E1766 TRÅDFRI Open/Close Remote
          - IKEA E1766 TRÅDFRI Open/Close Remote (#2)
          - IKEA E1812 TRÅDFRI Shortcut button
          - IKEA E2001/E2002 STYRBAR Remote control
          - IKEA E2001/E2002 STYRBAR Remote control (#2)
          - IKEA ICTC-G-1 TRÅDFRI wireless dimmer
          - OSRAM AC025XX00NJ SMART+ Switch Mini
          - Philips 324131092621 Hue Dimmer switch
          - Philips 8718699693985 Hue Smart Button
          - Philips 929002398602 Hue Dimmer switch v2
          - SONOFF SNZB-01 Wireless Switch
          - Xiaomi WXCJKG11LM Aqara Opple 2 button remote
          - Xiaomi WXCJKG12LM Aqara Opple 4 button remote
          - Xiaomi WXCJKG12LM Aqara Opple 4 button remote (#2)
          - Xiaomi WXCJKG13LM Aqara Opple 6 button remote
          - Xiaomi WXCJKG13LM Aqara Opple 6 button remote (#2)
          - Xiaomi WXCJKG13LM Aqara Opple 6 button remote (#3)
          - Xiaomi WXKG11LM Aqara Wireless Switch Mini
          sort: false
          custom_value: false
          multiple: false
    light:
      name: (Required) Light
      description: Light which will be controlled with this automation.
      selector:
        entity:
          domain:
          - light
          multiple: false
    light_color_mode:
      name: (Optional) Light color mode
      description: Specify how the controller will set the light color. Choose "Color
        Temperature" and "Hue - Saturation" depending on the features supported by
        your light. If you are not sure you can select "Auto". "None" will disable
        color control features.
      default: Auto
      selector:
        select:
          options:
          - Auto
          - Color Temperature
          - Hue - Saturation
          - None
          sort: false
          custom_value: false
          multiple: false
    light_transition:
      name: (Optional) Light Transition
      description: Number that represents the time (in milliseconds) the light should
        take turn on or off, if the light supports it.
      default: 250
      selector:
        number:
          min: 0.0
          max: 60000.0
          step: 1.0
          unit_of_measurement: milliseconds
          mode: box
    min_brightness:
      name: (Optional) Light minimum brightness
      description: The minimum brightness the light can be set with this automation.
      default: 1
      selector:
        number:
          min: 1.0
          max: 255.0
          step: 1.0
          unit_of_measurement: brightness
          mode: slider
    max_brightness:
      name: (Optional) Light maximum brightness
      description: The maximum brightness the light can be set with this automation.
      default: 255
      selector:
        number:
          min: 0.0
          max: 255.0
          step: 1.0
          unit_of_measurement: brightness
          mode: slider
    brightness_steps_short:
      name: (Optional) Light brightness steps - short actions
      description: Number of steps from min to max brightness when controlling brightness
        with short actions (eg. button press).
      default: 10
      selector:
        number:
          min: 1.0
          max: 255.0
          step: 1.0
          unit_of_measurement: steps
          mode: box
    brightness_steps_long:
      name: (Optional) Light brightness steps - long actions
      description: Number of steps from min to max brightness when controlling brightness
        with long actions (eg. button hold or controller rotation).
      default: 10
      selector:
        number:
          min: 1.0
          max: 255.0
          step: 1.0
          unit_of_measurement: steps
          mode: box
    force_brightness:
      name: (Optional) Force brightness value at turn on
      description: Force brightness to the "On brightness" input value, when the light
        is being turned on.
      default: false
      selector:
        boolean: {}
    on_brightness:
      name: (Optional) On brightness
      description: Brightness value to force when turning on the light
      default: 1
      selector:
        number:
          min: 0.0
          max: 255.0
          step: 1.0
          unit_of_measurement: brightness
          mode: slider
    smooth_power_on:
      name: (Optional) Smooth power on
      description: Force the light to turn on at minimum brightness when a brightness
        up command (single or continuous) is triggered and light is off.
      default: true
      selector:
        boolean: {}
    smooth_power_off:
      name: (Optional) Smooth power off
      description: Allow a brightness down command (single or continuous) to turn
        off the light when at minimum brightness. Disabling this will prevent the
        light from being turned off by brightness down commands.
      default: true
      selector:
        boolean: {}
variables:
  controller_model: !input controller_model
  controller_mapping:
    IKEA E1524/E1810 TRÅDFRI Wireless 5-Button Remote:
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
      color_down: button_left_short
      color_down_repeat: button_left_long
      color_up: button_right_short
      color_up_repeat: button_right_long
      toggle: button_center_short
    IKEA E1743 TRÅDFRI On/Off Switch & Dimmer:
      turn_on: button_up_short
      brightness_up_repeat: button_up_long
      color_up: button_up_double
      turn_off: button_down_short
      brightness_down_repeat: button_down_long
      color_down: button_down_double
    IKEA E1743 TRÅDFRI On/Off Switch & Dimmer (#2):
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      turn_on: button_up_double
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
      turn_off: button_down_double
    IKEA E1744 SYMFONISK Rotary Remote:
      brightness_down_repeat: rotate_left
      brightness_up_repeat: rotate_right
      toggle: click_short
      color_up: click_double
      color_down: click_triple
    IKEA E1766 TRÅDFRI Open/Close Remote:
      brightness_up: button_up_short
      turn_on: button_up_double
      brightness_down: button_down_short
      turn_off: button_down_double
    IKEA E1766 TRÅDFRI Open/Close Remote (#2):
      brightness_up: button_up_short
      color_up: button_up_double
      brightness_down: button_down_short
      color_down: button_down_double
    IKEA E1812 TRÅDFRI Shortcut button:
      toggle: button_short
      color_up: button_double
    IKEA E2001/E2002 STYRBAR Remote control:
      turn_on: button_up_short
      brightness_up_repeat: button_up_long
      turn_off: button_down_short
      brightness_down_repeat: button_down_long
      color_down: button_left_short
      color_down_repeat: button_left_long
      color_up: button_right_short
      color_up_repeat: button_right_long
    IKEA E2001/E2002 STYRBAR Remote control (#2):
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      turn_on: button_up_double
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
      turn_off: button_down_double
      color_down: button_left_short
      color_down_repeat: button_left_long
      color_up: button_right_short
      color_up_repeat: button_right_long
    IKEA ICTC-G-1 TRÅDFRI wireless dimmer:
      brightness_down_repeat: rotate_left
      brightness_up_repeat: rotate_right
    OSRAM AC025XX00NJ SMART+ Switch Mini:
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      color_up: button_up_double
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
      color_down: button_down_double
      toggle: button_center_short
    Philips 324131092621 Hue Dimmer switch:
      turn_on: button_on_short
      color_up_repeat: button_on_long
      turn_off: button_off_short
      color_down_repeat: button_off_long
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
    Philips 8718699693985 Hue Smart Button:
      toggle: button_short
    Philips 929002398602 Hue Dimmer switch v2:
      turn_on: button_on_short
      color_up_repeat: button_on_long
      turn_off: button_off_short
      color_down_repeat: button_off_long
      brightness_up: button_up_short
      brightness_up_repeat: button_up_long
      brightness_down: button_down_short
      brightness_down_repeat: button_down_long
    SONOFF SNZB-01 Wireless Switch:
      toggle: button_short
      color_up: button_double
    Xiaomi WXCJKG11LM Aqara Opple 2 button remote:
      turn_on: button_1_short
      brightness_up_repeat: button_1_long
      color_up: button_1_double
      turn_off: button_2_short
      brightness_down_repeat: button_2_long
      color_down: button_2_double
    Xiaomi WXCJKG12LM Aqara Opple 4 button remote:
      turn_on: button_1_short
      brightness_up_repeat: button_1_long
      color_up: button_1_double
      turn_off: button_2_short
      brightness_down_repeat: button_2_long
      color_down: button_2_double
    Xiaomi WXCJKG12LM Aqara Opple 4 button remote (#2):
      turn_on: button_3_short
      brightness_up_repeat: button_3_long
      color_up: button_3_double
      turn_off: button_4_short
      brightness_down_repeat: button_4_long
      color_down: button_4_double
    Xiaomi WXCJKG13LM Aqara Opple 6 button remote:
      turn_on: button_1_short
      brightness_up_repeat: button_1_long
      color_up: button_1_double
      turn_off: button_2_short
      brightness_down_repeat: button_2_long
      color_down: button_2_double
    Xiaomi WXCJKG13LM Aqara Opple 6 button remote (#2):
      turn_on: button_3_short
      brightness_up_repeat: button_3_long
      color_up: button_3_double
      turn_off: button_4_short
      brightness_down_repeat: button_4_long
      color_down: button_4_double
    Xiaomi WXCJKG13LM Aqara Opple 6 button remote (#3):
      turn_on: button_5_short
      brightness_up_repeat: button_5_long
      color_up: button_5_double
      turn_off: button_6_short
      brightness_down_repeat: button_6_long
      color_down: button_6_double
    Xiaomi WXKG11LM Aqara Wireless Switch Mini:
      toggle: button_short
      color_up: button_double
  brightness_up: '{{ controller_mapping[controller_model]["brightness_up"] | default(None)
    }}'
  brightness_up_repeat: '{{ controller_mapping[controller_model]["brightness_up_repeat"]
    | default(None) }}'
  brightness_down: '{{ controller_mapping[controller_model]["brightness_down"] | default(None)
    }}'
  brightness_down_repeat: '{{ controller_mapping[controller_model]["brightness_down_repeat"]
    | default(None) }}'
  color_up: '{{ controller_mapping[controller_model]["color_up"] | default(None) }}'
  color_up_repeat: '{{ controller_mapping[controller_model]["color_up_repeat"] | default(None)
    }}'
  color_down: '{{ controller_mapping[controller_model]["color_down"] | default(None)
    }}'
  color_down_repeat: '{{ controller_mapping[controller_model]["color_down_repeat"]
    | default(None) }}'
  toggle: '{{ controller_mapping[controller_model]["toggle"] | default(None) }}'
  turn_on: '{{ controller_mapping[controller_model]["turn_on"] | default(None) }}'
  turn_off: '{{ controller_mapping[controller_model]["turn_off"] | default(None) }}'
  light_color_mode: !input light_color_mode
  light: !input light
  light_transition: !input light_transition
  min_brightness: !input min_brightness
  max_brightness: !input max_brightness
  brightness_steps_short: !input brightness_steps_short
  brightness_steps_long: !input brightness_steps_long
  force_brightness: !input force_brightness
  on_brightness: !input on_brightness
  smooth_power_on: !input smooth_power_on
  smooth_power_off: !input smooth_power_off
  color_modes:
    Auto: auto
    Color Temperature: color_temp
    Hue - Saturation: hs_color
    None: none
  light_color_mode_id: '{%- if light_color_mode == "Auto" -%} {% set supported_color_modes
    = state_attr(light, "supported_color_modes") -%} {%- if "hs" in supported_color_modes
    or "xy" in supported_color_modes or "rgbw" in supported_color_modes or "rgbww"
    in supported_color_modes -%} {{ color_modes["Hue - Saturation"] }} {%- elif "color_temp"
    in supported_color_modes -%} {{ color_modes["Color Temperature"] }} {%- else -%}
    {{ color_modes["None"] }} {%- endif -%} {%- else -%} {{ color_modes[light_color_mode]
    }} {%- endif -%}'
  step_short: '{{ (max_brightness-min_brightness)/brightness_steps_short }}'
  step_long: '{{ (max_brightness-min_brightness)/brightness_steps_long }}'
mode: restart
max_exceeded: silent
trigger:
- platform: event
  event_type: ahb_controller_event
  event_data:
    controller: !input controller_device
- platform: event
  event_type: ahb_controller_event
  event_data:
    controller: !input controller_entity
condition: []
action:
- variables:
    action: '{{ trigger.event.data.action }}'
- choose:
  - conditions: '{{ action == toggle }}'
    sequence:
    - choose:
      - conditions: '{{ force_brightness }}'
        sequence:
        - service: light.toggle
          entity_id: !input light
          data:
            brightness: !input on_brightness
            transition: '{{ light_transition / 1000 }}'
      default:
      - service: light.toggle
        entity_id: !input light
        data:
          transition: '{{ light_transition / 1000 }}'
  - conditions: '{{ action == turn_on }}'
    sequence:
    - choose:
      - conditions: '{{ force_brightness }}'
        sequence:
        - service: light.turn_on
          entity_id: !input light
          data:
            brightness: !input on_brightness
            transition: '{{ light_transition / 1000 }}'
      default:
      - service: light.turn_on
        entity_id: !input light
        data:
          transition: '{{ light_transition / 1000 }}'
  - conditions: '{{ action == turn_off }}'
    sequence:
    - service: light.turn_off
      entity_id: !input light
      data:
        transition: '{{ light_transition / 1000 }}'
  - conditions: '{{ action == brightness_up }}'
    sequence:
    - choose:
      - conditions: '{{ states(light) == "off" and not smooth_power_on}}'
        sequence:
        - service: light.turn_on
          data:
            transition: '{{ light_transition / 1000 }}'
            entity_id: !input light
      - conditions: '{{ states(light) == "off" and smooth_power_on}}'
        sequence:
        - service: light.turn_on
          data:
            brightness: '{{ min_brightness }}'
            transition: '{{ light_transition / 1000 }}'
            entity_id: !input light
      default:
      - service: light.turn_on
        data:
          brightness: '{{ [ [state_attr(light,"brightness")+step_short, min_brightness]
            | max, max_brightness] | min }}'
          transition: 0.25
          entity_id: !input light
  - conditions: '{{ action == brightness_down and states(light) == "on" }}'
    sequence:
    - choose:
      - conditions: '{{ smooth_power_off and state_attr(light,"brightness") == min_brightness
          }}'
        sequence:
        - service: light.turn_off
          data:
            transition: '{{ light_transition / 1000 }}'
            entity_id: !input light
      default:
      - service: light.turn_on
        data:
          brightness: '{{ [ [state_attr(light,"brightness")-step_short, min_brightness]
            | max, max_brightness] | min }}'
          transition: 0.25
          entity_id: !input light
  - conditions: '{{ action == brightness_up_repeat }}'
    sequence:
    - choose:
      - conditions: '{{ states(light) == "off" and not smooth_power_on}}'
        sequence:
        - service: light.turn_on
          data:
            transition: '{{ light_transition / 1000 }}'
            entity_id: !input light
        - delay:
            milliseconds: !input light_transition
      - conditions: '{{ states(light) == "off" and smooth_power_on}}'
        sequence:
        - service: light.turn_on
          data:
            brightness: '{{ min_brightness }}'
            transition: '{{ light_transition / 1000 }}'
            entity_id: !input light
        - delay:
            milliseconds: !input light_transition
    - repeat:
        while: '{{ true }}'
        sequence:
        - service: light.turn_on
          data:
            brightness: '{{ [ [state_attr(light,"brightness")+step_long, min_brightness]
              | max, max_brightness] | min }}'
            transition: 0.25
            entity_id: !input light
        - delay:
            milliseconds: 250
  - conditions: '{{ action == brightness_down_repeat and states(light) == "on"  }}'
    sequence:
    - choose:
      - conditions: '{{ smooth_power_off }}'
        sequence:
        - repeat:
            while: '{{ states(light) != "off" }}'
            sequence:
            - choose:
              - conditions: '{{ state_attr(light,"brightness") == min_brightness }}'
                sequence:
                - service: light.turn_off
                  data:
                    transition: '{{ light_transition / 1000 }}'
                    entity_id: !input light
                - delay:
                    milliseconds: !input light_transition
              default:
              - service: light.turn_on
                data:
                  brightness: '{{ [ [state_attr(light,"brightness")-step_long, min_brightness]
                    | max, max_brightness] | min }}'
                  transition: 0.25
                  entity_id: !input light
              - delay:
                  milliseconds: 250
      default:
      - repeat:
          while: '{{ states(light) != "off" }}'
          sequence:
          - service: light.turn_on
            data:
              brightness: '{{ [ [state_attr(light,"brightness")-step_long, min_brightness]
                | max, max_brightness] | min }}'
              transition: 0.25
              entity_id: !input light
          - delay:
              milliseconds: 250
  - conditions: '{{ action == color_up and light_color_mode_id != "none" }}'
    sequence:
      choose:
      - conditions: '{{ light_color_mode_id == "color_temp" }}'
        sequence:
        - service: light.turn_on
          data:
            color_temp: '{{ state_attr(light,"color_temp")|int + 50 }}'
            transition: 0.25
          entity_id: !input light
      - conditions: '{{ light_color_mode_id == "hs_color" }}'
        sequence:
        - service: light.turn_on
          data:
            hs_color: '{{ [((state_attr(light,"hs_color")[0] or 0) + 15) % 360, 100]
              }}'
            transition: 0.25
          entity_id: !input light
  - conditions: '{{ action == color_down and light_color_mode_id != "none" }}'
    sequence:
      choose:
      - conditions: '{{ light_color_mode_id == "color_temp" }}'
        sequence:
        - service: light.turn_on
          data:
            color_temp: '{{ [state_attr(light,"color_temp")|int - 50, 1]|max }}'
            transition: 0.25
          entity_id: !input light
      - conditions: '{{ light_color_mode_id == "hs_color" }}'
        sequence:
        - service: light.turn_on
          data:
            hs_color: '{{ [((state_attr(light,"hs_color")[0] or 0) - 15) % 360, 100]
              }}'
            transition: 0.25
          entity_id: !input light
  - conditions: '{{ action == color_up_repeat and light_color_mode_id != "none" }}'
    sequence:
      choose:
      - conditions: '{{ light_color_mode_id == "color_temp" }}'
        sequence:
        - repeat:
            while: '{{ true }}'
            sequence:
            - service: light.turn_on
              data:
                color_temp: '{{ state_attr(light,"color_temp")|int + 50 }}'
                transition: 0.25
              entity_id: !input light
            - delay:
                milliseconds: 250
      - conditions: '{{ light_color_mode_id == "hs_color" }}'
        sequence:
        - repeat:
            while: '{{ true }}'
            sequence:
            - service: light.turn_on
              data:
                hs_color: '{{ [((state_attr(light,"hs_color")[0] or 0) + 15) % 360,
                  100] }}'
                transition: 0.25
              entity_id: !input light
            - delay:
                milliseconds: 250
  - conditions: '{{ action == color_down_repeat and light_color_mode_id != "none"
      }}'
    sequence:
      choose:
      - conditions: '{{ light_color_mode_id == "color_temp" }}'
        sequence:
        - repeat:
            while: '{{ true }}'
            sequence:
            - service: light.turn_on
              data:
                color_temp: '{{ [state_attr(light,"color_temp")|int - 50, 1]|max }}'
                transition: 0.25
              entity_id: !input light
            - delay:
                milliseconds: 250
      - conditions: '{{ light_color_mode_id == "hs_color" }}'
        sequence:
        - repeat:
            while: '{{ true }}'
            sequence:
            - service: light.turn_on
              data:
                hs_color: '{{ [((state_attr(light,"hs_color")[0] or 0) - 15) % 360,
                  100] }}'
                transition: 0.25
              entity_id: !input light
            - delay:
                milliseconds: 250
