esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 34 forks source link

Cannot use !include with color #3316

Closed michaelblight closed 2 years ago

michaelblight commented 2 years ago

The problem

I have a number of components stored in separate files, and tried to do the same for color.

binary_sensor:
- <<: !include include/binary_sensor/connection-status.yaml

color:
- <<: !include include/color/named.yaml

The IDE is happy with it (no red x on line 1), but installing produces the compile error shown below. If I paste the content of the file (shown below) instead of the !include, it all works ok.

- id: color_blue
  red: 0%
  green: 100%
  blue: 0%
- id: color_green
  red: 0%
  green: 100%
  blue: 0%
- id: color_orange
  red: 100%
  green: 65%
  blue: 0%
- id: color_orange_deep
  red: 93%
  green: 46%
  blue: 0%
- id: color_red
  red: 100%
  green: 0%
  blue: 0%

The same thing occurs if I try to do it in a file referenced in device_base under packages.

Note that it works if I put the color: inside the file as well, and change my main yaml to:

binary_sensor:
- <<: !include include/binary_sensor/connection-status.yaml

<<: !include include/color/named.yaml

But I'm wanting to include multiple files like I do with sensors.

Which version of ESPHome has the issue?

2022.5.0

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2022.5.4

What platform are you using?

ESP32

Board

m5stick-c

Component causing the issue

color

Example YAML snippet

See above

Anything in the logs that might be useful for us?

INFO Reading configuration /config/esphome/m5stick-c-1.yaml...
WARNING GPIO15 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING Pin 10 (9-10) might already be used by the flash interface in QUAD IO flash mode.
INFO Generating C++ source...
INFO Compiling app...
Processing m5stick-c-1 (board: m5stack-core-esp32; framework: arduino; platform: platformio/espressif32 @ 3.5.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- <AsyncTCP-esphome> 1.2.2
|-- <WiFi> 1.0
|-- <FS> 1.0
|-- <Update> 1.0
|-- <ESPAsyncWebServer-esphome> 2.1.0
|   |-- <AsyncTCP-esphome> 1.2.2
|-- <DNSServer> 1.1.0
|-- <ESPmDNS> 1.0
|-- <SPI> 1.0
|-- <Wire> 1.0.1
Compiling /data/m5stick-c-1/.pioenvs/m5stick-c-1/src/main.cpp.o
/config/esphome/m5stick-c-1.yaml: In lambda function:
/config/esphome/m5stick-c-1.yaml:44:32: error: 'color_orange_deep' was not declared in this scope
       it.print(80, 10, id(font_1), color_orange_deep, TextAlign::TOP_LEFT, "Test 1");
                                ^
/config/esphome/m5stick-c-1.yaml:48:34: error: 'color_green' was not declared in this scope
       it.rectangle(80, 41, 1, 1, color_green);      // Centre
                                  ^
*** [/data/m5stick-c-1/.pioenvs/m5stick-c-1/src/main.cpp.o] Error 1
========================== [FAILED] Took 4.31 seconds ==========================

Additional information

No response

edenhaus commented 2 years ago

Have you tried to remove the - before the include like:

color:
  <<: !include include/color/named.yaml

The dash specifies a list entry in yaml meaning you are making a list in a list with your above configuration

michaelblight commented 2 years ago

Dammit, you're right! All of my other uses have the "list in a list" situation - i.e. a - before the <<: and also in the included file, like I did with the binary_sensor above. It's probably more surprising that they work. I also changed the binary_sensor to remove the -, and it's happy like that. Good that this is recorded here, for when I do the same thing again.

michaelblight commented 2 years ago

Now I'm confused - I did a 'Clean Build' and the error is back. So maybe closing was hasty. It seems I don't fully understand !include. It seems that the included file can only contain one list item if it's under a component. If correct, this makes it a bit useless for what I'm trying to do.

So the following both work:

<<: !include include/color/named.yaml    # where the yaml contains "color:"
color: !include/color/named.yaml              # not actually tested, but same as home assistant

Whereas the following will only include the first list item in the file:

color:
  <<: !include include/color/named.yaml     # where the yaml does not contain "color:"
#### or ####
color:
- <<: !include include/color/named.yaml     # where the yaml does not contain "color:"

You can easily verify this by duplicating the id of something in an included file - you don't get an error because it picks up the first list item and ignores everything else.

In Home Assisstant you can achieve what I want using labels:

color label1: !include file1
color label2: !include file2

But this doesn't work in ESPHome. Perhaps time for a Feature Request.

EDIT: I tried various versions of include_dir_merge_list (etc), but couldn't get them to work either, although they appear supported. They seem to work, but I think it's remnants of a previous build somehow. It came unstuck with a "Clean Build".