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

# https://www.home-assistant.io/integrations/ping/
binary_sensor:
  - platform: ping
    host: TL-SG3452X
    name: "Switch02"
    count: 3
    scan_interval: 5 

# 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 

# 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 
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

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

  # send zonenstatus to MQTT, REST
  - alias: 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: single

  # send zonenstatus to telegram
  - alias: 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: single
