annotate espNode/tasks.py @ 1749:4b29ce991e59

cloudfree plug sends mqtt metrics, which we export to victoriametrics
author drewp@bigasterisk.com
date Sun, 28 Apr 2024 16:01:38 -0700
parents 097bfd91187d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
1 from invoke import task
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
2 from pathlib import Path
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
3
1741
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
4 port = 'usb-1a86_USB_Serial-if00-port0'
1701
33747dcf57ea rf_switch_desk update
drewp@bigasterisk.com
parents: 1678
diff changeset
5
1741
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
6 tag = 'docker.io/esphome/esphome:2023.2.4'
1701
33747dcf57ea rf_switch_desk update
drewp@bigasterisk.com
parents: 1678
diff changeset
7
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
8 esphome = (
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
9 'docker run --rm ' + #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
10 '-v `pwd`:/config ' + #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
11 '-v /usr/share/fonts:/usr/share/fonts ' + #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
12 '-v /dev:/dev ' + #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
13 '-v /tmp:/tmp ' + #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
14 f'-it {tag}')
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
15
800
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
16
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
17 @task
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
18 def get_dev_esphome(ctx):
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
19 ctx.run(
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
20 f'docker build -t esphome_dev -f docker/Dockerfile https://github.com/MasterTim17/esphome.git#dev'
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
21 )
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
22
686
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
23
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
24 @task
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
25 def pull_esphome(ctx):
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
26 ctx.run(f"docker pull {tag}")
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
27
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
28
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
29 @task
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
30 def program_board_over_usb(ctx, board):
800
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
31 board = board.replace('.yaml', '')
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
32 print(
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
33 'connect gnd, 3v3, rx/tx per https://randomnerdtutorials.com/esp32-cam-video-streaming-web-server-camera-home-assistant/, '
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
34 )
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
35 print(
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
36 'rts to reset (if possible), dtr to gpio0 per https://github.com/espressif/esptool/wiki/ESP32-Boot-Mode-Selection#automatic-bootloader'
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
37 )
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
38 ctx.run(f"{esphome} run {board}.yaml --device={port}", pty=True, echo=True)
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
39
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
40
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
41 @task
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
42 def program_board_over_wifi(ctx, board):
1716
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
43 global esphome
800
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
44 board = board.replace('.yaml', '')
1716
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
45 if board == 'theater_lcd':
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
46 tag = 'esphome_dev'
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
47 esphome = 'esphome-local/env/bin/esphome'
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
48
800
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
49 ctx.run(f"{esphome} {board}.yaml run", pty=True)
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
50
1716
2bed2f68243c older work
drewp@bigasterisk.com
parents: 1701
diff changeset
51
596
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
52 @task
a56bc73d308c espNode code
drewp@bigasterisk.com
parents:
diff changeset
53 def monitor_usb(ctx, board):
800
b311e6ca7bbd use local esphome dev version
drewp@bigasterisk.com
parents: 773
diff changeset
54 board = board.replace('.yaml', '')
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
55 ctx.run(f"{esphome} logs {board}.yaml --device={port}", pty=True)
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
56
686
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
57
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
58 # device up?
438edc93e29e add task to pull new docker image
drewp@bigasterisk.com
parents: 596
diff changeset
59 # nmap -Pn -p 3232,6053 10.2.0.21
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
60
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
61
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
62
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
63 # -s k v seemed to have no effect
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
64 def config_replace(in_file, out_file, repls):
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
65 yaml_text = open(in_file, 'rt').read()
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
66 for k, v in repls.items():
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
67 yaml_text = yaml_text.replace(k, str(v))
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
68 with open(out_file, 'wt') as out:
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
69 out.write(yaml_text)
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
70
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
71
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
72 def prep_tmp(ctx, project):
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
73 tmp = Path(f'/tmp/esphome_build/{project}')
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
74 ctx.run(f'mkdir -p {tmp}')
1718
82213d91471c new cam component with http server
drewp@bigasterisk.com
parents: 1717
diff changeset
75 ctx.run(f'rsync -a component secrets.yaml {tmp}')
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
76 return tmp
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
77
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
78
1741
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
79 def replace_yaml_for_cam(inPath, outPath, camName):
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
80 config_replace(inPath, outPath, {
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
81 '$build_path': outPath.parent,
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
82 '$addr': '10.2.0.22',
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
83 '$name': camName,
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
84 })
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
85
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
86 @task
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
87 def program_cam_usb(ctx, name='cam-foo'):
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
88 idVendor = '1a86'
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
89 port = f'/dev/serial/by-id/usb-{idVendor}_USB_Serial-if00-port0'
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
90 tmp = prep_tmp(ctx, 'cam')
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
91 tmpyaml = tmp / 'cam.yaml'
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
92 replace_yaml_for_cam("cam.yaml", tmpyaml, name)
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
93 ctx.run(
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
94 f"{esphome} " #
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
95 + f" run {tmpyaml} " #
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
96 + f'--device={port} ',
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
97 pty=True,
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
98 echo=True)
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
99
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
100 @task
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
101 def program_cam_ota(ctx, name='0'):
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
102 tmp = prep_tmp(ctx, 'cam')
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
103 tmpyaml = tmp / 'cam.yaml'
1741
097bfd91187d cam deploy code that might not work
drewp@bigasterisk.com
parents: 1718
diff changeset
104 replace_yaml_for_cam("cam.yaml", tmpyaml, name)
1717
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
105 ctx.run(
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
106 f"{esphome} " #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
107 + f"run {tmpyaml} " #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
108 + "--device=OTA --no-logs" #
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
109 ,
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
110 pty=True,
e9540ee0cf73 starting 2022 attempt at esp32cam
drewp@bigasterisk.com
parents: 1716
diff changeset
111 echo=True)