esphome / issues

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

empty name with no friendly name should fail validation instead of crashing #5602

Closed jclsn closed 2 weeks ago

jclsn commented 5 months ago

The problem

Trying to compile a switch.yaml component

esphome:
  name: lautsprecherdrehen

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

wifi:
  ssid: "fritz.box"
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fallback Hotspot"
    password: "klingel123"

captive_portal:

switch:
  - platform: gpio
    id: lautsprecherdrehen
    name: ""
    pin: 13
    inverted: true
    on_turn_on:
      then:
        - switch.toggle: lautsprecherdrehen

and am getting an error. Had to take the dev version because of https://github.com/esphome/issues/issues/5486 and now this occurs.

INFO ESPHome 2024.4.0-dev
INFO Reading configuration speaker-switch.yaml...
INFO Generating C++ source...
Traceback (most recent call last):
  File "/usr/bin/esphome", line 33, in <module>
    sys.exit(load_entry_point('esphome==2024.4.0.dev0', 'console_scripts', 'esphome')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 1061, in main
    return run_esphome(sys.argv)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 1048, in run_esphome
    rc = POST_CONFIG_ACTIONS[args.command](args, config)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 478, in command_run
    exit_code = write_cpp(config)
                ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 192, in write_cpp
    generate_cpp_contents(config)
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 204, in generate_cpp_contents
    CORE.flush_tasks()
  File "/usr/lib/python3.11/site-packages/esphome/core/__init__.py", line 679, in flush_tasks
    self.event_loop.flush_tasks()
  File "/usr/lib/python3.11/site-packages/esphome/coroutine.py", line 246, in flush_tasks
    next(task.iterator)
  File "/usr/lib/python3.11/site-packages/esphome/__main__.py", line 184, in wrapped
    await coro(conf)
  File "/usr/lib/python3.11/site-packages/esphome/components/gpio/switch/__init__.py", line 27, in to_code
    var = await switch.new_switch(config)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/components/switch/__init__.py", line 169, in new_switch
    await register_switch(var, config)
  File "/usr/lib/python3.11/site-packages/esphome/components/switch/__init__.py", line 164, in register_switch
    await setup_switch_core_(var, config)
  File "/usr/lib/python3.11/site-packages/esphome/components/switch/__init__.py", line 139, in setup_switch_core_
    await setup_entity(var, config)
  File "/usr/lib/python3.11/site-packages/esphome/cpp_helpers.py", line 106, in setup_entity
    add(var.set_object_id(sanitize(snake_case(CORE.friendly_name))))
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/esphome/helpers.py", line 362, in snake_case
    return value.replace(" ", "_").lower()
           ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

Which version of ESPHome has the issue?

esphome-dev

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

No response

What platform are you using?

ESP8266

Board

No response

Component causing the issue

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

ssieb commented 5 months ago

That should give a nicer validation error instead of crashing. I think the problem is that your switch name is empty and you don't have the friendly name set.

jclsn commented 5 months ago

@ssieb Ah yes, thank you! It compiles now

ssieb commented 5 months ago

I'll keep this open. That should give a validation error instead of crashing.

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.