#for documentation see project at https://github.com/Dilbert66/esphome-VistaECP
substitutions:
  systemName: "vistaalarm"
  accessCode: !secret access_code #Only comes into effect if needed for arming and quickarm is not set
  keypadAddr: "17" #set this to an unused keypad address slot. Make sure to enable it in  your vista panel programming
 
  ##esp32
  #rxPin: "22" #GPIO pin to use for data receive (yellow line) 
  #txPin: "21" #GPIO pin to use for data transmit (green line)
  #monitorPin: "18" #GPIO pin to use for monitoring module traffic such as RF or Expanders . Set to -1 to disable
  
  ##esp8266
  rxPin: "5" #GPIO pin to use for data receive (yellow line) 
  txPin: "4" #GPIO pin to use for data transmit (green line)
  monitorPin: "14" #GPIO pin to use for monitoring module traffic such as RF or Expanders . Set to -1 to disable
  
  # module addresses:
  # 07 4229 zone expander  zones 9-16
  # 08 4229 zone expander zones 17-24
  # 09 4229 zone expander zones 25-32
  # 10 4229 zone expander zones 33-40
  # 11 4229 zone expander zones 41 48
  # 12 4204 relay module  
  # 13 4204 relay module
  # 14 4204 relay module
  # 15 4204 relay module
  
  expanderAddr1: "0" # 1st zone expander emulator (4229) address to use . Set to 0 to disable. 
  expanderAddr2: "0" # 2nd expander emulator address to use . Set to 0 to disable. 
  
  relayAddr1: "0" # relay module emulation (4204) addresses. Set to 0 to disable
  relayAddr2: "0"
  relayAddr3: "0"
  relayAddr4: "0"
  
  TTL: "30000"  # time  to live  in ms for zone/fire status before expiring;
  quickArm: "false"
  lrrSupervisor: "false" # set to true if we don't have an LRR monitoring supervisor we can emulate one to get the statuses
  
globals:
       #persistent storage variables
    - id: zoneStates  #persistant storage for zone states in case of reboot
      type: int
      restore_value: yes
    - id: zoneAlarms  #persistant storage for zone states in case of reboot
      type: int
      restore_value: yes
    - id: zoneBypass  #persistant storage for zone states in case of reboot
      type: int
      restore_value: yes
    - id: zoneChecks  #persistant storage for zone states in case of reboot
      type: int
      restore_value: yes      
    - id: lrrCode  #persistant storage for last lrr message in case of reboot
      type: int
      restore_value: yes      
  
      

esphome:
  name: $systemName
  #platform: ESP32
  #board: nodemcu-32s
  platform: ESP8266
  board: nodemcuv2

  # subdirectory where library *.h and *.cpp are placed 
  includes:
    - vistaEcpInterface/

wifi:
  ssid: !secret wifi_ssid3
  password: !secret wifi_password3

  ap:
    ssid: "$systemName"
    password: !secret wifi_password

logger:
  baud_rate: 115200
  level: DEBUG
  
api:
   password: !secret api_password  
   #encryption:
     #key: !secret encryption_key   
   
ota:
   password: !secret ota_password
   safe_mode: True
   on_begin:
    - lambda: |-
        disconnectVista();
   
#status_led:
 # pin:
  #  number: GPIO2
   # inverted: yes

custom_component:
- lambda: |-
    auto VistaECP = new vistaECPHome($keypadAddr,$rxPin,$txPin,$monitorPin);
    VistaECP->accessCode="$accessCode";
    VistaECP->quickArm=$quickArm;
    VistaECP->expanderAddr1=$expanderAddr1; //zone expander
    VistaECP->expanderAddr2=$expanderAddr2;
    VistaECP->relayAddr1=$relayAddr1; //relay module 
    VistaECP->relayAddr2=$relayAddr2;
    VistaECP->relayAddr3=$relayAddr3; 
    VistaECP->relayAddr4=$relayAddr4;     
    VistaECP->lrrSupervisor=$lrrSupervisor;
    VistaECP->TTL=$TTL;
    VistaECP->debug=1;
    VistaECP->onSystemStatusChange([&](std::string statusCode) {
       id(system_status).publish_state(statusCode);
    });
    VistaECP->onLrrMsgChange([&](std::string msg) {
        id(m1).publish_state(msg); 
    }); 
    VistaECP->onRfMsgChange([&](std::string msg) {
        id(rf1).publish_state(msg); 
    });    
    VistaECP->onLine1DisplayChange([&](std::string msg) {
        id(l1).publish_state(msg); 
    });  
    VistaECP->onLine2DisplayChange([&](std::string msg) {
        id(l2).publish_state(msg); 
    }); 
    VistaECP->onBeepsChange([&](std::string beeps) {
        id(beep1).publish_state(beeps); 
    });      
    VistaECP->onStatusChange([&](sysState led,bool open) {
      switch(led) {
        case sfire: id(fire).publish_state(open);break;
        case salarm: id(alarm1).publish_state(open);break;
        case strouble: id(trouble).publish_state(open);break;
        case sarmedstay: id(stay).publish_state(open);break;
        case sarmedaway: id(away).publish_state(open);break;
        case sinstant: id(instant).publish_state(open);break; 
        case sready: id(ready).publish_state(open);break; 
        case sac: id(ac).publish_state(open);break;          
        case sbypass: id(bypass).publish_state(open);break;  
        case schime: id(chime).publish_state(open);break;
        case sbat: id(bat).publish_state(open);break;
        case scheck: id(check).publish_state(open);break;
        case sarmednight: id(night).publish_state(open);break;   
        case sarmed: id(armed).publish_state(open);break;       
        default: break;
        }
    });
    VistaECP->onZoneStatusChange([&](uint8_t zone, std::string open) {
      switch (zone) {
        case 1: id(z1).publish_state(open); break;
        case 2: id(z2).publish_state(open); break;
        case 3: id(z3).publish_state(open); break;
        case 4: id(z4).publish_state(open); break;
        case 5: id(z5).publish_state(open); break;
        case 6: id(z6).publish_state(open); break;
        case 7: id(z7).publish_state(open); break;
        case 8: id(z8).publish_state(open); break;
        
        case 9: id(z9).publish_state(open); break;
        case 10: id(z10).publish_state(open); break;
       
        case 17: id(z17).publish_state(open); break;
      }
    }); //you can add more zones above . Also add the text sensor entry below
    
    VistaECP->onRelayStatusChange([&](uint8_t addr,uint8_t zone,bool open) {
     switch(addr) {
     case 12: switch (zone) {
                case 1: id(r1).publish_state(open); break;
                case 2: id(r2).publish_state(open); break;
              }
              break;
      case 13: break;
      }
    });  //add as many case and switch statements as needed to control your binary sensor outputs      
    return {VistaECP};

binary_sensor:
 #- platform: gpio  #example use of pin d8 as a zone trigger port for the emulated zone expander
  #  pin: D8
  #  id: pind8
  #  device_class: window
   # on_press:       #zone,on/off
   #   - lambda: |-
   #       vista.setExpFault(17,1); 
  #  on_release:
  #    - lambda: |-
   #       vista.setExpFault(17,0);  
    
    #system status indicator definitions
  - platform: template
    id: trouble
    name: "$systemName Trouble"
    #device_class: problem
    
  - platform: template
    id: bypass
    name: "$systemName Bypass"
    
  - platform: template
    id: away
    name: "$systemName Away"
    
  - platform: template
    id: armed
    name: "$systemName Armed"    
    
  - platform: template
    id: stay
    name: "$systemName Stay"
    
  - platform: template
    id: instant
    name: "$systemName Instant"
    
  - platform: template
    id: night
    name: "$systemName Night"
    
  - platform: template
    id: ac
    name: "$systemName AC"
    device_class: plug  
    
  - platform: template
    id: chime
    name: "$systemName Chime"
    
  - platform: template
    id: check
    name: "$systemName Check"
    device_class: problem  

  - platform: template
    id: alarm1
    name: "$systemName Alarm"
    
  - platform: template
    id: bat
    name: "$systemName Battery"
    device_class: problem
    
  - platform: template
    id: fire
    device_class: smoke
    name: "$systemName Fire"

  - platform: template
    id: ready
    name: "$systemName Ready"
    #device_class: problem
    
    #relay module channels add as many as you need.  To hide, comment out the name: attribute
  - platform: template
    id: r1
    name: "$systemName Relay1"

  - platform: template
    id: r2
    name: "$systemName Relay2"   
   
    #zone definitions.  Add more (also add to the switch statment above). To hide, comment out the name: attribute
text_sensor:
    #zone definitions
  - platform: template
    id: z1
    name: "$systemName Front door"

  - platform: template
    id: z2
    name: "$systemName Garage door"

  - platform: template
    id: z3
    name: "$systemName Back door"

  - platform: template
    id: z4
    name: "$systemName Living room window"

  - platform: template
    id: z5
    name: "$systemName Dining room window"

  - platform: template
    id: z6
    name: "$systemName Family room window LF"

  - platform: template
    id: z7
    name: "$systemName Family room window RF"

  - platform: template
    id: z8
    name: "$systemName Basement windows"
    
  - platform: template
    id: z9
    name: "$systemName Basement windows9"
    
  - platform: template
    id: z10
    name: "$systemName Basement windows10"
 

  - platform: template
    id: z17
    name: "$systemName zone 17"

    #system status 
  - platform: template
    id: system_status
    name: "$systemName System Status"
    icon: "mdi:shield"

  - platform: template
    id: m1
    name: "$systemName Lrr Msg"
    icon: "mdi:alert-box"
    
  - platform: template
    id: rf1
    name: "$systemName RF Msg"
    icon: "mdi:alert-box"    

  - platform: template
    id: l1
    name: "$systemName Line1"
 

  - platform: template
    id: l2
    name: "$systemName Line2"
    
  - platform: template
    id: beep1
    name: "$systemName Beeps"    

switch:
  - platform: template
    name: "$systemName Connection"
    id: connection_status_switch
    lambda: |-
      return vista.keybusConnected;
    icon: "mdi:shield-link-variant"
    turn_on_action:
      - switch.toggle: restart_switch
    turn_off_action:
      - lambda: |-
          disconnectVista();
  - platform: restart
    id: restart_switch

