hencou / esphome_components

Collection of own ESPhome components
25 stars 15 forks source link

Feature Request: Add Trigger for Code Received #10

Closed shbatm closed 2 years ago

shbatm commented 2 years ago

Have a feature request for you--I could probably clobber something together but my C++ is terrible.

Add a on_code_received trigger & callback to allow automations for remotes not bound to a bulb, similar to the RFBridge Component:

https://esphome.io/api/classesphome_1_1rf__bridge_1_1_r_f_bridge_received_code_trigger.html https://esphome.io/api/classesphome_1_1rf__bridge_1_1_r_f_bridge_component.html#a8d44e0233fe8f3e405563d6ee5dbf849

Reason: this would allow for extra remotes to be used and events sent back to home assistant using ESPHome Automations.

For example (using RF Bridge as a reference):

mi:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.milight_code_received
          data:
            key: !lambda 'return format_hex(data.key);'
            id: !lambda 'return format_hex(data.id);'
            command: !lambda 'return format_hex(data.command);'
            argument: !lambda 'return format_hex(data.argument);'
            group: !lambda 'return format_hex(data.group);'
hencou commented 2 years ago

Hi shbatm,

Seems possible. See the modified yaml example: https://github.com/hencou/esphome_components/blob/main/example_milight.yaml

Example of yaml code:

mi:
  ce_pin: D0
  csn_pin: D8
  on_command_received:
  - homeassistant.event:
      event: esphome.mi_command_received
      data:
        device_id: !lambda 'return format_hex(data.device_id);'
        group_id: !lambda 'return data.group_id;'
        remote_type: !lambda 'return data.remote_type.c_str();'
        command: !lambda 'return data.command.c_str();'

With the example above, in HA, when you subscibe on the event "esphome.mi_command_received" you will see the incoming events from the ESPHome module.

hencou commented 2 years ago

Tested in my own system -- still working for 6 days without problems.

shbatm commented 2 years ago

Awesome, thanks! I didn't see any issues with the code, but I wasn't able to test before I left town. I have a spare Gledopto remote I'll get setup when I get back.