esphome:
  name: blinds
  friendly_name: "Blinds"
  area: "Entryway"

  areas:
    - id: wintergarten
      name: Wintergarten

  devices:
    - id: meater_device
      name: Meater
      area_id: wintergarten

  project:
    name: dasfoo.blinds
    version: "4.0"

esp32:
  framework:
    type: esp-idf

packages:
  device_base: !include templates/esp32-poe.yaml
  api: !include templates/api.yaml # for multi-device

external_components:
  - source: components

spi:
  clk_pin: 14
  miso_pin: 16
  mosi_pin: 15

cc1101:
  id: transceiver
  cs_pin: 13
  frequency: 433.34MHz

remote_transmitter:
  pin: 4
  carrier_duty_percent: 100%
  id: somfy_remote_transmitter
  on_transmit:
    then:
      - cc1101.begin_tx: transceiver
  on_complete:
    then:
      - cc1101.set_idle: transceiver

cover:
  - name: "Office window"
    remote_id_offset: 0
    <<: &somfy_cover
      platform: somfy
      transmitter: somfy_remote_transmitter
      # Can be retrieved from MQTT server. Take the key with lowest integer value
      # from /$board/rolling_code/ path.
      remote_id_base: !secret somfy_remote_id_base

  - name: "Bedroom"
    remote_id_offset: 1
    <<: *somfy_cover
  - name: "Living Room side"
    remote_id_offset: 2
    # Timing: open 45s, close 44s
    <<: *somfy_cover
  - name: "Wardrobe"
    remote_id_offset: 3
    <<: *somfy_cover
  - name: "Child West door"
    remote_id_offset: 4
    <<: *somfy_cover
  - name: "Child West window"
    remote_id_offset: 5
    <<: *somfy_cover
  - name: "Hallway Stairs window"
    remote_id_offset: 6
    <<: *somfy_cover
  - name: "Kitchen window"
    remote_id_offset: 7
    <<: *somfy_cover
  - name: "Kitchen door"
    remote_id_offset: 8
    <<: *somfy_cover
  - name: "Living Room door"
    remote_id_offset: 9
    # Timing: open 46s, close 45s
    <<: *somfy_cover
  - name: "Living Room window"
    remote_id_offset: 10
    <<: *somfy_cover
  - name: "Child East window"
    remote_id_offset: 11
    <<: *somfy_cover
  - name: "Child East door"
    remote_id_offset: 12
    <<: *somfy_cover

  - platform: template
    name: "Wintergarten"
    device_class: awning
    optimistic: true
    assumed_state: true # always show both buttons
    stop_action:
      - switch.turn_off: wintergarten_deploy
      - switch.turn_off: wintergarten_retract
    open_action:
      - switch.turn_off: wintergarten_deploy
      - switch.turn_on: wintergarten_retract
      - delay: 35s
      - switch.turn_off: wintergarten_retract # TODO: check last action
    close_action:
      - switch.turn_off: wintergarten_retract
      - switch.turn_on: wintergarten_deploy
      - delay: 35s
      - switch.turn_off: wintergarten_deploy # TODO: check last action

switch:
  - platform: gpio
    pin: GPIO32
    id: wintergarten_deploy
    restore_mode: ALWAYS_OFF
    inverted: true

  - platform: gpio
    pin: GPIO33
    id: wintergarten_retract
    restore_mode: ALWAYS_OFF
    inverted: true

ble_client:
  - mac_address: "B8:1F:5E:0F:BB:2F"
    id: meater

sensor:
  - name: "Tip temperature"
    device_id: meater_device
    platform: ble_client
    type: characteristic
    ble_client_id: meater
    service_uuid: "a75cc7fc-c956-488f-ac2a-2dbc08b63a04"
    characteristic_uuid: "7edda774-045e-4bbf-909b-45d1991a2876"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    device_class: "temperature"
    state_class: "measurement"
    notify: true
    lambda: |-
      float tip_temp = (x[0] + (x[1] << 8) + 8.0) / 16.0;
      return tip_temp;
  - name: "Ambient temperature"
    device_id: meater_device
    platform: ble_client
    type: characteristic
    ble_client_id: meater
    service_uuid: "a75cc7fc-c956-488f-ac2a-2dbc08b63a04"
    characteristic_uuid: "7edda774-045e-4bbf-909b-45d1991a2876"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    device_class: "temperature"
    state_class: "measurement"
    notify: true
    lambda: |-
      uint16_t tip = x[0] + (x[1] << 8);
      uint16_t ra = x[2] + (x[3] << 8);
      uint16_t oa = x[4] + (x[5] << 8);
      uint16_t min_val = 48;
      float ambient = (tip + std::max(0., (((ra - std::min(min_val, oa)) * 16. * 589.) / 1487.)) + 8.0) / 16.;
      return ambient;
  - name: "Battery level"
    device_id: meater_device
    platform: ble_client
    type: characteristic
    ble_client_id: meater
    service_uuid: "a75cc7fc-c956-488f-ac2a-2dbc08b63a04"
    characteristic_uuid: "2adb4877-68d8-4884-bd3c-d83853bf27b8"
    unit_of_measurement: "%"
    device_class: "battery"
    state_class: "measurement"
    notify: true
    lambda: |-
      uint16_t battery = (x[0] + x[1]) * 10;
      return battery;
  - name: "RSSI"
    device_id: meater_device
    platform: ble_client
    type: rssi
    ble_client_id: meater
    device_class: "signal_strength"
    state_class: "measurement"
    entity_category: diagnostic

text_sensor:
  - name: "Firmware"
    device_id: meater_device
    platform: ble_client
    ble_client_id: meater
    service_uuid: "180A"
    entity_category: diagnostic
    characteristic_uuid: "00002a26-0000-1000-8000-00805f9b34fb"
    filters:
      - lambda: |-
          if (x.size()) {
            std::string data_string(x.begin(), x.end());
            return data_string;
          }
          return {};
