Compiling yaml on desktop gives error "Not a valid font file"

I don’t know if this problem can be posted here, but here I go: since compiling yaml is painfully slow on my HA Green, I have installed Python and ESPHome on my desktop computer, but every time I try to compule the yaml code, I get the error that the font I want to use is not a valid font file. I have literally tried every supported way of referencing a font, but I always get the same error.

For example, when I use this code:

font:
  - file:
      type: gfonts
      family: Roboto
      weight: 400
    id: roboto
    size: 40
  - file:
      type: gfonts
      family: Roboto
      weight: 900
    id: robotobold
    size: 50

I get the error:
Gfont Roboto:0:400 is not a valid font file.

Validating the code inside the ESPHome device builder in HA doesn’t give any problems. What am I missing?

Google fonts have to be downloaded, so perhaps there’s an issue there?

But even when I reference locally installed fonts in the .esphome folder, I get the same error “not a valid font file”. (The font names and path are correct, and the font files are not corrupted - I tried with various fonts.)

Have you seen this note in the documentation?

Yes, I have installed the pillow package already, but to no avail, alas.

Looking at the component code, if anything fails while trying to let Freetype load the font, it’ll just exit with the error that you get.

You could try creating a test.py file with these contents:

from freetype import ( Face )

Face("path/to/myfont.ttf")

(where path/to/myfont.ttf is the path to a local font file you know should be okay)

Then run with: python test.py

(this assumes the Python esphome package is installed globally, that is, not in a virtual environment or something similar)

Perhaps it gives more information on why it can’t load the font file.

Thanks for the follow-up!
I’m completely new to Python and ‘real’ coding, and when I run the following test.py:

from freetype import ( Face )

Face("\Users\Björn\AppData\Local\Programs\Python\Python312\Scripts\font\Roboto-Bold.ttf")

I get this error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

but from what I found, this has probably to do with me not knowing enough about Python syntax … (sorry).

You should be able to use forward (/) slashes, but perhaps it’s easier to just copy the font file to the same directory as the Python script, and simply use just the filename (not the directory path) in the script:

from freetype import ( Face )

Face("Roboto-Bold.ttf")

Thanks for your patience :slight_smile:

I just put the font in the same directory as the Python script, and it doesn’t give me an error (nor anything else), so now I’m even more confused about the cause of the error …

Possibly an incorrectly formatted path in the YAML, like with the script?

Wouldn’t explain the issue with type: gfonts, but since you’re using Windows it wouldn’t surprise me if there’s some sort of firewall getting in the way there.

I just placed the fonts in the same folder as the yaml-file (they are found correctly) but it still says “not a valid font file” …

I’ve run out of ideas :frowning:

No problem - thanks for your help. I haven’t found any useful information online on this error, at least not with other suggestions to solve it. Compiling via ESPHome Device builder inside HA works fine, at least.

Thanks for the advice, but Pillow was already up to date …

Requirement already satisfied: Pillow in C:\Users\Björn\AppData\Local\Programs\Python\Python312\Lib\site-packages (12.2.0)

As stated above, I already tried with the .ttf in the same folder as my project, but to no avail: I keep getting the error … I’ve also tried with multiple fonts and font types supported by esphome by all give the same error.

Edit: I got a step further by referencing the fonts directly in the Windows Fonts folder, and I no longer got the error. But I now get CMake error (fatal: not a git repository (or any of the parent directories): .git) … but that’s not something to be discussed here, I suppose :slight_smile:

You should not have to manually download a Google font. If you use the YAML format you have, it’s suppose to download the font to your .esphome folder and cache it for use. If you’re trying to find a way to point to a local folder, you are likely spending time on something that isn’t going to help you.

I would try deleting the .esphome folder (should be in the same directory as the YAML file but might be hidden) and then try again. If that doesn’t work, try this format in the YAML instead:

  # gfonts://family[@weight]
  - file: "gfonts://Roboto"
    id: roboto_20
    size: 20

What you have should work though. It’s exactly what I use in my YAML, and I’ve never had to manually download any font.

Strangely enough, the font seems to be downloaded, as after I deleted the .esphome folder and run the YAML-file again, I get a “font”-folder inside the recreated .esphome folder with the font file which gets the name “Roboto@400@[email protected]”. I can open that font file without any problems in Windows.

Now that we aren’t down the “font must be downloaded” rabbit hole, I’d suggest going back to one of the earlier troubleshooting things. Move that test.py file you created earlier into the same directory as the YAML. Then modify it to read:

from freetype import ( Face )
Face(".esphome/font/Roboto@400@[email protected]")

I’d be curious to see if you get a different result than last time, since now you are testing the actual font that was downloaded that ESPHome is trying to use.

I adapted the test.py-file as you suggested, and I don’t get any error (I don’t get anything at all).

The only other thing I can think of offhand is that there is some odd permissions issue with your .esphome folder that is keeping ESPHome from properly reading the file when it runs versus when you open it by hand.

That’s to be expected if the font was loaded properly.