-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathMakefile.common
More file actions
87 lines (74 loc) · 3.34 KB
/
Makefile.common
File metadata and controls
87 lines (74 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
.DEFAULT_GOAL := help
# Makefile.common - Common ESPHome Makefile for single and multi-board projects
# Usage: In your project's Makefile, set PROJECT and optionally BOARD, then include this file.
PROJECT ?= default-project
BOARD ?=
TARGET ?= $(if $(BOARD),$(PROJECT)-$(BOARD).yml,$(PROJECT).yml)
BOARD_FILES ?= $(if $(BOARD),boards/$(BOARD).yml)
ESPHOME_VERSION ?= $(shell grep '^esphome==' requirements.txt | cut -d'=' -f3)
UV_CHECK = @command -v uv >/dev/null 2>&1 || { \
echo "[ERROR] 'uv' is not installed. Please install it with:"; \
echo "See https://docs.astral.sh/uv/getting-started/installation/ for help."; \
exit 1; \
}
# Check if 'uv' is installed
check-uv:
$(UV_CHECK)
# Compile the binary only if YAML sources or dependencies change
compile: .esphome/build/$(PROJECT)/.pioenvs/$(PROJECT)/firmware.bin ## Read the configuration and compile the binary.
validate-config: check-uv ## Validate the configuration without building the binary
uvx --with pip esphome==$(ESPHOME_VERSION) config $(TARGET)
# Build firmware.bin if YAML sources or dependencies change
.esphome/build/$(PROJECT)/.pioenvs/$(PROJECT)/firmware.bin: $(TARGET) packages/*.yml $(BOARD_FILES) components/**/*.* requirements.txt $(shell command -v uv 2>/dev/null)
$(UV_CHECK)
uvx --with pip esphome==$(ESPHOME_VERSION) compile $(TARGET)
upload: .esphome/build/$(PROJECT)/.pioenvs/$(PROJECT)/firmware.bin check-uv ## Validate the configuration, create a binary, upload it, and start logs.
@suffix="$(HOST_SUFFIX)"; \
if [ -z "$$suffix" ] && [ -f .selected_suffix ]; then \
suffix=$$(cat .selected_suffix); \
fi; \
if [ -z "$$suffix" ]; then \
uvx --with pip esphome==$(ESPHOME_VERSION) run $(TARGET); \
else \
uvx --with pip esphome==$(ESPHOME_VERSION) run $(TARGET) --device $(PROJECT)$$suffix; \
fi
logs: check-uv ## Start logs.
@suffix="$(HOST_SUFFIX)"; \
if [ -z "$$suffix" ] && [ -f .selected_suffix ]; then \
suffix=$$(cat .selected_suffix); \
fi; \
if [ -z "$$suffix" ]; then \
uvx --with pip esphome==$(ESPHOME_VERSION) logs $(TARGET); \
else \
uvx --with pip esphome==$(ESPHOME_VERSION) logs $(TARGET) --device $(PROJECT)$$suffix; \
fi
discover: ## Discover ESPHome devices on the network
@PROJECT=$(PROJECT) bash -c ' \
echo "Scanning network for ESPHome devices..."; \
devices=($$(timeout 2 dns-sd -B _esphomelib._tcp local 2>/dev/null | grep "Add" | grep "$$PROJECT" | awk "{print \$$NF}" | sed "s/$$PROJECT-//" | sed "s/\.local//")); \
if [ $${#devices[@]} -eq 0 ]; then \
echo "No devices found (or dns-sd timed out)"; \
else \
selected=$${devices[0]}; \
echo "Using device: $$PROJECT-$$selected"; \
echo "-$$selected" > .selected_suffix; \
if [ $${#devices[@]} -gt 1 ]; then \
echo "Other devices found:"; \
for i in $${!devices[@]}; do \
if [ $$i -gt 0 ]; then \
echo " $$PROJECT-$${devices[i]}"; \
fi; \
done; \
fi; \
fi'
.PHONY: clean
clean: ## Remove the esphome build directory
rm -rf .esphome .selected_suffix
.PHONY: help
help: ## Show help messages for make targets
@grep -E '^[a-zA-Z0-9_./-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sed 's/[^:]*://' \
| sort \
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
compile_docker: $(TARGET) packages/*.yml $(BOARD_FILES) ## Compile the binary using docker
docker run --rm -v $(PWD):/config ghcr.io/esphome/esphome:$(ESPHOME_VERSION) compile /config/$(TARGET)