blueprint:
  name: "HASPone create device triggers"
  description: |

    ## Blueprint Version: `1.05.00`

    # Description

    Create [Device Triggers](https://www.home-assistant.io/integrations/device_trigger.mqtt/) for each of the HASPone buttons defined below.  Device triggers can be utilized while creating your own automations through the Home Assistant UI.

    This allows for the easy creation of automations which will be triggered when pressing buttons on your HASPone.

    ## HASPone Page and Button Reference

    The images below show each available HASPone page along with the layout of available button objects.

    <details>

    | Page 0 | Pages 1-3 | Pages 4-5 |
    |--------|-----------|-----------|
    | ![Page 0](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p0_Init_Screen.png) | ![Pages 1-3](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p1-p3_4buttons.png) | ![Pages 4-5](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p4-p5_3sliders.png) |

    | Page 6 | Page 7 | Page 8 |
    |--------|--------|--------|
    | ![Page 6](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p6_8buttons.png) | ![Page 7](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p7_12buttons.png) | ![Page 8](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p8_5buttons+1slider.png) |

    | Page 9 | Page 10 | Page 11 |
    |--------|---------|---------|
    | ![Page 9](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p9_9buttons.png) | ![Page 10](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p10_5buttons.png) | ![Page 11](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_p11_1button+1slider.png)

    </details>

  domain: automation
  input:
    haspdevice:
      name: "HASPone Device"
      description: "Select the HASPone device"
      selector:
        device:
          integration: mqtt
          manufacturer: "HASwitchPlate"
          model: "HASPone v1.0.0"
    objects:
      name: "HASPone buttons"
      description: "Create one device trigger for each button in this list."
      default:
        - p[1].b[4]
        - p[1].b[5]
        - p[1].b[6]
        - p[1].b[7]
      selector:
        object:

mode: parallel
max_exceeded: silent

variables:
  haspdevice: !input haspdevice
  objects: !input objects
  haspname: >-
    {%- for entity in device_entities(haspdevice) -%}
      {%- if entity|regex_search("^sensor\..+_sensor(?:_\d+|)$") -%}
        {{- entity|regex_replace(find="^sensor\.", replace="", ignorecase=true)|regex_replace(find="_sensor(?:_\d+|)$", replace="", ignorecase=true) -}}
      {%- endif -%}
    {%- endfor -%}
  haspsensor: >-
    {%- for entity in device_entities(haspdevice) -%}
      {%- if entity|regex_search("^sensor\..+_sensor(?:_\d+|)$") -%}
        {{ entity }}
      {%- endif -%}
    {%- endfor -%}
  haspClientId: '{{state_attr(haspsensor, "haspClientID")}}'
  haspManufacturer: '{{state_attr(haspsensor, "haspManufacturer")}}'
  haspModel: '{{state_attr(haspsensor, "haspModel")}}'
  sw_version: '{{state_attr(haspsensor, "espVersion")}}'

trigger_variables:
  haspdevice: !input haspdevice
  haspsensor: >-
    {%- for entity in device_entities(haspdevice) -%}
      {%- if entity|regex_search("^sensor\..+_sensor(?:_\d+|)$") -%}
        {{ entity }}
      {%- endif -%}
    {%- endfor -%}

trigger:
  - platform: template
    value_template: "{{ is_state(haspsensor, 'ON') }}"
  - platform: homeassistant
    event: start

condition:
  - condition: template
    value_template: "{{ is_state(haspsensor, 'ON') }}"

action:
  - repeat:
      count: "{{objects|length}}"
      sequence:
        - service: mqtt.publish
          data:
            topic: "homeassistant/device_automation/{{haspname}}/{{objects[repeat.index-1]|replace('.','')|replace('[','')|replace(']','')}}-ON/config"
            payload: >-
              {"automation_type":"trigger",
              "payload":"ON",
              "topic":"hasp/{{haspname}}/state/{{objects[repeat.index-1]}}",
              "type":"button_short_press",
              "subtype":"{{objects[repeat.index-1]}}",
              "device":{
              "identifiers":["{{haspClientId}}"],
              "name":"{{haspname}}",
              "manufacturer":"{{haspManufacturer}}",
              "model":"{{haspModel}}",
              "sw_version":{{sw_version}}
              }}
        - service: mqtt.publish
          data:
            topic: "homeassistant/device_automation/{{haspname}}/{{objects[repeat.index-1]|replace('.','')|replace('[','')|replace(']','')}}-OFF/config"
            payload: >-
              {"automation_type":"trigger",
              "payload":"OFF",
              "topic":"hasp/{{haspname}}/state/{{objects[repeat.index-1]}}",
              "type":"button_short_release",
              "subtype":"{{objects[repeat.index-1]}}",
              "device":{
              "identifiers":["{{haspClientId}}"],
              "name":"{{haspname}}",
              "manufacturer":"{{haspManufacturer}}",
              "model":"{{haspModel}}",
              "sw_version":{{sw_version}}
              }}
  