esphome / issues

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

Unable to use `!extend` syntax with LD2410 #5728

Open Pimentoso opened 3 weeks ago

Pimentoso commented 3 weeks ago

The problem

In my house I am using many LD2410 presence sensors, so I wanted to use !extend to add some actions to the binary sensors. The yml is failing to compile.

main yml file

substitutions:
  device_name: device-test-01

packages:
  common: !include modules/common.yml
  motion_sensor: !include modules/motion_sensor.yml
  light: !include modules/light.yml

  # this contains the extend code
  motion_status_leds: !include automations/motion_status_leds.yml

motion_sensor.yml

[...]
ld2410:

binary_sensor:
  - platform: ld2410
    has_target:
      name: "${device_name}-presence"
      id: motion_sensor
    has_moving_target:
      name: "${device_name}-moving-target"
      id: moving_target_sensor
    has_still_target:
      name: "${device_name}-still-target"
      id: still_target_sensor

motion_status_leds.yml

binary_sensor:
- platform: ld2410
  has_moving_target:
    id: !extend moving_target_sensor
    on_press:
      then:
        - light.addressable_set:
            id: rgb_leds
            [...turn on a led]
    on_release: 
      then:
        - light.addressable_set:
            id: rgb_leds
            [...turn off a led]

I receive the following error

Failed config

binary_sensor.ld2410: [source automations/motion_status_leds.yml:2]
  platform: ld2410
  has_moving_target: 

    Source for extension of ID 'moving_target_sensor' was not found.
    id: !extend moving_target_sensor

I suspect it's because the !extend syntax cannot handle nested IDs like in the LD2410 case? Or am I doing anything wrong? Is there any workaround?

cc @quentinmit

Which version of ESPHome has the issue?

2024.3.2

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 3 weeks ago

I think extend can only be used in the main yaml.

Pimentoso commented 2 weeks ago

@ssieb does not work even by moving the contents of motion_status_leds.yml inside main.yml (after the packages declaration). Gives the same error.

ssieb commented 2 weeks ago

And you can't extend sub-components like that. You need to move it up one level. I think that should work.

Pimentoso commented 2 weeks ago

But that's literally what the extend function should do. From the original PR description #3555

which means that a user can override a field in a package's component by referencing the same id in their config with !extend id.

However! I might have found a workaround, which is using the Copy integration

Basically I made moving_target_sensor internal, and exposed a copy of it. Now it does not have the has_moving_target: section, the yml compiles. I haven't tested it yet though.

So motion_sensor.yml became

binary_sensor:
  - platform: ld2410
    has_target:
      id: motion_sensor_internal
      internal: true
    has_moving_target:
      id: moving_target_sensor_internal
      internal: true
    has_still_target:
      id: still_target_sensor_internal
      internal: true
  - platform: copy
    source_id: motion_sensor_internal
    id: motion_sensor
    name: "${device_name}-presence"
  - platform: copy
    source_id: moving_target_sensor_internal
    id: moving_target_sensor
    name: "${device_name}-moving-target"
  - platform: copy
    source_id: still_target_sensor_internal
    id: still_target_sensor
    name: "${device_name}-still-target"
ssieb commented 2 weeks ago

You didn't want to do:

binary_sensor:
- platform: ld2410
  id: !extend my_ld2410
  has_moving_target:
    on_press:
      then:
        - light.addressable_set:
            id: rgb_leds
            [...turn on a led]
    on_release: 
      then:
        - light.addressable_set:
            id: rgb_leds
            [...turn off a led]

You might need to include the id: and name: as well though.

Pimentoso commented 1 week ago

That's not working because:

[id] is an invalid option for [binary_sensor.ld2410].

Thanks for your suggestions however. I will test my workround soon and report back.