---
substitutions:
  device: nspanel-fireplace
  name: Fireplace NSPanel
  area: Living Room & Kitchen
  comment: "${area}, Fireplace | NSPanel"
  panel_recv_topic: "tele/${device}/RESULT"
  panel_send_topic: "cmnd/${device}/CustomSend"
  tft_url: !secret tft_url

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}
  on_boot:
    then:
      - switch.turn_off: screen_power
      - delay: 1s
      - switch.turn_on: screen_power

esp32:
  variant: esp32
  framework:
    type: esp-idf
    advanced:
      minimum_chip_revision: "3.0"

external_components:
  - source:
      type: git
      url: https://github.com/sairon/esphome-nspanel-lovelace-ui
      ref: dev
    components: [nspanel_lovelace]

packages:
  common: !include common/common.yaml

api:
  # https://github.com/granadaxronos/120-SONG_NOKIA_RTTTL_RINGTONE_PLAYER_FOR_ARDUINO_UNO/blob/master/RTTTL_PLAYER/songs.h
  services:
    - service: play_rtttl
      variables:
        song: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song;'

    - service: notify
      variables:
        heading: string
        message: string
        song: string
      then:
        - lambda: 'id(nspanel_id).send_custom_command("notify~" + heading + "~" + message);'
        - rtttl.play:
            rtttl: !lambda 'return song;'
        - delay: 10s
        - lambda: 'id(nspanel_id).send_custom_command("notify~~");'

    - service: update_tft
      then:
        - lambda: |-
            id(nspanel_id).upload_tft("${tft_url}");

    - service: upload_tft
      variables:
        url: string
      then:
        - lambda: |-
            id(nspanel_id).upload_tft(url);

    # Service to send a command directly to the display. Useful for testing
    - service: send_command
      variables:
        cmd: string
      then:
        - lambda: "id(nspanel_id).send_custom_command(cmd.c_str());"

    # Service to pusblish to mqtt used for refreshin the panel
    - service: publish_to_recv_topic
      variables:
        cmd: string
      then:
        - mqtt.publish:
            topic: $panel_recv_topic
            payload: !lambda "return cmd;"

    # Service to send a command wake the screen
    - service: wake
      then:
        - lambda: 'id(nspanel_id).send_custom_command("wake");'

    # Service to send a command to show screensaver (some of the values are hardcoded)
    - service: show_screensaver
      then:
        - mqtt.publish:
            topic: $panel_recv_topic
            payload: '{"CustomRecv":"event,sleepReached,cardGrid"}'
    # Service to navigate to screen
    - service: navigate_to_page
      variables:
        page: string
      then:
        - mqtt.publish_json:
            topic: $panel_recv_topic
            payload: |-
              root["CustomRecv"] = "event,buttonPress2,navigate." + page + ",button";

    # Service to send a command disable screensaver
    - service: disable_screensaver
      then:
        - lambda: 'id(nspanel_id).send_custom_command("timeout~0");'

    # Service to send a command enabled screensaver
    - service: enable_screensaver
      then:
        - lambda: 'id(nspanel_id).send_custom_command("timeout~20");'

    # Service to send a command wake the screen
    - service: dim_0_to_100
      variables:
        intensity: string
      then:
        - lambda: 'id(nspanel_id).send_custom_command("dimmode~"+intensity+"~100");'

    # Service to send a command To show the screen of a specific like
    - service: show_entity
      variables:
        entity: string
        title: string
      then:
        - lambda: 'id(nspanel_id).send_custom_command("pageType~popupLight~" + title + "~" + entity);'

    # Service to send a command To show the screen of a specific like
    - service: notify_on_screensaver
      variables:
        line1: string
        line2: string
      then:
        - lambda: 'id(nspanel_id).send_custom_command("notify~" + line1 + "~" + line2);'
        - rtttl.play: "short:d=4,o=5,b=100:16e6"

    # Service to send a command To show the screen of a specific like
    # interaction of the buttons are on implemented yet
    - service: notify_fullscreen
      variables:
        title: string
        description: string
        button1: string
        button2: string
        time_secs: string
      then:
        # show notification screen
        - lambda: 'id(nspanel_id).send_custom_command("pageType~popupNotify");'
        # set values on notification screen
        # color defined as number created in binary and converted to decimal (rrrrggggbbbbaaaa)
        - lambda: 'id(nspanel_id).send_custom_command("entityUpdateDetail~id~"+ title + "~65535~" + button1 +"~3840~" + button2 + "~61440~" + description + "~65535~" + time_secs);'
        - rtttl.play: "scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b"

uart:
  id: display_uart
  tx_pin: 16
  rx_pin: 17
  baud_rate: 115200

mqtt:
  id: mqtt_client
  broker: !secret mqtt_ip
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: false
  idf_send_async: true
  reboot_timeout: 0s

nspanel_lovelace:
  id: nspanel_id
  mqtt_recv_topic: $panel_recv_topic
  mqtt_send_topic: $panel_send_topic

time:
  - platform: homeassistant
    id: ha_time
    timezone: Europe/Bratislava

binary_sensor:
  - platform: gpio
    name: Left Button
    pin:
      number: 14
      inverted: true
    on_click:
      #   - switch.toggle: relay_1
      - min_length: 50ms
        max_length: 350ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.living_room
          - mqtt.publish_json:
              topic: $panel_recv_topic
              payload: |-
                root["CustomRecv"] = "event,buttonPress2,navigate.cardGrid_main,button";
      - min_length: 500ms
        max_length: 2000ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.worktop_lights
          - mqtt.publish_json:
              topic: $panel_recv_topic
              payload: |-
                root["CustomRecv"] = "event,buttonPress2,navigate.cardGrid_kitchen,button";

  - platform: gpio
    name: Right Button
    pin:
      number: 27
      inverted: true
    on_click:
      #   - switch.toggle: relay_2
      - min_length: 50ms
        max_length: 350ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.dining_table
          - mqtt.publish_json:
              topic: $panel_recv_topic
              payload: |-
                root["CustomRecv"] = "event,buttonPress2,navigate.cardGrid_main,button";
      - min_length: 500ms
        max_length: 2000ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.mcu_kitchen_stove
          - mqtt.publish_json:
              topic: $panel_recv_topic
              payload: |-
                root["CustomRecv"] = "event,buttonPress2,navigate.cardGrid_kitchen,button";
output:
  - platform: ledc
    id: buzzer_out
    pin:
      number: 21

switch:
  - platform: gpio
    name: Kitchen Worktop Light
    id: relay_1
    pin:
      number: 22

  - platform: gpio
    name: Dining Table Light MAIN
    id: relay_2
    entity_category: config
    pin:
      number: 19

  - platform: gpio
    name: Screen Power
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON

rtttl:
  id: buzzer
  output: buzzer_out

sensor:
  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 5min
    attenuation: 12db

  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25°C
      reference_resistance: 10kOhm
    name: "Living Room Temperature"

  - platform: template
    id: room_temperature_id
