blueprint:
  name: "HASPone p[x].b[y] displays the current weather condition icon only"
  description: |

    ## Blueprint Version: `1.05.00`

    # Description

    A HASPone button displays the current weather condition icon only

    ![Preview](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/hasp_Display_Weather_Condition_Icon_Only.png)

    ## 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>

    ## HASPone Font Reference

    <details>

    Weather icons are available in 6 different sizes

    | Font | Name              |
    | :--- | :---------------- |
    | 5    | Noto Sans 24      |
    | 6    | Noto Sans 32      |
    | 7    | Noto Sans 48      |
    | 8    | Noto Sans 64      |
    | 9    | Noto Sans 80      |
    | 10   | Noto Sans Bold 80 |

    ### Font examples

    ![HASPone Fonts 4-7](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_Fonts_4-7.png) ![HASPone Fonts 8-10](https://raw.githubusercontent.com/HASwitchPlate/HASPone/main/images/NextionUI_Fonts_8-10.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"
    hasppage:
      name: "HASPone Page"
      description: "Select the HASPone page (1-3) for the temperature"
      default: 1
      selector:
        number:
          min: 1
          max: 11
          mode: slider
          unit_of_measurement: page
    haspbutton:
      name: "HASPone Button"
      description: "Select the HASPone button (4-7) for the temperature.  Refer to the HASPone Page and Button reference above."
      default: 4
      selector:
        number:
          min: 4
          max: 15
          mode: slider
          unit_of_measurement: button
    weather_provider:
      name: "Weather provider"
      description: "Select the weather provider to obtain the temperature"
      selector:
        entity:
          domain: weather
    font_select:
      name: "Font"
      description: "Select the font for the displayed icon.  Refer to the HASPone Font Reference above."
      default: "8 - Noto Sans 64"
      selector:
        select:
          options:
            - "5 - Noto Sans 24"
            - "6 - Noto Sans 32"
            - "7 - Noto Sans 48"
            - "8 - Noto Sans 64"
            - "9 - Noto Sans 80"
            - "10 - Noto Sans Bold 80"
    xcen_select:
      name: "Icon horizontal alignment"
      description: "Horizontal icon alignment: 0=Left 1=Center 2=Right"
      default: "1 - Centered"
      selector:
        select:
          options:
            - "0 - Left aligned"
            - "1 - Centered"
            - "2 - Right aligned"
    ycen_select:
      name: "Icon vertical alignment"
      description: "Vertical icon alignment: 0=Top 1=Center 2=Bottom"
      default: "1 - Centered"
      selector:
        select:
          options:
            - "0 - Top aligned"
            - "1 - Centered"
            - "2 - Bottom aligned"
    selected_fgcolor:
      name: "Selected foreground color"
      description: 'Selected foreground color in Nextion RGB565 format (see "Nextion color codes" above for reference). -1 = Current theme selected foreground color.'
      default: -1
      selector:
        number:
          min: -1
          max: 65535
          mode: slider
    selected_bgcolor:
      name: "Selected background color"
      description: 'Selected background color in Nextion RGB565 format (see "Nextion color codes" above for reference). -1 = Current theme selected background color.'
      default: -1
      selector:
        number:
          min: -1
          max: 65535
          mode: slider
    unselected_fgcolor:
      name: "Unselected foreground color"
      description: 'Unselected foreground color in Nextion RGB565 format (see "Nextion color codes" above for reference). -1 = Current theme unselected foreground color.'
      default: -1
      selector:
        number:
          min: -1
          max: 65535
          mode: slider
    unselected_bgcolor:
      name: "Unselected background color"
      description: 'Unselected background color in Nextion RGB565 format (see "Nextion color codes" above for reference). -1 = Current theme unselected background color.'
      default: -1
      selector:
        number:
          min: -1
          max: 65535
          mode: slider
    colorize_icon:
      name: "Colorize weather icon"
      description: "When enabled, weather condition icon will be colored based on the condition (eg, sunny will be yellow).  If disabled, use the colors selected above."
      default: true
      selector:
        boolean:

mode: parallel
max_exceeded: silent

variables:
  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 -%}
  hasppage: !input hasppage
  haspbutton: !input haspbutton
  weather_provider: !input weather_provider
  font_select: !input font_select
  font: '{{ font_select.split(" - ")[0] | int }}'
  xcen_select: !input xcen_select
  xcen: '{{ xcen_select.split(" - ")[0] | int }}'
  ycen_select: !input ycen_select
  ycen: '{{ ycen_select.split(" - ")[0] | int }}'
  selected_fgcolor: !input selected_fgcolor
  selected_bgcolor: !input selected_bgcolor
  unselected_fgcolor: !input unselected_fgcolor
  unselected_bgcolor: !input unselected_bgcolor
  colorize_icon: !input colorize_icon
  haspobject: '{{ "p[" ~ hasppage ~ "].b[" ~ haspbutton ~ "]" }}'
  commandtopic: '{{ "hasp/" ~ haspname ~ "/command/" ~ haspobject }}'
  jsoncommandtopic: '{{ "hasp/" ~ haspname ~ "/command/json" }}'
  jsontopic: '{{ "hasp/" ~ haspname ~ "/state/json" }}'
  icon: >-
    {%- set condition=states(weather_provider) -%}
    {%- if condition == "clear-night" -%}
      
    {%- elif condition == "cloudy" -%}
      
    {%- elif condition == "fog" -%}
      
    {%- elif condition == "hail" -%}
      
    {%- elif condition == "lightning" -%}
      
    {%- elif condition == "lightning-rainy" -%}
      
    {%- elif condition == "partlycloudy" -%}
      
    {%- elif condition == "pouring" -%}
      
    {%- elif condition == "rainy" -%}
      
    {%- elif condition == "snowy" -%}
      
    {%- elif condition == "snowy-rainy" -%}
      
    {%- elif condition == "sunny" -%}
      
    {%- elif condition == "windy" -%}
      
    {%- elif condition == "windy-variant" -%}
      
    {%- elif condition == "exceptional" -%}
      
    {%- else -%}
      
    {%- endif -%}
  activepage: >-
    {%- set activepage = namespace() -%}
    {%- for entity in device_entities(haspdevice) -%}
      {%- if entity|regex_search("^number\..*_active_page(?:_\d+|)$") -%}
        {%- set activepage.entity=entity -%}
      {%- endif -%}
    {%- endfor -%}
    {{ states(activepage.entity) | int(default=-1) }}
  selectedfgtopic: '{{ "hasp/" ~ haspname ~ "/light/selectedforegroundcolor/rgb" }}'
  selectedbgtopic: '{{ "hasp/" ~ haspname ~ "/light/selectedbackgroundcolor/rgb" }}'
  unselectedfgtopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedforegroundcolor/rgb" }}'
  unselectedbgtopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedbackgroundcolor/rgb" }}'
  selectedfg: >-
    {%- if (selected_fgcolor|int) >= 0 -%}
      {{ selected_fgcolor }}
    {%- else -%}
      {%- set color = namespace() -%}
      {%- for entity in device_entities(haspdevice) -%}
        {%- if entity|regex_search("^light\..*_selected_foreground_color(?:_\d+|)$") -%}
          {%- set color.source=entity -%}
        {%- endif -%}
      {%- endfor -%}
      {%- set brightness = state_attr(color.source, "brightness")|int(default=255) / 255 -%}
      {%- set red=(state_attr(color.source, "rgb_color")[0] * brightness)|int(default=0) -%}
      {%- set green=(state_attr(color.source, "rgb_color")[1] * brightness)|int(default=0) -%}
      {%- set blue=(state_attr(color.source, "rgb_color")[2] * brightness)|int(default=0) -%}
      {{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
    {%- endif -%}
  selectedbg: >-
    {%- if (selected_bgcolor|int) >= 0 -%}
      {{ selected_bgcolor }}
    {%- else -%}
      {%- set color = namespace() -%}
      {%- for entity in device_entities(haspdevice) -%}
        {%- if entity|regex_search("^light\..*_selected_background_color(?:_\d+|)$") -%}
          {%- set color.source=entity -%}
        {%- endif -%}
      {%- endfor -%}
      {%- set brightness = state_attr(color.source, "brightness")|int(default=255) / 255 -%}
      {%- set red=(state_attr(color.source, "rgb_color")[0] * brightness)|int(default=0) -%}
      {%- set green=(state_attr(color.source, "rgb_color")[1] * brightness)|int(default=0) -%}
      {%- set blue=(state_attr(color.source, "rgb_color")[2] * brightness)|int(default=0) -%}
      {{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
    {%- endif -%}
  unselectedfg: >-
    {%- if (unselected_fgcolor|int) >= 0 -%}
      {{ unselected_fgcolor }}
    {%- else -%}
      {%- set color = namespace() -%}
      {%- for entity in device_entities(haspdevice) -%}
        {%- if entity|regex_search("^light\..*_unselected_foreground_color(?:_\d+|)$") -%}
          {%- set color.source=entity -%}
        {%- endif -%}
      {%- endfor -%}
      {%- set brightness = state_attr(color.source, "brightness")|int(default=255) / 255 -%}
      {%- set red=(state_attr(color.source, "rgb_color")[0] * brightness)|int(default=0) -%}
      {%- set green=(state_attr(color.source, "rgb_color")[1] * brightness)|int(default=0) -%}
      {%- set blue=(state_attr(color.source, "rgb_color")[2] * brightness)|int(default=0) -%}
      {{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
    {%- endif -%}
  unselectedbg: >-
    {%- if (unselected_bgcolor|int) >= 0 -%}
      {{ unselected_bgcolor }}
    {%- else -%}
      {%- set color = namespace() -%}
      {%- for entity in device_entities(haspdevice) -%}
        {%- if entity|regex_search("^light\..*_unselected_background_color(?:_\d+|)$") -%}
          {%- set color.source=entity -%}
        {%- endif -%}
      {%- endfor -%}
      {%- set brightness = state_attr(color.source, "brightness")|int(default=255) / 255 -%}
      {%- set red=(state_attr(color.source, "rgb_color")[0] * brightness)|int(default=0) -%}
      {%- set green=(state_attr(color.source, "rgb_color")[1] * brightness)|int(default=0) -%}
      {%- set blue=(state_attr(color.source, "rgb_color")[2] * brightness)|int(default=0) -%}
      {{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
    {%- endif -%}
  icon_color: >-
    {%- if not colorize_icon -%}
      {{ selectedfg }}
    {%- else -%}
      {%- set condition=states(weather_provider) -%}
      {%- if condition == "clear-night" -%}
        6350
      {%- elif condition == "cloudy" -%}
        29779
      {%- elif condition == "fog" -%}
        29779
      {%- elif condition == "hail" -%}
        29779
      {%- elif condition == "lightning" -%}
        65504
      {%- elif condition == "lightning-rainy" -%}
        65504
      {%- elif condition == "partlycloudy" -%}
        29779
      {%- elif condition == "pouring" -%}
        29779
      {%- elif condition == "rainy" -%}
        29779
      {%- elif condition == "snowy" -%}
        1535
      {%- elif condition == "snowy-rainy" -%}
        1535
      {%- elif condition == "sunny" -%}
        65504
      {%- elif condition == "windy" -%}
        1535
      {%- elif condition == "windy-variant" -%}
        1535
      {%- elif condition == "exceptional" -%}
        63488
      {%- else -%}
        {{ selected_fgcolor }}
      {%- endif -%}
    {%- endif -%}  

trigger_variables:
  haspdevice: !input haspdevice
  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 -%}
  jsontopic: '{{ "hasp/" ~ haspname ~ "/state/json" }}'
  selectedfgtopic: '{{ "hasp/" ~ haspname ~ "/light/selectedforegroundcolor/rgb" }}'
  selectedbgtopic: '{{ "hasp/" ~ haspname ~ "/light/selectedbackgroundcolor/rgb" }}'
  unselectedfgtopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedforegroundcolor/rgb" }}'
  unselectedbgtopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedbackgroundcolor/rgb" }}'

trigger:
  - platform: state
    entity_id: !input weather_provider
  - platform: homeassistant
    event: start
  - platform: template
    value_template: "{{ is_state(haspsensor, 'ON') }}"
  - platform: mqtt
    topic: "{{jsontopic}}"
  - platform: mqtt
    topic: "{{selectedfgtopic}}"
  - platform: mqtt
    topic: "{{selectedbgtopic}}"
  - platform: mqtt
    topic: "{{unselectedfgtopic}}"
  - platform: mqtt
    topic: "{{unselectedbgtopic}}"

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

action:
  - choose:
      #########################################################################
      # RUN ACTIONS or Home Assistant Startup or HASPone Connect
      # Apply styles, place text, and then place icon if our target page is currently active
      - conditions:
          - condition: template
            value_template: >- 
              {{-
                (trigger is not defined)
              or
                (trigger.platform is none)
              or
                ((trigger.platform == 'homeassistant') and (trigger.event == 'start'))
              or
                ((trigger.platform == 'template') and (trigger.entity_id == haspsensor) and (trigger.to_state.state == 'ON'))
              -}}
        sequence:
          - service: mqtt.publish
            data:
              topic: "{{jsoncommandtopic}}"
              payload: >-
                ["{{haspobject}}.font={{font}}",
                "{{haspobject}}.xcen={{xcen}}",
                "{{haspobject}}.ycen={{ycen}}",
                "{{haspobject}}.pco={{icon_color}}",
                "{{haspobject}}.bco={{selectedbg}}",
                "{{haspobject}}.pco2={{unselectedfg}}",
                "{{haspobject}}.bco2={{unselectedbg}}",
                "{{haspobject}}.txt=\"{{icon}}\""]
      #########################################################################
      # Update weather condition if our weather provider changed state
      - conditions:
          - condition: template
            value_template: '{{ (trigger.platform == "state") and (trigger.entity_id == weather_provider) }}'
        sequence:
          - service: mqtt.publish
            data:
              topic: "{{jsoncommandtopic}}"
              payload: >-
                ["{{haspobject}}.pco={{icon_color}}",
                "{{haspobject}}.font={{font}}",
                "{{haspobject}}.txt=\"{{icon}} \""]
      #########################################################################
      # Catch triggers fired by incoming MQTT messages
      - conditions:
          - condition: template
            value_template: '{{ trigger.platform == "mqtt" }}'
        sequence:
          - choose:
              #########################################################################
              # Theme: Apply selected foreground color when it changes.
              # Any change to the button will remove the overlaid icon.
              - conditions:
                  - condition: template
                    value_template: "{{ (trigger.topic == selectedfgtopic) and not colorize_icon }}"
                sequence:
                  - service: mqtt.publish
                    data:
                      topic: "{{commandtopic}}.pco"
                      payload: "{{trigger.payload}}"
              #########################################################################
              # Theme: Apply selected background color on change
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.topic == selectedbgtopic }}"
                sequence:
                  - service: mqtt.publish
                    data:
                      topic: "{{commandtopic}}.bco"
                      payload: "{{trigger.payload}}"
              #########################################################################
              # Theme: Apply unselected foreground color on change
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.topic == unselectedfgtopic }}"
                sequence:
                  - service: mqtt.publish
                    data:
                      topic: "{{commandtopic}}.pco2"
                      payload: "{{trigger.payload}}"
              #########################################################################
              # Theme: Apply unselected background color on change
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.topic == unselectedbgtopic }}"
                sequence:
                  - service: mqtt.publish
                    data:
                      topic: "{{commandtopic}}.bco2"
                      payload: "{{trigger.payload}}"
