# ESP32 PoE board with ZigBee module (cc2652) connected to it via serial.
# The purpose is to expose that serial port via TCP.
#
# cc2652p firmware (launchpad):
# https://github.com/Koenkk/Z-Stack-firmware/tree/master/coordinator/Z-Stack_3.x.0
#
# Binary files can be found at:
# https://github.com/Koenkk/Z-Stack-firmware/releases
# Latest flashed copy can be found in tubeszb/ directory.
#
# Coordinator flashing instructions:
# https://www.zigbee2mqtt.io/guide/adapters/flashing/flashing_via_cc2538-bsl.html
#
# Router flashing instructions:
# https://github.com/tube0013/tube_gateways/tree/8cf2d2998602d0d89ff2cd9ce06dbd98997e223d/models/current/tubeszb-router#flashing-the-router

esphome:
  name: "zigbee-coordinator-ch${channel}"
  friendly_name: "ZigBee Coordinator CH${channel}"

  project:
    name: "dasfoo.zigbee-coordinator-ch${channel}"
    version: "5.0"

  on_boot:
    priority: 600  # Default; most sensors set up, no network yet.
    then:
    - script.execute: cc2652_reset

packages:
  device_base: !include esp32-poe.yaml

esp32:
  framework:
    type: esp-idf

external_components:
  - source: github://oxan/esphome-stream-server

script:
- id: fw_update_mode
  then:
  - switch.turn_on: zBSL
  - delay: 1s
  - switch.turn_on: zRST_gpio
  - delay: 1s
  - switch.turn_off: zRST_gpio
  - logger.log: "Delaying ~10 seconds for cc2652 to settle"
  - delay: 11s
  - switch.turn_off: zBSL
  - logger.log: "Please try update with cc2538-bsl tool now"
  - logger.log: "cc-bsl usage: cc2538-bsl.py -p socket://ip-of-gw:6638 -evw firmware.hex"

- id: cc2652_reset
  then:
  - switch.turn_on: zRST_gpio
  - delay: 15ms
  - switch.turn_off: zRST_gpio

button:
- platform: template
  name: "ZigBee Module reset"
  disabled_by_default: true
  id: zRST
  on_press:
  - script.execute: cc2652_reset

- platform: template
  name: "ZigBee Module enter programming mode"
  disabled_by_default: true
  on_press:
  - script.execute: fw_update_mode

stream_server:
  uart_id: uart_bus
  id: ss
  port: 6638
  buffer_size: 1024

binary_sensor:
- platform: stream_server
  connected:
    name: "ZigBee Module serial stream connected"
