blueprint:
  name: Ulanzi Light Control
  description: 控制 Ulanzi 像素时钟显示灯光进度条
  domain: automation
  input:
    ulanzi_device:
      name: Ulanzi 设备
      description: 选择你的 Ulanzi 像素时钟设备
      selector:
        device:
          filter:
            integration: mqtt
            manufacturer: Ulanzi
          multiple: false
    light_entity:
      name: 灯光实体
      description: 选择要监听的灯光实体
      selector:
        entity:
          filter:
            domain: light
    app_name:
      name: App 名称
      description: 显示在设备上的应用名称（建议英文）
      default: "light"
      selector:
        text:

trigger:
  - platform: state
    entity_id: !input light_entity

variables:
  device_id: !input ulanzi_device
  light_entity: !input light_entity
  app_name: !input app_name
  mqtt_prefix: "{{ states((device_entities(device_id) | select('search','device_topic') | list)[0]) | replace(' ','') }}"
  mqtt_topic: "{{ mqtt_prefix }}/custom/{{ app_name }}"
  is_on: "{{ states(light_entity) == 'on' }}"
  brightness: "{{ state_attr(light_entity, 'brightness') | int(0) }}"
  rgb: "{{ state_attr(light_entity, 'rgb_color') or [255,255,255] }}"
  fill_w: "{{ [[(brightness / 255 * 48) | int, 1] | max, 48] | min }}"
  hex_color: "{{ '#%02X%02X%02X' % (rgb[0]|int, rgb[1]|int, rgb[2]|int) }}"

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ not is_on }}"
        sequence:
          - service: mqtt.publish
            data:
              topic: "{{ mqtt_topic }}"
              payload: "{}"
      - conditions:
          - condition: template
            value_template: "{{ is_on }}"
        sequence:
          - service: mqtt.publish
            data:
              topic: "{{ mqtt_topic }}"
              payload: >-
                {"duration":0,"draw":[{"dr":[1,4,50,8,"#FFFFFF"]},{"df":[2,5,{{ fill_w }},6,"{{ hex_color }}"]}]}

mode: single
