---
substitutions:
  device: nspanel-dev
  name: NSPanel DEV
  area: DEV
  comment: "${area} | 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
      - delay: 2s
      - lambda: 'id(nspanel_id).set_display_dim(5, 80);'

  #   priority: -100
  #   then:
  #     - wait_until:
  #         api.connected
  #     - delay: 10s
  #     - homeassistant.service:
  #         service: automation.trigger
  #         data:
  #           entity_id: automation.nspanel_send_weather_to_nspanel

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

external_components:
  # - source:
  #     type: git
  #     url: https://github.com/sairon/esphome-nspanel-lovelace-ui
  #     ref: dev
  #   refresh: 0s
  #   components: [nspanel_lovelace]
  - source:
      type: git
      url: https://github.com/olicooper/esphome-nspanel-lovelace-native
      ref: dev
    refresh: 0s
    components: [nspanel_lovelace]
  # - source: components

packages:
  common: !include common/common.yaml
  # common: !include common/common_dev.yaml

# web_server:
#   port: 80
#   local: true

api:
  homeassistant_services: true
  homeassistant_states: true
  services:
    # https://github.com/granadaxronos/120-SONG_NOKIA_RTTTL_RINGTONE_PLAYER_FOR_ARDUINO_UNO/blob/master/RTTTL_PLAYER/songs.h
    - 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).notify_on_screensaver(heading, message, 10000);'
        # - 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~~");'

    # DISABLED until https://github.com/olicooper/esphome-nspanel-lovelace-native/issues/20 is fixed
    # - 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
        timeout: int
        icon: string
      then:
        - lambda: 'id(nspanel_id).notify_on_screensaver(title, description, timeout);'
        # 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~" + timeout + "~3~" + icon + "~65535~");'
        - 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

nspanel_lovelace:
  id: nspanel_id
  sleep_timeout: 10
  locale:
    language: nspanel/sk.json
    temperature_unit: celcius
  screensaver:
    time_id: ha_time
    time_format: '%H:%M~$name_day'
    date_format: '%A, %d. %b. %Y'
    weather:
      entity_id: sensor.weather_forecast_daily
    # type: screensaver2
    # statusIcon1:
    #   entity: switch.office_r_light
    #   icon:
    #     "on": mdi:sofa
    #     "off": mdi:sofa
    #   color:
    #     "on": [255, 255, 255]
    #     "off": [255, 255, 255]
    # statusIcon2:
    #   entity: light.office_r_desk_strip
    #   icon:
    #     "on": mdi:table-furniture
    #     "off": mdi:table-furniture
    #   color:
    #     "on": [255, 255, 255]
    #     "off": [255, 255, 255]
    # entities:
    #   - entity: sensor.weather_forecast_daily
    #     color: [255, 255, 255]
    #   - entity: delete
    #   - entity: delete
    #   - entity: delete
    #   - entity: sensor.weather_forecast_daily
    #     color: [255, 255, 255]
    #     type: 0
    #   - entity: sensor.weather_forecast_daily
    #     color: [255, 255, 255]
    #     type: 1
    #   - entity: sensor.weather_forecast_daily
    #     color: [255, 255, 255]
    #     type: 2
    #   - entity: sensor.weather_forecast_daily
    #     color: [255, 255, 255]
    #     type: 3
    #   - entity: delete
    #   - entity: sensor.zigbee_office_r_temperature
    #     color: [255, 255, 255]
    #     name: ""
    #     icon: mdi:home-thermometer
    #   - entity: switch.heating_relay_3
    #     icon:
    #       "on": mdi:radiator
    #       "off": "text:"
    #     color:
    #       "on": [255, 255, 255]
    #   - entity: binary_sensor.heating_fireplace_fire
    #     icon:
    #       "on": mdi:fireplace
    #       "off": "text:"
    #     color:
    #       "on": [255, 255, 255]
    #   - entity: delete
    #   - entity: delete
    #   - entity: delete
  cards:
    - type: cardGrid
      title: Kuchyňa / Obývačka
      entities:
        - name: Kuchyňa
          entity_id: light.kitchen
          icon: ceiling-light
        - name: Stôl
          entity_id: light.dining_table
          icon: ceiling-light
        - name: Gauč
          entity_id: light.living_room
          icon: ceiling-light
        - name: TV
          entity_id: light.tv_light
          icon: led-strip-variant
        - name: (Stôl)
          entity_id: light.dining_table_ambilight
          icon: ceiling-light-multiple-outline
        - name: (Gauč)
          entity_id: light.living_room_ambilight
          icon: ceiling-light-multiple-outline
    - type: cardGrid
      title: Romanko
      entities:
        - name: Svetlo
          entity_id: switch.office_r_light
          icon: wall-sconce-flat
        - name: LED pásik
          entity_id: light.office_r_desk_strip
          icon: led-strip-variant
        - entity_id: delete
        - name: Monitor
          entity_id: light.office_r_display_lamp
          icon: monitor-star
        - name: (Monitor)
          entity_id: light.office_r_display_lamp_ambilight
          icon: monitor-star
        - name: Whitebox
          entity_id: switch.whitebox
    - type: cardGrid
      title: Von
      entities:
        - name: Terasa
          entity_id: light.patio_light
          icon: lightbulb-group
        - name: Terasa
          entity_id: switch.patio_relay
          icon: power-socket-fr
        - name: Vzadu
          entity_id: switch.outside_light_back
          icon: light-flood-down
        - name: Z. domček
          entity_id: switch.shed_light
          icon: wall-sconce-flat
          # state_template: '{{ is_state("binary_sensor.shed_status", "off") }}'
        - name: Z. domček
          entity_id: switch.shed_socket
          icon: power-socket-fr
          # state_template: '{{ is_state("binary_sensor.shed_status", "off") }}'
        - name: Záhrada
          entity_id: switch.garden_light
          icon: light-flood-down
          # state_template: '{{ is_state("binary_sensor.shed_status", "off") }}'
    - type: cardGrid
      title: Brána
      entities:
        - name: Otvoriť
          entity_id: button.gate_open
          icon: gate-open
        - name: Zatvoriť
          entity_id: button.gate_close
          icon: gate
        - name: Automatizácia
          entity_id: navigate.card_gate_automation
    - type: cardGrid
      title: Závlaha
      entities:
        - name: A1
          entity_id: switch.irrigation_valve_a1
          icon: valve
        - name: A2
          entity_id: switch.irrigation_valve_a2
          icon: valve
        - name: A3
          entity_id: switch.irrigation_valve_a3
          icon: valve
        - name: A4
          entity_id: switch.irrigation_valve_a4
          icon: valve
        - name: A5
          entity_id: switch.irrigation_valve_a5
          icon: valve
        - name: Automatizácia
          entity_id: navigate.card_irrigation_automation
    - type: cardThermo
      title: Kúrenie
      thermo_entity_id: climate.bt_trv_office_r
    # - type: cardQR
    #   title: Guest Wifi
    #   qr_text: "WIFI:T:WPA;S:LAB;P:slniecko;;"
    #   # qrCode: "URL:https://www.johndoe.com/"
    # #        entities:
    # #          - entity_id: delete
    # #          - entity_id: iText.LAB
    # #            name: Name
    # #            icon: wifi
    # #          - entity_id: iText.slniecko
    # #            name: Password
    # #            icon: key
    - type: cardEntities
      title: Zwift
      entities:
        - name: Svetlo
          entity_id: light.basement_light
          icon: lightbulb-multiple
        - name: LED
          entity_id: light.exercise_room
          icon: led-strip-variant
        - name: Ventilátor
          entity_id: light.exercise_room_fan
          icon: fan
        - name: Automatizácia
          entity_id: navigate.card_exercise_automation
    - type: cardEntities
      title: Automatizácia
      id: card_exercise_automation
      hidden: true
      entities:
        - name: Start
          entity_id: automation.zwift_enable_fan
        - name: End
          entity_id: automation.zwift_disable_fan
    - type: cardEntities
      title: Automatizácia
      id: card_gate_automation
      hidden: true
      entities:
        - name: Auto-otváranie
          entity_id: switch.gate_auto_open
        - name: Auto-zatváranie
          entity_id: switch.gate_auto_close
    - type: cardEntities
      title: Automatizácia
      id: card_irrigation_automation
      hidden: true
      entities:
        - name: Ráno
          entity_id: automation.irrigation_morning
        - name: Terasa
          entity_id: automation.irrigation_patio
        - name: Rast trávy
          entity_id: automation.irrigation_grow_grass

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

binary_sensor:
  - platform: gpio
    name: Left Button
    pin:
      number: 14
      inverted: true
    on_press:
      - switch.toggle: relay_1
      # - homeassistant.service:
      #     service: switch.toggle
      #     data:
      #       entity_id: switch.gate_open

  - platform: gpio
    name: Right Button
    pin:
      number: 27
      inverted: true
    on_press:
      - switch.toggle: relay_1
      # - homeassistant.service:
      #     service: switch.toggle
      #     data:
      #       entity_id: switch.gate_close

output:
  - platform: ledc
    id: buzzer_out
    pin:
      number: 21

switch:
  - platform: gpio
    name: Relay 1
    id: relay_1
    pin:
      number: 22
    on_turn_on:
      # show a light on the panel when we swich on the light
      # - lambda: 'id(nspanel).send_custom_command("pageType~popupLight~[TITLE TO SHOW ON SCREEN]~[ENTITY NAME]");'
      - lambda: 'id(nspanel_id).send_display_command("pageType~popupLight~Light 1~light.bed_light");'
    # on_turn_off:
    #   # send to screensaver when switch off the light
    #   - mqtt.publish:
    #       topic: $panel_recv_topic
    #       payload: '{"CustomRecv":"event,sleepReached,cardGrid"}'

  - platform: gpio
    name: Relay 2
    id: relay_2
    pin:
      number: 19
    on_turn_on:
      # show a light on the panel when we swich on the light
      - lambda: 'id(nspanel_id).send_display_command("pageType~popupLight~Light 2~light.bed_light2");'
    # on_turn_off:
    #   # send to screensaver when switch off the light
    #   - mqtt.publish:
    #       topic: $panel_recv_topic
    #       payload: '{"CustomRecv":"event,sleepReached,cardGrid"}'

  # Turn screen power on/off. Easy way to configure the screen power control, but this should not be used from HA, as all components must be re-initialized afterwards. For lights, names of lights etc. this practically means that the state must change once to happen.
  - 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 Entry

  - platform: template
    id: room_temperature_id

text_sensor:
  - platform: homeassistant
    id: name_day
    entity_id: sensor.nameday_sk

# esp32_ble_tracker:
#   scan_parameters:
#     duration: 1min

# bluetooth_proxy:
