# Loads default set of integrations. Do not remove.
default_config:

# includes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# https://www.home-assistant.io/docs/configuration/basic/
homeassistant:  
  name: "warpzone"
  latitude: 51.944514
  longitude: 7.638933
  elevation: 70
  unit_system: metric
  currency: EUR 
  country: DE
  time_zone: Europe/Berlin
  internal_url: https://{{ domain }}
  auth_providers:
    - type: trusted_networks
      trusted_networks:
        - 10.0.0.0/22
        - 192.168.0.0/24
      allow_bypass_login: true
    - type: homeassistant

# https://www.home-assistant.io/integrations/http/
http:
  server_port: 8123
  ip_ban_enabled: False
  login_attempts_threshold: 10
  use_x_forwarded_for: true
  trusted_proxies: 
    - 172.0.0.0/8

# https://www.home-assistant.io/integrations/api/
api:

# https://www.home-assistant.io/integrations/logger
# log_level= critical, fatal, error, warning, warn, info, debug, notset
logger:
  default: info

# https://www.home-assistant.io/integrations/influxdb/
influxdb:
  api_version: 2 
  host: influxdb
  port: 8086 
  ssl: false
  organization: homeassistant
  bucket: homeassistant
  token: {{ influxdb_token }}
  default_measurement: units


# eQ3 Max 
maxcube:
  gateways:
    - host: {{ eq3max.cube_host }}
      port: 62910
      scan_interval: 60

# zonenstatus wird vom hauptschalter gesteuert, switch02 ping ist nicht mehr notwendig
# https://www.home-assistant.io/integrations/ping/
#binary_sensor:
#  - platform: ping
#    host: TL-SG3452X
#    name: "Switch02"
#    count: 3
#    scan_interval: 20 

# https://www.home-assistant.io/integrations/tts/
tts:
  - platform: picotts
    language: "de-DE"

# https://www.home-assistant.io/integrations/rest_command/
rest_command: 
  set_zone_status_open: 
    url: "https://api.warpzone.ms/setstatus?newstatus=OPEN&update_key={{ warpai_status_update_key }}"
    method: GET 
  set_zone_status_closed: 
    url: "https://api.warpzone.ms/setstatus?newstatus=CLOSED&update_key={{ warpai_status_update_key }}"
    method: GET 
  set_lounge_matrix_off:
    url: "http://led-matrix.warpzone.lan/play/off.png"
    method: GET
  set_lounge_matrix_on:
    url: "http://led-matrix.warpzone.lan/skip"
    method: GET
  open_door:
    url: "http://shippo.warpzone.lan:7000/open_door?username=warpzone&password={{ portal_password }}"
    method: GET

# https://www.home-assistant.io/integrations/telegram/
telegram_bot:
  - platform: polling
    api_key: "{{ telegram_statusbot_token }}"
    allowed_chat_ids:
      - -1001893089134
      - -1001341516106

notify:
  - platform: telegram
    name: zonenstatus
    chat_id: -1001893089134
  - platform: telegram
    name: stromverbrauch
    chat_id: -1001341516106

## Automatisierungen ################################################################

# helper to store zonenstatus 
input_select:
  zonenstatus:
    options:
      - open
      - closed
    icon: mdi:door-open

# current time for zonenstatus telegram
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

automation ansible:
  # set zonenstatus
  - alias: ANSIBLE_zonenstatus_set
    description: Speichert Zonenstatus in einem Helper
    mode: restart
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.esphome_status_hauptschalter
        from: "off"
        to: "on"
      - platform: state
        entity_id:
          - binary_sensor.esphome_status_hauptschalter
        from: "on"
        to: "off"
    condition: []
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: binary_sensor.esphome_status_hauptschalter
                state: "on"
            sequence:
              - service: input_select.select_option
                data:
                  option: open
                target:
                  entity_id: input_select.zonenstatus
          - conditions:
              - condition: state
                entity_id: binary_sensor.esphome_status_hauptschalter
                state: "off"
            sequence:
              - service: input_select.select_option
                data:
                  option: closed
                target:
                  entity_id: input_select.zonenstatus

  # send zonenstatus to MQTT, REST
  - alias: ANSIBLE_zonenstatus_send_MQTT_REST
    description: Sendet den Zonenstatus per MQTT und REST für Shutdown und SpaceAPI
    trigger:
    - platform: time_pattern
      seconds: /10
    condition: []
    action:
    - choose:
      - conditions:
        - condition: state
          entity_id: input_select.zonenstatus
          state: 'open'
        sequence:
        - service: mqtt.publish
          data:
            topic: warpzone/door/status
            payload: OPEN
        - service: rest_command.set_zone_status_open
          data: {}
      - conditions:
        - condition: state
          entity_id: input_select.zonenstatus
          state: 'closed'
        sequence:
        - service: mqtt.publish
          data:
            topic: warpzone/door/status
            payload: CLOSED
        - service: rest_command.set_zone_status_closed
          data: {}
    mode: restart

  # send zonenstatus once on change to MQTT
  - alias: ANSIBLE_zonenstatus_send_once_MQTT
    description: Sendet den Zonenstatus EINMALIG (on change) per MQTT
    trigger:
      - platform: state
        entity_id:
          - input_select.zonenstatus
    condition: []
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: open
            sequence:
              - service: mqtt.publish
                data:
                  topic: warpzone/door/status_once
                  payload: OPEN
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: closed
            sequence:
              - service: mqtt.publish
                data:
                  topic: warpzone/door/status_once
                  payload: CLOSED
    mode: restart

  # send zonenstatus to telegram
  - alias: ANSIBLE_zonenstatus_send_telegram
    description: Sendet den Zonenstatus per Telegram-Bot
    trigger:
      - platform: state
        entity_id:
          - input_select.zonenstatus
    condition: []
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: open
            sequence:
              - service: telegram_bot.send_message
                data:
                  target: -1001893089134
                  timeout: 30
                  message: Die warpzone ist seit {{ '{{' }} states('sensor.time') {{ '}}' }} geöffnet!
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: closed
            sequence:
              - service: telegram_bot.send_message
                data:
                  target: -1001893089134
                  timeout: 30
                  message: Die warpzone ist seit {{ '{{' }} states('sensor.time') {{ '}}' }} geschlossen!
    mode: restart
  
  # Stromverbrauch auf Telegram senden
  - alias: ANSIBLE_stromverbrauch_send_telegram
    description: Sendet alle 10 Minuten den Stromverbrauch per Telegram
    trigger:
      - platform: time_pattern
        minutes: /10
    condition: []
    action:
      - if:
        - condition: state
          entity_id: automation.ansible_zonenstatus_send_telegram
          attribute: current
          state: "1"
        then:
          - wait_for_trigger:
            - platform: state
              entity_id:
                - automation.ansible_zonenstatus_send_telegram
              attribute: current
              to: "0"
      - service: telegram_bot.send_message
        data:
          target: -1001341516106
          timeout: 30
          message: >
            Zähler Power: {{ '{{' }} states('sensor.esphome_power_total_power') {{ '}}' }} W 

            Solar Power: {{ '{{' }} states('sensor.esphome_power_total_power') {{ '}}' }} W 

            Temperatur Hackcenter: {{ '{{' }} states('sensor.temp') {{ '}}' }} °C
    mode: restart

  # Licht im Hackcenter dimmen
  - alias: ANSIBLE_dali_licht_hackcenter
    description: Ruft den Helper aus um per MQTT das Licht zu dimmen
    trigger:
      - platform: state
        entity_id:
          - input_number.dali_licht_hackcenter
        for:
          hours: 0
          minutes: 0
          seconds: 0
    action:
      - service: mqtt.publish
        data:
          topic: light/dali
          payload_template: "{{ '{{' }} states('input_number.dali_licht_hackcenter') | int {{ '}}' }}"
    mode: restart

  # Feinstaubsensor steuert Luftfilter
  - alias: ANSIBLE_Feinstaubsensor_Luftfilter
    description: Feinstaubsensor steuer Luftfilter
    trigger:
      - platform: time_pattern
        seconds: /10
    condition: []
    action:
      - choose:
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                below: 36
            sequence:
              - service: fan.set_percentage
                data:
                  percentage: 33
                target:
                  device_id: 8b6ff876340558ef11d13441afecec8d
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                above: 35
                below: 86
            sequence:
              - service: fan.set_percentage
                data:
                  percentage: 66
                target:
                  device_id: 8b6ff876340558ef11d13441afecec8d
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                above: 85
            sequence:
              - service: fan.set_percentage
                data:
                  percentage: 100
                target:
                  device_id: 8b6ff876340558ef11d13441afecec8d
    mode: restart


  # Feinstaubsensor steuert Ampel
  - alias: ANSIBLE_Feinstaubsensor_Ampel
    description: Feinstaubsensor status auf Ampel anzeigen
    trigger:
      - platform: time_pattern
        seconds: /10
    condition: []
    action:
      - choose:
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                below: 36
            sequence:
              - device_id: e26403f906b93f7c6b4f5339a21f9b26
                domain: select
                entity_id: select.ampel_preset
                type: select_option
                option: Green static
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                above: 35
                below: 86
            sequence:
              - device_id: e26403f906b93f7c6b4f5339a21f9b26
                domain: select
                entity_id: select.ampel_preset
                type: select_option
                option: Orange static
          - conditions:
              - condition: numeric_state
                entity_id: sensor.feinstaub_2_5
                above: 85
            sequence:
              - device_id: e26403f906b93f7c6b4f5339a21f9b26
                domain: select
                entity_id: select.ampel_preset
                type: select_option
                option: Red static
    mode: restart

  # WLED rot lounge
  - alias: ANSIBLE_WLED_lounge_red
    description: WLED rot in der lounge
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_5
        domain: binary_sensor
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 255
            - 0
            - 0
          effect: Solid
        target:
          area_id:
            - kuche
            - lounge
            - serverrack
    mode: queued
    max: 30

  # WLED rot im hackcenter
  - alias: ANSIBLE_WLED_hackcenter_red
    description: WLED rot im hackcenter
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_2
        domain: binary_sensor
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 255
            - 0
            - 0
          effect: Solid
        target:
          area_id:
            - hackcenter
            - eingang
    mode: queued
    max: 30


  # WLED gruen lounge
  - alias: ANSIBLE_WLED_lounge_green
    description: WLED gruen in der lounge
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_5
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 1
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 0
            - 255
            - 0
          effect: Solid
        target:
          area_id:
            - kuche
            - lounge
            - serverrack
    mode: queued
    max: 30

  # WLED gruen hackcenter
  - alias: ANSIBLE_WLED_hackcenter_green
    description: WLED gruen im hackcenter
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 1
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 0
            - 255
            - 0
          effect: Solid
        target:
          area_id:
            - hackcenter
            - eingang
    mode: queued
    max: 30


  # WLED blue lounge
  - alias: ANSIBLE_WLED_lounge_blue
    description: wLED blau in der lounge
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_5
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 2
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 0
            - 0
            - 255
          effect: Solid
        target:
          area_id:
            - kuche
            - lounge
            - serverrack
    mode: queued
    max: 30

  # WLED blue hackcenter
  - alias: ANSIBLE_WLED_hackcenter_blue
    description: wLED blau im hackcenter
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 2
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 0
            - 0
            - 255
          effect: Solid
        target:
          area_id:
            - hackcenter
            - eingang
    mode: queued
    max: 30


  # WLED white lounge
  - alias: ANSIBLE_WLED_lounge_white
    description: WLED weiss in der lounge
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_5
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 3
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 255
            - 255
            - 255
          effect: Solid
        target:
          area_id:
            - kuche
            - lounge
            - serverrack
    mode: queued
    max: 30

  # WLED white hackcenter
  - alias: ANSIBLE_WLED_hackcenter_white
    description: WLED weiss im hackcenter
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 3
    condition: []
    action:
      - service: light.turn_on
        data:
          rgb_color:
            - 255
            - 255
            - 255
          effect: Solid
        target:
          area_id:
            - hackcenter
            - eingang
    mode: queued
    max: 30

  # WLED off lounge
  - alias: ANSIBLE_WLED_lounge_off
    description: WLED aus in der lounge
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_6
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 0
    condition: []
    action:
      - service: light.turn_off
        target:
          area_id:
            - kuche
            - lounge
            - serverrack
      - service: rest_command.set_lounge_matrix_off
        data: {}
    mode: queued
    max: 30
  
  # WLED off hackcenter
  - alias: ANSIBLE_WLED_hackcenter_off
    description: WLED aus im hackcenter
    trigger:
      - type: turned_on
        platform: device
        device_id: 33915e12bd114226dcd4718e77a27779
        entity_id: binary_sensor.button_3
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 0
    condition: []
    action:
      - service: light.turn_off
        target:
          area_id:
            - hackcenter
            - eingang
    mode: queued
    max: 30
  
  # WLED Zonenstatus
  - alias: "ANSIBLE_WLED_Zonenstatus"
    description: "Turn WLED devices on, when opened, off when closed"
    mode: single
    trigger:
      - platform: state
        entity_id:
          - input_select.zonenstatus
    condition: []
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: open
            sequence:
              - service: light.turn_on
                data: {}
                target:
                  area_id:
                    - eingang
                    - hackcenter
                    - kuche
                    - lounge
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: closed
            sequence:
              - service: light.turn_off
                data: {}
                target:
                  area_id:
                    - eingang
                    - hackcenter
                    - kuche
                    - lounge

  # ESPHOME Zonenstatus
  - alias: "ANSIBLE_ESPHOME_Zonenstatus"
    description: ""
    mode: single
    trigger:
      - platform: state
        entity_id:
          - input_select.zonenstatus
    condition: []
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: "opened"
            sequence:
              - service: light.turn_on
                data:
                  effect: Rainbow
                target:
                  entity_id:
                    - light.liba_logo_leds
                    - light.kuehl_leds
              - type: turn_on
                device_id: 7ba91dde6b1413340fdc66f2984dfd00
                entity_id: light.brightness
                domain: light
          - conditions:
              - condition: state
                entity_id: input_select.zonenstatus
                state: "closed"
            sequence:
              - type: turn_off
                device_id: 1dace6e3f89d85152cde6383312792eb
                entity_id: switch.esphome_plug01_relay
                domain: switch
              - type: turn_off
                device_id: 9f6aded98a4a96ab85c96b829895f449
                entity_id: switch.esphome_plug02_relay
                domain: switch
              - type: turn_off
                device_id: 502f88ff07aa71cb053ad4112e0dd6bd
                entity_id: switch.esphome_plug03_relay
                domain: switch
              - type: turn_off
                device_id: b9ea4fd30dabfe365b5098c66eb1f4ba
                entity_id: switch.esphome_plug04_relay
                domain: switch
              - type: turn_off
                device_id: c5f54add49fda7640497b1883315d48b
                entity_id: switch.esphome_plug05_relay
                domain: switch
              - type: turn_off
                device_id: a0788c8cf4ee036ce43e0d563fdbdb3a
                entity_id: switch.esphome_plug06_relay
                domain: switch
              - type: turn_off
                device_id: 1637a583963ba02dc7baa17427f8ac00
                entity_id: switch.esphome_plug07_relay
                domain: switch
              - type: turn_off
                device_id: 1da3229859c49c822fd8e9c630fa9004
                entity_id: switch.esphome_plug08_relay
                domain: switch
              - service: light.turn_off
                data: {}
                target:
                  entity_id:
                    - light.liba_logo_leds
                    - light.kuehl_leds
              - type: turn_off
                device_id: 7ba91dde6b1413340fdc66f2984dfd00
                entity_id: light.brightness
                domain: light