hencou / esphome_components

Collection of own ESPhome components
25 stars 15 forks source link

Not receiving S2 commands when additional light is defined in yaml #34

Closed szupi-ipuzs closed 6 months ago

szupi-ipuzs commented 6 months ago

Hi, I now the following setup:

The MiLight hub (nodemcu + nrf24) flashed with esphome + your mi component is supposed to intercept the keypresses from the first S2, but is also paired with the FUT038s and can control the light. Here's the yaml I use (part of it):

mi:
  id: mi1  #optional
  ce_pin: D2 #required, default: 4
  csn_pin: D8 #required, default: 15
  reset_pin: 0 #optional, default: 0, only needed with LT8900 radio
  radio_interface_type: nrf24 #optional, default: nrf24, possible values: nrf24,lt8900
  packet_repeats: 50 #optional, default: 50, total of sended packets per command
  listen_repeats: 10 #optional, default: 20, amount of received packets needed for a incoming command from other remote
  state_flush_interval: 5000 #optional, default: 10000, time in miliseconds to send the latest state report to HomeAssistant
  packet_repeat_throttle_threshold: 200 #optional, default: 200, threshold to limit the amount of packets in a second
  packet_repeat_throttle_sensitivity: 0 #optional, default: 0
  packet_repeat_minimum: 3 #optional, default: 3
  enable_automatic_mode_switching: true #optional, default: false
  rf24_power_level: MAX #optional, default: MAX, possible values: MIN, LOW, HIGH, MAX
  rf24_listen_channel: HIGH #optional, default: LOW, possible values: LOW, MID, HIGH
  packet_repeats_per_loop: 10 #optional, default: 10, repeat packets sended per loop
  resend_last_command: false #optional, default: true, repeats the latest command after a random time between 2 and 3 seconds again
  rf24_channels: #optional, 1-3 values required when used, default: LOW-MID-HIGH, possible values: LOW, MID, HIGH
    - LOW
    - MID
    - HIGH
  on_command_received: #optional, useful to send remote commands to HA and process them further there in automations
    - 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();"

light:
  - platform: mi #required
    device_id: 0xAB01 #required, hexadacimal value of MiLight id
    id: fake
    internal: True
    group_id: 1 #required, 1-4 or 1-8, depending on remote type
    remote_type: s2 #required, possible values: rgb_cct, rgb, cct, rgbw, fut089, fut091, fut020, s2
    default_transition_length: 0s #optional, but 0s gives a better behaviour instead the default 200ms
  # Set these to calibrate the color temperature of your light, measured with an external color temp. sensor or app
  # optional, [153, 370] mireds is the range miboxer uses internally ([6535, 2702] K)
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K
# optional variables: all variables of ESPHome base light component

  - platform: mi
    name: MiBoxer
    id: miboxer
    device_id: 0xAB02
    group_id: 2
    remote_type: rgbw
    default_transition_length: 0s

button:
  - platform: mi
    id: pair
    name: ${friendly_name} pair
    device_id: 0xAB02
    group_id: 2
    remote_type: rgbw
    command: pair
    entity_category: config
  - platform: mi
    id: unpair
    name: ${friendly_name} unpair
    device_id: 0xAB02
    group_id: 2
    remote_type: rgbw
    command: unpair
    entity_category: config

The problem is: If I have only the "fake" light enabled (the other is commented out) then all the keypresses from the first S2 are received correctly. But as soon as I enable the "miboxer" light the keypresses from the first S2 are very rarely received. And sometimes even the wifi connection is dropped and I need to reset the hub. However sending the commands to FUT038s works fine. I've noticed that the first keypress always gets through, but later - very rarely. It's almost as if the receiver does not get cpu time to do the detection. The logs (VERY_VERBOSE) don't show anything interesting, but I can attach them if needed.

I can try debugging this, but don't really know where to start. Is there any particular place I where should add traces to see what's going on?

hencou commented 6 months ago

HI,

I would sugest to start debuggin in https://github.com/hencou/esphome_components/blob/main/components/mi/mi.cpp, in the function "Mi::handleListen()" starting from line 215. In that function, every configured "bulbId" is applied, and the ESP will listen to received commands then. I expect the ESP module can only accept commands from one configured bulbId, and the others will be ignored but I am no sure.

I am looking forward to your test results!