esphome / issues

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

UTF-8 sequences in map filter #6009

Closed andrein closed 3 months ago

andrein commented 3 months ago

The problem

I have a weather forecat text_sensor and I'm attempting to translate the forecast string to an icon using the material design icons webfont:

font:
  - file: "fonts/materialdesignicons-webfont.ttf"
    id: weather_icons
    size: 128
    bpp: 4
    glyphs:
      - "\U000F0594" # clear-night
      - "\U000F0590" # cloudy
      - "\U000F0F2F" # exceptional
      - "\U000F0591" # fog
      - "\U000F0592" # hail
      - "\U000F0593" # lightning
      - "\U000F067E" # lightning-rainy
      - "\U000F0595" # partlycloudy
      - "\U000F0596" # pouring
      - "\U000F0597" # rainy
      - "\U000F0598" # snowy
      - "\U000F067F" # snowy-rainy
      - "\U000F0599" # sunny
      - "\U000F059D" # windy
      - "\U000F059E" # windy-variant
      - "\U000F14E4" # sunny-off

text_sensor:
  - platform: homeassistant
    id: weather_forecast_sensor
    entity_id: weather.forecast_home
    filters:
      - map: 
          - from: clear-night
            to: \U000F0594 
          - from: cloudy 
            to: \U000F0590
          - from: exceptional 
            to: \U000F0F2F 
          - from: fog 
            to: \U000F0591
          - from: hail 
            to: \U000F0592
          - from: lightning 
            to: \U000F0593
          - from: lightning-rainy 
            to: \U000F067E
          - from: partlycloudy 
            to: \U000F0595
          - from: pouring 
            to: \U000F0596
          - from: rainy 
            to: \U000F0597
          - from: snowy 
            to: \U000F0598
          - from: snowy-rainy 
            to: \U000F067F
          - from: sunny 
            to: \U000F0599
          - from: windy 
            to: \U000F059D
          - from: windy-variant 
            to: \U000F059E
          - from: sunny-off 
            to: \U000F14E4

This doesn't work, the generated code looks like this (slightly formatted for readability):

text_sensor_mapfilter_id = new text_sensor::MapFilter(std::map<std::string, std::string>({
  {"clear-night", "\134U000F0594"},
  {"cloudy", "\134U000F0590"},
  {"exceptional", "\134U000F0F2F"},
  {"fog", "\134U000F0591"},
  {"hail", "\134U000F0592"},
  {"lightning", "\134U000F0593"},
  {"lightning-rainy", "\134U000F067E"},
  {"partlycloudy", "\134U000F0595"},
  {"pouring", "\134U000F0596"},
  {"rainy", "\134U000F0597"},
  {"snowy", "\134U000F0598"},
  {"snowy-rainy", "\134U000F067F"},
  {"sunny", "\134U000F0599"},
  {"windy", "\134U000F059D"},
  {"windy-variant", "\134U000F059E"},
  {"sunny-off", "\134U000F14E4"}
}));

Notice that the \U becomes \134U in the generated code, which breaks the rendering of the symbol. (I tried hard coding it, it works as expected in that case)

Which version of ESPHome has the issue?

2024.6.4

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

-

What platform are you using?

ESP32-IDF

Board

sunton esp32-8048s050

Component causing the issue

text_sensor

Example YAML snippet

esphome:
  name: utf8-issue

esp32:
  board: esp32-s3-devkitc-1
  variant: esp32s3
  framework:
    type: esp-idf

api:

wifi:
  ssid: "xxx"
  password: "xxx"
  fast_connect: True

font:
  - file: "fonts/materialdesignicons-webfont.ttf"
    id: weather_icons
    size: 128
    bpp: 4
    glyphs:
      - "\U000F0594" # clear-night
      - "\U000F0590" # cloudy
      - "\U000F0F2F" # exceptional
      - "\U000F0591" # fog
      - "\U000F0592" # hail
      - "\U000F0593" # lightning
      - "\U000F067E" # lightning-rainy
      - "\U000F0595" # partlycloudy
      - "\U000F0596" # pouring
      - "\U000F0597" # rainy
      - "\U000F0598" # snowy
      - "\U000F067F" # snowy-rainy
      - "\U000F0599" # sunny
      - "\U000F059D" # windy
      - "\U000F059E" # windy-variant
      - "\U000F14E4" # sunny-off

text_sensor:
  - platform: homeassistant
    id: weather_forecast_sensor
    entity_id: weather.forecast_home
    filters:
      - map: 
          - from: clear-night
            to: \U000F0594 
          - from: cloudy 
            to: \U000F0590
          - from: exceptional 
            to: \U000F0F2F 
          - from: fog 
            to: \U000F0591
          - from: hail 
            to: \U000F0592
          - from: lightning 
            to: \U000F0593
          - from: lightning-rainy 
            to: \U000F067E
          - from: partlycloudy 
            to: \U000F0595
          - from: pouring 
            to: \U000F0596
          - from: rainy 
            to: \U000F0597
          - from: snowy 
            to: \U000F0598
          - from: snowy-rainy 
            to: \U000F067F
          - from: sunny 
            to: \U000F0599
          - from: windy 
            to: \U000F059D
          - from: windy-variant 
            to: \U000F059E
          - from: sunny-off 
            to: \U000F14E4

display:
  - platform: rpi_dpi_rgb
    id: screen
    color_order: RGB
    update_interval: never
    auto_clear_enabled: false # takes 2.8 seconds to clear the display
    dimensions:
      width: 800
      height: 480
    de_pin: GPIO40
    hsync_pin: GPIO39
    vsync_pin: GPIO41
    pclk_pin: GPIO42
    pclk_frequency: 12.5MHz
    hsync_pulse_width: 4
    hsync_front_porch: 8
    hsync_back_porch: 8
    vsync_pulse_width: 4
    vsync_front_porch: 8
    vsync_back_porch: 8
    data_pins:
      red:
        - GPIO45
        - GPIO48
        - GPIO47
        - GPIO12
        - GPIO14
      green:
        - GPIO5 
        - GPIO6
        - GPIO7
        - GPIO15
        - GPIO16
        - GPIO4
      blue:
        - GPIO8
        - GPIO3
        - GPIO46
        - GPIO9
        - GPIO1

Anything in the logs that might be useful for us?

No response

Additional information

No response

andrein commented 3 months ago

It's not a bug after all, the UTF-8 sequences need to be quoted, this works as expected:


text_sensor:
  - platform: homeassistant
    id: weather_forecast_sensor
    entity_id: weather.forecast_home
    filters:
      - map: 
          - from: clear-night
            to: "\U000F0594"
          - from: cloudy
            to: "\U000F0590"
          - from: exceptional
            to: "\U000F0F2F"
          - from: fog
            to: "\U000F0591"
          - from: hail
            to: "\U000F0592"
          - from: lightning
            to: "\U000F0593"
          - from: lightning-rainy
            to: "\U000F067E"
          - from: partlycloudy
            to: "\U000F0595"
          - from: pouring
            to: "\U000F0596"
          - from: rainy
            to: "\U000F0597"
          - from: snowy
            to: "\U000F0598"
          - from: snowy-rainy
            to: "\U000F067F"
          - from: sunny
            to: "\U000F0599"
          - from: windy
            to: "\U000F059D"
          - from: windy-variant
            to: "\U000F059E"
          - from: sunny-off
            to: "\U000F14E4"