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

  # Autodiscovery für Licht Hackcenter
  - alias: ANSIBLE_autodiscovery_light_hackcenter
    description: "MQTT Autodiscovery für Licht Hackcenter"
    mode: single
    triggers:
      - trigger: homeassistant
        event: start
    action:
      - service: mqtt.publish
        data:
          topic: "homeassistant/light/licht_hackcenter_01/config"
          retain: true
          qos: 0
          payload: >
            {
              "name": "Licht Hackcenter",
              "unique_id": "licht_hackcenter_01",
              "state_topic": "stat/tasmota_B1233C/POWER",
              "command_topic": "cmnd/tasmota_B1233C/Power1",
              "brightness_state_topic": "light/dali_out",
              "brightness_command_topic": "light/dali_in",
              "qos": 0,
              "payload_on": "ON",
              "payload_off": "OFF",
              "optimistic": false,
              "device": {
                "identifiers": ["licht_hackcenter_01"],
                "name": "Licht Hackcenter",
                "manufacturer": "warpzone",
                "model": "DALI"
              }
            }

  # Abluft dauerhaft an
  - alias: ANSIBLE_Abluft_dauer_an
    description: Verhindert Ausschalten der Abluft und setzt festen Wert bei schließen der Zone
    mode: restart
    triggers:
      - type: turned_off
        device_id: 96844a416179e61fff99195b6a16522e
        entity_id: 466bdccea823dfee632a8feac65399a2
        domain: fan
        for:
          hours: 0
          minutes: 0
          seconds: 1
        trigger: device
      - entity_id:
        - input_select.zonenstatus
        from: open
        to: closed
        for:
          hours: 0
          minutes: 0
          seconds: 1
        trigger: state
    conditions: []
    actions:
      - if:
        - condition: state
          entity_id: sensor.season
          state: summer
        then:
          - action: fan.turn_on
            metadata: {}
            data:
              percentage: 15
            target:
              device_id: 96844a416179e61fff99195b6a16522e
        else:
          - action: fan.turn_on
            metadata: {}
            data:
              percentage: 20
            target:
              device_id: 96844a416179e61fff99195b6a16522e

  - alias: ANSIBLE_Zonenshutdown
    mode: single
    triggers:
      - trigger: state
        entity_id:
          - input_select.zonenstatus
        from: open
        to: closed
    conditions: []
    actions:
      - parallel:
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_backcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_dreckcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_gehcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_hackcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_kackcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_loetcenter_aus 
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_rackcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_saal1_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_schnackcenter_aus
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ansible_hackcenter_licht_nach_shutdown

  - alias: ANSIBLE_Zonenboot
    mode: single
    triggers:
      - trigger: state
        entity_id:
          - input_select.zonenstatus
        from: closed
        to: open
    conditions: []
    actions:
      - parallel:
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_backcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_dreckcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_gehcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_hackcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_kackcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_loetcenter_an 
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_rackcenter_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_saal1_an
          - action: automation.trigger
            metadata: {}
            data:
              skip_condition: true
            target:
              entity_id: automation.ANSIBLE_ZONE_schnackcenter_an

  # 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.hms_1600_4t_power')|float(0) {{ '}}' }} W 

            Temperatur Hackcenter: {{ '{{' }} states('sensor.temp') {{ '}}' }} °C
    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

  # Türöffner Knopf  
  - alias: ANSIBLE_Button_Türöffner
    description: ""
    mode: single
    triggers:
      - domain: mqtt
        device_id: 388988a29515369fb821e4e0ff8d9e27
        type: action
        subtype: single
        trigger: device
    conditions: []
    actions:
      - action: rest_command.open_door
        metadata: {}
        data: {}


  - alias: ANSIBLE_ZONE_backcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: backcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: backcenter
    mode: single

  - alias: ANSIBLE_ZONE_dreckenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: dreckcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: dreckcenter
    mode: single

  - alias: ANSIBLE_ZONE_gehcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: gehcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: gehcenter
    mode: single

  - alias: ANSIBLE_ZONE_hackcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: hackcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: hackcenter
    mode: single

  - alias: ANSIBLE_ZONE_kackcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: kackcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: kackcenter
    mode: single

  - alias: ANSIBLE_ZONE_loetcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: loetcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: loetcenter
    mode: single

  - alias: ANSIBLE_ZONE_rackcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: rackcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: rackcenter
    mode: single

  - alias: ANSIBLE_ZONE_saal1_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: saal_1
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: saal_1
    mode: single

  - alias: ANSIBLE_ZONE_schnackcenter_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: schnackcenter
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: schnackcenter
    mode: single

  - alias: ANSIBLE_ZONE_NoAutoOn_aus
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          area_id: NoAutoOn
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          area_id: NoAutoOn
    mode: single

  - alias: ANSIBLE_ZONE_backcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: backcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: backcenter
    mode: single

  - alias: ANSIBLE_ZONE_dreckenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: dreckcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: dreckcenter
    mode: single

  - alias: ANSIBLE_ZONE_gehcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: gehcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: gehcenter
    mode: single

  - alias: ANSIBLE_ZONE_hackcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: hackcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: hackcenter
    mode: single

  - alias: ANSIBLE_ZONE_kackcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: kackcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: kackcenter
    mode: single

  - alias: ANSIBLE_ZONE_loetcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: loetcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: loetcenter
    mode: single

  - alias: ANSIBLE_ZONE_rackcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: rackcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: rackcenter
    mode: single

  - alias: ANSIBLE_ZONE_saal1_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: saal_1
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: saal_1
    mode: single

  - alias: ANSIBLE_ZONE_schnackcenter_an
    description: ""
    triggers: []
    conditions: []
    actions:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          area_id: schnackcenter
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          area_id: schnackcenter
    mode: single