SecKatie / ha-wyzeapi

Home Assistant Integration for Wyze devices.
722 stars 112 forks source link

[Bug] Wyze color bulb transition not working #329

Closed 3Necromancer closed 2 years ago

3Necromancer commented 2 years ago

I have a wyze color bulb connected, I use call service -> light turn off with the bulb entity and transition 60, but it turns off immediately. same with turn on.

It works well with my LIFX bulbs.

image

JoeSchubert commented 2 years ago

I don't have any bulbs, but afaik wyze bulbs don't support transitions.

brg468 commented 2 years ago

I can confirm, they do not support transition.

3Necromancer commented 2 years ago

can it be emulated ? sending a few pulses to change the brightness over a period of time? pulse = brightness_difference/transition_time*3 gives the change in brightness for 3 seconds, brightness_difference/pulse gives the amount of pulses needed to achieve the change in brightness over the defined period of time.

tggman commented 2 years ago

can it be emulated ? sending a few pulses to change the brightness over a period of time? pulse = brightness_difference/transition_time*3 gives the change in brightness for 3 seconds, brightness_difference/pulse gives the amount of pulses needed to achieve the change in brightness over the defined period of time.

I've written code that emulates light brightness transitions, but it fails to work reliably due to Wyze Rate limiting. Here's the code if you want to try it yourself. I've tested/confirmed this code using LIFX bulbs:

# ----------------------------------------------------------- #
# Purpose: Transition light brightness over a period of time
#
# Service Call:
# service: script.light_transition
# data:
#   entity_id: light.lifx_color_800tkg02                             #-: [ ... ]
#   brightness: 255                                                  #-: (1-255) or brightness_pct (0-100)
#   color_temp: 235
#   transition: 360                                                  #-: time in seconds
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
light_transition:
  alias: "Light Brightness Transition"
  variables:
    fade_time_sec:  "{{ transition }}"                                         #-: time to transition brightness (actual may be slightly longer)
    fade_time_msec: "{{ fade_time_sec | float(0) * 1000 }}"
    brightness: "{{ [brightness_pct|default(-1) * 255/100, brightness|default(-1)] | max|round(0,'round-to-even',0) }}" #-: convert % to byte as reqiured
    delay_time_msec: "{{ fade_time_msec / brightness }}"                       #-: max brightness: 255/fade_time_sec  = msec / Index (0-255)
  # delay_time_msec: "{{ fade_time_msec / 100 }}"                              #-: brightness_pct: 100%/fade_time_sec = msec / 1.0% (0-100%)
    fade_loop_count: "{{ (fade_time_msec / delay_time_msec)|round(0,'round-to-even',0) }}"     #-: should work out to 255 (or 100%)
  mode: parallel
  sequence:
    - service: light.turn_on
      data:
        entity_id: "{{ entity_id }}"
        brightness: 1
        color_temp: "{{ color_temp }}"
    - wait_template: "{{ is_state(entity_id, 'on') }}"
      timeout: 5                                                               #-: timeout value should be an eternity
      continue_on_timeout: false                                               #-: abort the script if timeout duration expires
    - repeat:                                                                  # START repeat
        sequence:
          - wait_template: "{{ is_state(entity_id, 'off') }}"                  # delay + check to see if light was turned off manually 
            timeout:
              milliseconds: "{{ delay_time_msec }}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
          - choose:                                                                 # if (light was NOT turned off) then
              conditions: "{{ not wait.completed }}"                                # ..
              sequence:                                                             # .. continue the brightness transition process
                service: light.turn_on                                              # ..
                data:                                                               # ..
                  entity_id: "{{ entity_id }}"                                      # ..
                  brightness: "{{ (brightness/fade_loop_count * repeat.index)|int(0) }}"  # 255  / TotalLoopCount * CurrentLoopCount
                # brightness_pct: "{{ (100/fade_loop_count * repeat.index)|int(0) }}"     # 100% / TotalLoopCount * CurrentLoopCount
            #default:                                                               # else (do nothing)
            #[end.choose]#                                                          # endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
        until: "{{ is_state(entity_id, 'off') or (repeat.index > fade_loop_count) }}"
#   - END repeat                                                               # END repeat
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
JoeSchubert commented 2 years ago

@tggman the rate limiting should only be a factor with cloud control of the bulbs. If you're able to use local control I wouldn't suspect that it would be a factor

tggman commented 2 years ago

@tggman the rate limiting should only be a factor with cloud control of the bulbs. If you're able to use local control I wouldn't suspect that it would be a factor

Good point! If your color bulbs are able to use and maintain local control (local_control: true once the latest version of wyzeapy is released) rate limiting should not be an issue!

jheld commented 2 years ago

So there is a dependency on a newer wyzeapy version?

Also the wyze app now supports transitions/delays (Timer) for the bulbs -- is that something which may be available as part of the wyze api natively?

brg468 commented 2 years ago

There’s a timer in the Wyze app, but that’s not the same thing as a transition. It only turns the light on/off after a certain amount of time.

JoeSchubert commented 2 years ago

So there is a dependency on a newer wyzeapy version?

There's nothing that can be done third party to implement a true "transition" on the bulb. It's something that wyze would have to include in the bulb's firmware, which as far as I am aware, they do not. Anything that is fine beyond that is just a hacky workaround... At best.

Essentially, for a transition you would serve a single command to the bulb to start the transition. This would include things like the start/stop brightness/color and duration. After that, the firmware on the bulb handles all of the logic to make the transition happen as specified.

Given wyze's API limits and often laggy command sequences, there's really not much chance that this will ever be doable through their normal control as is. It's something that you could simulate with local control by slamming the bulb with commands (assuming that the bulb's firmware doesn't choke, which with wyze... I wouldn't hold my breath). But even that would be a stuttery transition as it would just be setting new values at intervals rather than using PWM in the bulb's controller.

SecKatie commented 2 years ago

AFAIK there is nothing we can do to enable this from the integration without a horrible hack