# ESP32

- [ESP32](#esp32)
  - [Install](#install)
  - [Devices](#devices)
    - [Lilygo tdongle](#lilygo-tdongle)
  - [Local Flashing](#local-flashing)
  - [Adding a New Device](#adding-a-new-device)
  - [Controlling Home Assistant](#controlling-home-assistant)
  - [Configuration Sections](#configuration-sections)
    - [esphome](#esphome)
    - [esp32](#esp32-1)
    - [logger](#logger)
    - [api](#api)
    - [wifi](#wifi)
    - [ota](#ota)
    - [captive portal](#captive-portal)
    - [button](#button)
    - [i2s audio](#i2s-audio)
    - [microphone](#microphone)
    - [speaker](#speaker)
    - [media player](#media-player)
    - [voice assistant](#voice-assistant)
    - [micro wake word](#micro-wake-word)
    - [light](#light)
    - [binary sensor](#binary-sensor)
    - [lambda](#lambda)
    - [Display](#display)

## Install

```bash
# Check that you have python 3.11 installed
uv python list --only-installed

# Create the venv (python 3.11 is recommended in the docs)
uv venv --python 3.11

# Install esphome
uv pip install esphome wheel pip
source .venv/bin/activate
```

## Devices

### Lilygo tdongle

Display: 80 X 160

## Local Flashing

Make sure your permissions are set correctly

```bash
sudo usermod -a -G dialout ducoterra
```

Then "run" your config file

```bash
cd active/device_esphome
uv venv
uv pip install esphome
source .venv/bin/activate

esphome run m5stack-atom-echo.yaml
```

## Adding a New Device

1. Create a new yaml configuration file called "my-device-device-type.yaml"

## Controlling Home Assistant

<https://esphome.io/components/api/#api-actions>

## Configuration Sections

<https://esphome.io/components/>

### esphome

### esp32

<https://esphome.io/components/esp32/#configuration-variables>

### logger

<https://esphome.io/components/logger/>

### api

<https://esphome.io/components/api/>

### wifi

<https://esphome.io/components/wifi/>

### ota

<https://esphome.io/components/ota/>

<https://esphome.io/components/ota/esphome/>

### captive portal

<https://esphome.io/components/captive_portal/>

### button

<https://esphome.io/components/button/>

### i2s audio

<https://esphome.io/components/i2s_audio/>

### microphone

<https://esphome.io/components/microphone/>

<https://esphome.io/components/microphone/i2s_audio/>

### speaker

<https://esphome.io/components/speaker/i2s_audio/>

### media player

<https://esphome.io/components/media_player/speaker/>

Sometimes you'll need to convert media files to supported encoders.

```bash
ffmpeg -i input.flac output.wav
```

To play media on other devices from home assistant, put the 

```yaml
action: media_player.play_media
target:
  entity_id: media_player.kitchen_google_home
data:
  media_content_type: "audio/wav"
  media_content_id: "media-source://media_source/local/wake_word_triggered.wav"
```

### voice assistant

<https://esphome.io/components/voice_assistant/>

In Home Assistant's configuration.yaml, add the following to listen to
audio recordings of your voice request:

```bash
assist_pipeline:
   debug_recording_dir: /share/assist_pipeline
```

### micro wake word

<https://esphome.io/components/micro_wake_word/>

### light

<https://esphome.io/components/light/#light-effects>

### binary sensor

<https://esphome.io/components/binary_sensor/>

### lambda

<https://esphome.io/automations/templates/#config-lambda>

> id(...) is a helper function that makes ESPHome fetch an object with the
> supplied ID (which you defined somewhere else, like top_end_stop ) and lets
> you call any of ESPHome’s many APIs directly. For example, here we’re
> retrieving the current state of the end stop using .state and using it to
> construct our cover state.

### Display

Display pages

```yaml
display:
  - platform: st7735
    spi_id: spi_lcd
    model: "INITR_MINI160X80"
    reset_pin: GPIO1
    cs_pin: GPIO4
    dc_pin: GPIO2
    rotation: 270
    device_width: 82
    device_height: 161
    col_start: 0
    row_start: 0
    eight_bit_color: true
    invert_colors: true
    use_bgr: true
    auto_clear_enabled: true
    id: my_display
    pages:
      - id: page1
        lambda: |-
          it.print(0, 10, id(font_roboto), "Connecting to");
          it.print(0, 30, id(font_roboto), "Home Assistant...");
      - id: page2
        lambda: |-
          it.print(0, 10, id(font_roboto), "Configuring");
          it.print(0, 30, id(font_roboto), "sensors...");
      - id: page3
        lambda: |-
          it.print(0, 10, id(font_roboto), "Loading");
          it.print(0, 30, id(font_roboto), "important");
          it.print(0, 50, id(font_roboto), "update...");
      - id: page4
        lambda: |-
          it.image(0, 0, id(my_image), COLOR_OFF, COLOR_ON);
```

Switch pages

```yaml
interval:
  - interval: 5s
    then:
      - display.page.show_next: my_display
      - component.update: my_display
```

Show an image

```yaml
image:
  - file: "test_tdongle_image.png"
    type: RGB
    id: my_image
```

Specify a font

```yaml
font:
  - file: "gfonts://Roboto"
    id: font_roboto
    size: 20
```
