view espNode/tasks.py @ 1738:7d270896d6d0

reformat
author drewp@bigasterisk.com
date Fri, 01 Sep 2023 17:11:22 -0700
parents 82213d91471c
children 097bfd91187d
line wrap: on
line source

from invoke import task
from pathlib import Path

port = '/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0'

tag = 'docker.io/esphome/esphome:2022.7.0-dev20220807'

esphome = (
    'docker run --rm ' +  #
    '-v `pwd`:/config ' +  #
    '-v /usr/share/fonts:/usr/share/fonts ' +  #
    '-v /dev:/dev ' +  #
    '-v /tmp:/tmp ' +  #
    f'-it {tag}')


@task
def get_dev_esphome(ctx):
    ctx.run(
        f'docker build -t esphome_dev -f docker/Dockerfile https://github.com/MasterTim17/esphome.git#dev'
    )


@task
def pull_esphome(ctx):
    ctx.run(f"docker pull {tag}")


@task
def program_board_over_usb(ctx, board):
    board = board.replace('.yaml', '')
    print(
        'connect gnd, 3v3, rx/tx per https://randomnerdtutorials.com/esp32-cam-video-streaming-web-server-camera-home-assistant/, '
    )
    print(
        'rts to reset (if possible), dtr to gpio0 per https://github.com/espressif/esptool/wiki/ESP32-Boot-Mode-Selection#automatic-bootloader'
    )
    ctx.run(f"{esphome} run {board}.yaml --device={port}", pty=True, echo=True)


@task
def program_board_over_wifi(ctx, board):
    global esphome
    board = board.replace('.yaml', '')
    if board == 'theater_lcd':
        tag = 'esphome_dev'
        esphome = 'esphome-local/env/bin/esphome'

    ctx.run(f"{esphome} {board}.yaml run", pty=True)


@task
def monitor_usb(ctx, board):
    board = board.replace('.yaml', '')
    ctx.run(f"{esphome} logs {board}.yaml --device={port}", pty=True)


# device up?
#  nmap -Pn -p 3232,6053 10.2.0.21


@task
def program_cam_usb(ctx, name='0'):
    idVendor = '1a86'
    port = f'/dev/serial/by-id/usb-{idVendor}_USB_Serial-if00-port0'
    ctx.run(
        f"{esphome} "  #
        + f"-s camname cam{name}"  #
        + " run cam.yaml "  #
        + f'--device={port} ',
        pty=True,
        echo=True)


# -s k v seemed to have no effect
def config_replace(in_file, out_file, repls):
    yaml_text = open(in_file, 'rt').read()
    for k, v in repls.items():
        yaml_text = yaml_text.replace(k, str(v))
    with open(out_file, 'wt') as out:
        out.write(yaml_text)


def prep_tmp(ctx, project):
    tmp = Path(f'/tmp/esphome_build/{project}')
    ctx.run(f'mkdir -p {tmp}')
    ctx.run(f'rsync -a component secrets.yaml {tmp}')
    return tmp


@task
def program_cam_ota(ctx, name='0'):
    tmp = prep_tmp(ctx, 'cam')
    tmpyaml = tmp / 'cam.yaml'
    config_replace('cam.yaml', tmpyaml, {
        '$build_path': tmp,
        '$addr': '10.2.0.22',
        '$name': f'cam{name}'
    })
    ctx.run(
        f"{esphome} "  #
        + f"run {tmpyaml} "  #
        + "--device=OTA --no-logs"  #
        ,
        pty=True,
        echo=True)