Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new ble_adv_controller component integrating the handling of fan, lights, pairing button and new variants #17

Merged
merged 22 commits into from
Aug 22, 2024

Conversation

NicoIIT
Copy link
Contributor

@NicoIIT NicoIIT commented Jul 20, 2024

Creation of a new component 'ble_adv_controller' able to handle not only lights but also fans combined in a same lamp. It is based mainly on your component 'ble_adv_light' and the fork from @flicker581, with a lot of refactoring. It does not support only lights and has a centralized controller so I renamed it.

New Features:

  • Centralized controller for multiple entities to support lamps with multiple lights and/or fan
  • Handling of 'pair' button, more easy to use than service call from HA.
  • Handling of Fan for ZhiJia
  • Handling of Fan for FanLamp pro
  • Handling of ZhiJia encoding v0 (MSC16) and v1 (MSC26) based on work done by @14roiron here and retro engineering from app for the command codes and arguments.
  • Handling of FanLamp encoding v1a / v1b / v2 / v3 based on work done by @flicker581. Verified by comparing packets generated from the app on his forked repository and this new component.
  • Handling of custom commands to be able to test features more easily, and explanations on reverse engineering.
  • Handling of Dynamic configuration from HA for: encoding, duration, min_brightness
  • Review of Brightness and Color Temperature handling.
  • Added lots of doc for new users

Related Issues:
#18 : Resolving flickering issue with Zhi Jia lamps
#16 : Add of Fan Support
#13 : Solved by the support of more encoding on ZhiJia
#12 : No more compilation issues on the new component
#11: Added encoding proposed
#5: Solved already in @flicker581 repo, encoding added here
#1: Changing the default value of 'default_transition_length' to 0s

…ir button as well as all encoding for ZhiJia(3) andFanLamp(4)
@NicoIIT NicoIIT changed the title Add new ble_adv_component integrating the handling of fan, lights, pairing button and new variants Add new ble_adv_controller component integrating the handling of fan, lights, pairing button and new variants Jul 20, 2024
@aronsky
Copy link
Owner

aronsky commented Jul 20, 2024

Thank you for the hard work! That's a huge PR, I'll go over it when I find the time, and hopefully will merge it soon.

@cRazyK67
Copy link

cRazyK67 commented Jul 20, 2024

I have many lamp controlled by FanLamp and ZhiJia App.
Include two identical lamp controller by ZhiJia App.

In app lamp controlled separately.
In HA lamp controlled same time.

My esphome config:

external_components:
source: github://NicoIIT/esphome-components

ble_adv_controller:

  • id: bedroom_bedsidelamp_my
    encoding: zhijia
    variant: v2
    duration: 500
    forced_id: 0xC630B1

  • id: bedroom_bedsidelamp_notmy
    encoding: zhijia
    variant: v2
    duration: 500
    forced_id: 0xC630B2

  • id: bedroom_chandelier
    encoding: fanlamp_pro
    variant: v1a
    duration: 500
    reversed: false
    forced_id: 0

light:

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_bedsidelamp_my
    name: bedroom_bedsidelamp_my
    default_transition_length: 0s

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_bedsidelamp_notmy
    name: bedroom_bedsidelamp_notmy
    default_transition_length: 0s

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_chandelier
    name: bedroom_chandelier
    index: 0
    min_brightness: 21

button:

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_bedsidelamp_my
    name: Pair bedroom_bedsidelamp_my
    cmd: pair

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_bedsidelamp_notmy
    name: Pair bedroom_bedsidelamp_notmy
    cmd: pair

  • platform: ble_adv_controller
    ble_adv_controller_id: bedroom_chandelier
    name: Pair bedroom_chandelier
    cmd: pair

ESPHome log:

[20:50:45][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[20:50:45][D][light:051]: Brightness: 71%
[20:50:45][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 97, ww: 97
[20:50:45][D][zhijia:636]: UUID: '0x00C630', tx: 11, Command: '0xAE', Args: [127,0,0]
[20:50:45][D][ble_adv_controller:071]: Prepared packet: 22.9D.B4.C4.1B.AF.43.9C.EF.3F.1A.80.2D.E5.D1.02.C3.24.92.4A.5F.85.F6.9C.A9.19 (26)
[20:50:45][D][zhijia:636]: UUID: '0x00C630', tx: 12, Command: '0xAD', Args: [194,0,0]
[20:50:45][D][ble_adv_controller:071]: Prepared packet: 22.9D.0D.7A.1F.AB.40.98.EB.3B.19.87.29.E1.D5.01.C3.23.96.4A.5F.85.F6.9C.A9.19 (26)
[20:50:45][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 143917
[20:50:45][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[20:50:45][D][light:047]: State: OFF
[20:50:45][D][zhijia:636]: UUID: '0x00C630', tx: 13, Command: '0xA6', Args: [0,0,0]
[20:50:45][D][ble_adv_controller:071]: Prepared packet: 22.9D.C5.B3.15.A1.4B.92.E1.31.12.86.23.EB.DF.0A.C3.22.9C.4A.5F.85.F6.9C.A9.19 (26)
[20:50:46][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 144432
[20:50:46][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 144437
[20:50:46][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 144940
[20:50:46][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 144950
[20:50:47][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 145457
[20:51:08][D][light:036]: 'bedroom_bedsidelamp_notmy' Setting:
[20:51:08][D][light:047]: State: ON
[20:51:08][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 255, ww: 255
[20:51:08][D][zhijia:636]: UUID: '0x00C630', tx: 2, Command: '0xA5', Args: [0,0,0]
[20:51:08][D][ble_adv_controller:071]: Prepared packet: 22.9D.C9.B0.19.AD.48.9E.ED.3D.11.89.2F.E7.D3.09.C3.2D.90.4A.5F.85.F6.9C.A9.19 (26)
[20:51:08][D][zhijia:636]: UUID: '0x00C630', tx: 3, Command: '0xAE', Args: [127,0,0]
[20:51:08][D][ble_adv_controller:071]: Prepared packet: 22.9D.BC.C4.13.A7.43.94.E7.37.1A.88.25.ED.D9.02.C3.2C.9A.4A.5F.85.F6.9C.A9.19 (26)
[20:51:08][D][zhijia:636]: UUID: '0x00C630', tx: 4, Command: '0xAD', Args: [255,0,0]
[20:51:08][D][ble_adv_controller:071]: Prepared packet: 22.9D.38.47.17.A3.40.90.E3.33.19.8F.21.E9.DD.01.C3.2B.9E.4A.5F.85.F6.9C.A9.19 (26)
[20:51:08][W][component:237]: Component light took a long time for an operation (69 ms).
[20:51:08][W][component:238]: Components should block for at most 30 ms.
[20:51:08][D][ble_adv_controller:085]: bedroom_bedsidelamp_notmy - start advertising at 166474
[20:51:08][D][ble_adv_controller:096]: bedroom_bedsidelamp_notmy - stop advertising at 166980
[20:51:08][D][ble_adv_controller:085]: bedroom_bedsidelamp_notmy - start advertising at 166987
[20:51:09][D][ble_adv_controller:096]: bedroom_bedsidelamp_notmy - stop advertising at 167491
[20:51:09][D][ble_adv_controller:085]: bedroom_bedsidelamp_notmy - start advertising at 167499
[20:51:09][D][ble_adv_controller:096]: bedroom_bedsidelamp_notmy - stop advertising at 168003

Lamp ID in config not implemented by ble packet

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 21, 2024

@cRazyK67, thanks a lot for your testing and input!
The UUID used by ZhiJia on 3 characters is computed from the input forced_id by truncation. I simulated what was done by the ZhiJia App from the AndroidId : 0xAABBCCDD => [AA, BB, CC]. In your case:
0xC630B1 = 0x00C630B1 => [00,C6,30]
0xC630B2 = 0x00C630B2 => [00,C6,30]
So they have the same UUID at the end... This computation is definitively not smart as the way you entered the ID (having the last digit vary) is clearly the way anyone will enter them: the software needs to be changed or at least the doc :)

Still there is some mystery in how the App is controlling multiple lights, I am not sure the UUID is effectively the key used to differentiate the lights, especially because from the app it is using the AndroidId... Could you please try to change them to 0xC630B100 and 0xC630B200 and check it it works? If not, I will add some debug options to be able to have other for now "fixed" parameters to vary, such as GROUP and MAC.

Another question: did you manage to have the lights work separately with previous 'ble_adv_light' component ?

@cRazyK67
Copy link

cRazyK67 commented Jul 21, 2024

Still there is some mystery in how the App is controlling multiple lights, I am not sure the UUID is effectively the key used to differentiate the lights, especially because from the app it is using the AndroidId... Could you please try to change them to 0xC630B100 and 0xC630B200 and check it it works?

The change you suggested (changing the first 3 significant bytes of the UUID) allowed the lamps to be controlled separately.

Another question: did you manage to have the lights work separately with previous 'ble_adv_light' component ?

With the previous component, all sources worked simultaneously for me.

I also have a problem with lamps flickering when changing brightness or color temperature. It seems that the brightness is first set to 100%, and then to the desired value.

I’ll post a video of the lamp’s behavior on YouTube a little later.

Also need reversed option for ZhiJia.

[11:12:52][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[11:12:52][D][light:051]:   Brightness: 19%
[11:12:52][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 7, ww: 7
[11:12:52][D][zhijia:636]: UUID: '0xC630B1', tx: 5, Command: '0xAE', Args: [127,0,0]
[11:12:52][D][ble_adv_controller:071]: Prepared packet: 22.9D.F4.3B.9D.EF.0D.DC.AF.7F.92.C0.6D.A5.A1.4C.4B.E5.D2.4A.5F.85.F6.9C.A9.19 (26)
[11:12:52][D][zhijia:636]: UUID: '0xC630B1', tx: 6, Command: '0xAD', Args: [14,0,0]
[11:12:52][D][ble_adv_controller:071]: Prepared packet: 22.9D.85.49.9D.EF.0E.DC.AF.7F.91.C3.6D.A5.A1.4F.4B.E6.D2.4A.5F.85.F6.9C.A9.19 (26)
[11:12:52][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 166152
[11:12:53][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 166655
[11:12:53][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 166661
[11:12:54][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 167166
[11:12:57][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[11:12:57][D][light:051]:   Brightness: 46%
[11:12:57][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 28, ww: 28
[11:12:57][D][zhijia:636]: UUID: '0xC630B1', tx: 7, Command: '0xAE', Args: [127,0,0]
[11:12:57][D][ble_adv_controller:071]: Prepared packet: 22.9D.F6.3B.9F.ED.0D.DE.AD.7D.92.C2.6F.A7.A3.4C.4B.E7.D0.4A.5F.85.F6.9C.A9.19 (26)
[11:12:57][D][zhijia:636]: UUID: '0xC630B1', tx: 8, Command: '0xAD', Args: [56,0,0]
[11:12:57][D][ble_adv_controller:071]: Prepared packet: 22.9D.BD.7F.93.E1.0E.D2.A1.71.91.CD.63.AB.AF.4F.4B.E8.DC.4A.5F.85.F6.9C.A9.19 (26)
[11:12:57][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 170444
[11:12:57][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 170955
[11:12:57][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 170960
[11:12:58][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 171468
[11:13:00][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[11:13:00][W][light:227]: 'bedroom_bedsidelamp_my' - Color temperature value 333.00 is out of range [167.0 - 333.0]!
[11:13:00][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[11:13:00][D][light:066]:   Color temperature: 333.0 mireds
[11:13:00][D][light:071]:   Cold white: 0%, warm white: 100%
[11:13:00][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 0, ww: 28
[11:13:00][D][zhijia:636]: UUID: '0xC630B1', tx: 9, Command: '0xAE', Args: [255,0,0]
[11:13:00][D][ble_adv_controller:071]: Prepared packet: 22.9D.78.BB.91.E3.0D.D0.A3.73.92.CC.61.A9.AD.4C.4B.E9.DE.4A.5F.85.F6.9C.A9.19 (26)
[11:13:00][D][zhijia:636]: UUID: '0xC630B1', tx: 10, Command: '0xAD', Args: [28,0,0]
[11:13:00][D][ble_adv_controller:071]: Prepared packet: 22.9D.9B.5B.91.E3.0E.D0.A3.73.91.CF.61.A9.AD.4F.4B.EA.DE.4A.5F.85.F6.9C.A9.19 (26)
[11:13:00][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 173960
[11:13:01][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 174476
[11:13:01][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 174485
[11:13:01][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 174994
[11:13:04][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[11:13:04][D][light:051]:   Brightness: 27%
[11:13:04][D][ble_adv_light:053]: BleAdvLight::write_state called! Requested cw: 0, ww: 7
[11:13:04][D][zhijia:636]: UUID: '0xC630B1', tx: 11, Command: '0xAE', Args: [255,0,0]
[11:13:04][D][ble_adv_controller:071]: Prepared packet: 22.9D.7A.BB.93.E1.0D.D2.A1.71.92.CE.63.AB.AF.4C.4B.EB.DC.4A.5F.85.F6.9C.A9.19 (26)
[11:13:04][D][zhijia:636]: UUID: '0xC630B1', tx: 12, Command: '0xAD', Args: [7,0,0]
[11:13:04][D][ble_adv_controller:071]: Prepared packet: 22.9D.86.40.97.E5.0E.D6.A5.75.91.C9.67.AF.AB.4F.4B.EC.D8.4A.5F.85.F6.9C.A9.19 (26)
[11:13:04][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 178026
[11:13:05][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 178533
[11:13:05][D][ble_adv_controller:085]: bedroom_bedsidelamp_my - start advertising at 178539
[11:13:05][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising at 179043

@cRazyK67
Copy link

I observe inappropriate fan behavior FanLamp v2
My sequence of actions in HA:

  1. Set fan speed 6 (100%) - the fan on the lamp begins to rotate at maximum speed
  2. Set fan speed 3 (50%) - fan rotating on half-speed
  3. Turn off fan from ha button - fan stoping
  4. Turn on fan from ha button - fan still not rotate (in HA fan speed 50%)
  5. Change speed to another - fan rotating

log for last action (4,5)

[12:29:30][D][fan:021]: 'childroom_chandelier_fan' - Setting:
[12:29:30][D][fan:024]:   State: ON
[12:29:30][D][ble_adv_fan:027]: BleAdvFan::control called
[12:29:30][D][ble_adv_fan:038]: BleAdvFan::control - toggle
[12:29:30][D][fanlamp_pro:278]: ID: '0x960D13A2', tx: 85, Command: '0x31', Args: [0,0,0,0]
[12:29:31][D][ble_adv_controller:071]: Prepared packet: F0.08.10.80.E9.67.BC.2E.12.3E.35.3C.C8.F0.0D.98.5F.9D.D7.1B.12.1C.2A.FE.98.A0 (26)
[12:29:31][D][fan:120]: 'childroom_chandelier_fan' - Sending state:
[12:29:31][D][fan:121]:   State: ON
[12:29:31][D][fan:123]:   Speed: 3
[12:29:31][D][fan:129]:   Direction: FORWARD
[12:29:31][W][component:237]: Component api took a long time for an operation (56 ms).
[12:29:31][W][component:238]: Components should block for at most 30 ms.
[12:29:31][D][ble_adv_controller:085]: childroom_chandelier - start advertising at 539731
[12:29:31][D][ble_adv_controller:096]: childroom_chandelier - stop advertising at 539833
[12:29:52][D][fan:021]: 'childroom_chandelier_fan' - Setting:
[12:29:52][D][fan:024]:   State: ON
[12:29:52][D][fan:030]:   Speed: 4
[12:29:52][D][ble_adv_fan:027]: BleAdvFan::control called
[12:29:52][D][ble_adv_fan:031]: BleAdvFan::control - setup speed 4
[12:29:52][D][fanlamp_pro:278]: ID: '0x960D13A2', tx: 86, Command: '0x31', Args: [0,32,4,0]
[12:29:52][D][ble_adv_controller:071]: Prepared packet: F0.08.10.80.C8.01.CA.DE.EF.3C.2B.7C.7F.89.7A.30.7E.EF.FB.2F.91.01.CD.59.20.F1 (26)
[12:29:52][D][fan:120]: 'childroom_chandelier_fan' - Sending state:
[12:29:52][D][fan:121]:   State: ON
[12:29:52][D][fan:123]:   Speed: 4
[12:29:52][D][fan:129]:   Direction: FORWARD
[12:29:52][W][component:237]: Component api took a long time for an operation (63 ms).
[12:29:52][W][component:238]: Components should block for at most 30 ms.
[12:29:52][D][ble_adv_controller:085]: childroom_chandelier - start advertising at 562064
[12:29:52][D][ble_adv_controller:096]: childroom_chandelier - stop advertising at 562166
[12:32:54][W][api.connection:129]: Home Assistant 2024.7.3 (10.50.24.130) didn't respond to ping request in time. Disconnecting...
[12:32:55][D][api:102]: Accepted 10.50.24.130
[12:32:56][D][api.connection:1375]: Home Assistant 2024.7.3 (10.50.24.130): Connected successfully

…eviewed logging. Reviewed ID computation for ZhiJia.
@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 22, 2024

@cRazyK67, I reviewed the Fan and Light Algo to be smarter, this may solve your issues. Please note you can still experience flickering on first switch onjust after flash / power cut or things like that, but no more in standard usage. This includes the change of min_brigthness to a percentage value (0.21 or 21% for example), but it is the default so you can remove the line.
Also added "reversed" option for ZhiJia.
Reviewed the computation of the UUID for ZhiJia, you should go back to your previous forced_id 0xC630B1 and 0xC630B2 to avoid the need for re-pairing.

@cRazyK67
Copy link

@NicoIIT Thanks for the quick fixes.
Here is a short report on them:

Also added "reversed" option for ZhiJia.

Works

Reviewed the computation of the UUID for ZhiJia, you should go back to your previous forced_id 0xC630B1 and 0xC630B2 to avoid the need for re-pairing.

Works without re-paring

I reviewed the Fan and Light Algo to be smarter, this may solve your issues. Please note you can still experience flickering on first switch onjust after flash / power cut or things like that, but no more in standard usage. This includes the change of min_brigthness to a percentage value (0.21 or 21% for example), but it is the default so you can remove the line.

Fan works fine.

The change in brightness also occurs without flickering, however, when the color temperature changes, the brightness increases, according to my feelings, to a level of 40-50%

I do the following sequence of steps:

  1. I set the light to cold
  2. Set the Brightness to 100%
  3. Set the Brightness to 1%
  4. Set up warm light
  5. Set the brightness to 15%

After step 4, the brightness, as I wrote, is ~40-50%
After step 5, the brightness decreases and is approximately the minimum for my lamp.

Below is the log of these actions:

[21:51:23][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[21:51:23][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:51:23][D][light:066]:   Color temperature: 167.0 mireds
[21:51:23][D][light:071]:   Cold white: 100%, warm white: 1%
[21:51:23][D][ble_adv_light:048]: Corrected brightness: 46%
[21:51:23][D][ble_adv_light:070]: ct: 167.001, br: 0.459
[21:51:23][D][ble_adv_light:075]: BleAdvLight::write_state - Requested color temperature: 0
[21:51:23][D][zhijia:636]: UUID: '0xC630B1', tx: 6, Command: '0xAE', Args: [0,0,0]
[21:51:23][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.77.BB.61.13.F2.20.53.83.6D.3C.91.59.5D.B3.B4.19.2E.4A.5F.85.F6.9C.A9.19 (26)
[21:51:24][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:51:27][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:51:27][D][light:051]:   Brightness: 100%
[21:51:27][D][ble_adv_light:048]: Corrected brightness: 100%
[21:51:27][D][ble_adv_light:070]: ct: 167.001, br: 1.000
[21:51:27][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 255
[21:51:27][D][zhijia:636]: UUID: '0xC630B1', tx: 7, Command: '0xAD', Args: [255,0,0]
[21:51:27][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.8A.47.63.11.F1.22.51.81.6E.3D.93.5B.5F.B0.B4.18.2C.4A.5F.85.F6.9C.A9.19 (26)
[21:51:27][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:51:33][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:51:33][D][light:051]:   Brightness: 1%
[21:51:33][D][ble_adv_light:048]: Corrected brightness: 1%
[21:51:33][D][ble_adv_light:070]: ct: 167.001, br: 0.012
[21:51:33][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 3
[21:51:33][D][zhijia:636]: UUID: '0xC630B1', tx: 8, Command: '0xAD', Args: [3,0,0]
[21:51:33][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.86.44.93.E1.0E.D2.A1.71.91.CD.63.AB.AF.4F.4B.E8.DC.4A.5F.85.F6.9C.A9.19 (26)
[21:51:35][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:51:37][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[21:51:37][W][light:227]: 'bedroom_bedsidelamp_my' - Color temperature value 333.00 is out of range [167.0 - 333.0]!
[21:51:37][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:51:37][D][light:066]:   Color temperature: 333.0 mireds
[21:51:37][D][light:071]:   Cold white: 0%, warm white: 100%
[21:51:37][D][ble_adv_light:048]: Corrected brightness: 1%
[21:51:37][D][ble_adv_light:070]: ct: 333.000, br: 0.012
[21:51:37][D][ble_adv_light:075]: BleAdvLight::write_state - Requested color temperature: 255
[21:51:37][D][zhijia:636]: UUID: '0xC630B1', tx: 9, Command: '0xAE', Args: [255,0,0]
[21:51:37][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.78.BB.91.E3.0D.D0.A3.73.92.CC.61.A9.AD.4C.4B.E9.DE.4A.5F.85.F6.9C.A9.19 (26)
[21:51:37][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:51:45][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:51:45][D][light:051]:   Brightness: 15%
[21:51:45][D][ble_adv_light:048]: Corrected brightness: 15%
[21:51:45][D][ble_adv_light:070]: ct: 333.000, br: 0.149
[21:51:45][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 38
[21:51:45][D][zhijia:636]: UUID: '0xC630B1', tx: 10, Command: '0xAD', Args: [38,0,0]
[21:51:45][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.A1.61.91.E3.0E.D0.A3.73.91.CF.61.A9.AD.4F.4B.EA.DE.4A.5F.85.F6.9C.A9.19 (26)
[21:51:45][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
- platform: ble_adv_controller
   ble_adv_controller_id: bedroom_bedsidelamp_my
   name: bedroom_bedsidelamp_my
   default_transition_length: 0s
   min_brightness: 0%

I also noticed errors during compilation.
Everything compiles and works, but if you can fix the compiler warnings, then it's better to do so.

Compiling .pioenvs/esp-childroom-chandelier/src/esphome/components/esp32_ble/ble.cpp.o
src/esphome/components/ble_adv_controller/zhijia.cpp: In function 'void esphome::bleadvcontroller::msc16_msc26::whitening_init(uint8_t, int*, size_t)':
src/esphome/components/ble_adv_controller/zhijia.cpp:72:32: warning: narrowing conversion of '((((int)val) >> 5) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:48: warning: narrowing conversion of '((((int)val) >> 4) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:64: warning: narrowing conversion of '((((int)val) >> 3) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:80: warning: narrowing conversion of '((((int)val) >> 2) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                                                     ~~~~~~~~~~~^~~
Compiling .pioenvs/esp-childroom-chandelier/src/esphome/components/esp32_ble/ble_advertising.cpp.o

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 23, 2024

@cRazyK67, For the compilation warning, the code is not mine but taken from someone else, I will correct but I need to perform some supplementary testing to be sure it does not impact the functionality (ZhiJia v0 and v1). For the issue you have, from what I see in the logs, there is no "brightness" command issued when changing the color temperature, so it means the color temperature command itself is having an unexpected behavior. Still the command is quite basic (a code and a 1-byte value). I have 2 hypothesis but I would need your help to test it as I do not own any ZhiJia lamp. Could you please:

  1. Add the following custom buttons to your config:
button:
- platform: ble_adv_controller
  ble_adv_controller_id: bedroom_bedsidelamp_my
  name: ct250
  cmd: custom
  args: [174,250,0,0,0]
- platform: ble_adv_controller
  ble_adv_controller_id: bedroom_bedsidelamp_my
  name: br1
  cmd: custom
  args: [173,3,0,0,0]
  1. Perform Test 1:
    1. set the light to cold
    2. set the Brightness to 100%
    3. set the Brightness to 1%
    4. press button "ct250". This simulates a setting of color temperature but with a max arg of 250 instead of 255
    5. tell me if you have the same behavior than in your step 4 or a proper behavior.
  2. If it does not work, perform Test 2:
    1. set the light to cold
    2. set the Brightness to 100%
    3. press button "br1" 2 times in a raw (in less than 1 second). This simulates a potential behavior of the app consisting in sending 2 times the message (kind of start and end...)
    4. set color temperature to warm
    5. tell me if you have the same behavior than in your step 4 or a proper behavior.

Last questions:

  • Do you have the same issue when brightness is 100% and you switch color temperature?
  • Did you have the same flickering issues with the 'ble_adv_light' component ?

Thanks in advance!

@afharo
Copy link

afharo commented Jul 24, 2024

This is awesome! Thank you for giving some love to this already-awesome attempt to support FanLamp ceiling fans 🧡

I'm an owner of 2 v1b (or v1x). It is only capable of pairing when selecting v1b, but there are a couple of things not working yet:

  1. I cannot control the direction of the fan. I get these logs when I try to change it
// Switch to reverse
15:47:09	[D]	[fan:021]	'Despacho Fanlamp' - Setting:
15:47:09	[D]	[fan:033]	  Direction: REVERSE
15:47:09	[D]	[ble_adv_fan:063]	BleAdvFan::control - Setting direction rev
15:47:09	[W]	[ble_adv_controller:059]	Unsupported command received: 34. Aborted.
15:47:09	[D]	[fan:120]	'Despacho Fanlamp' - Sending state:
15:47:09	[D]	[fan:121]	  State: ON
15:47:09	[D]	[fan:123]	  Speed: 1
15:47:09	[D]	[fan:129]	  Direction: REVERSE

// Switch to forward
15:47:57	[D]	[fan:021]	'Despacho Fanlamp' - Setting:
15:47:57	[D]	[fan:033]	  Direction: FORWARD
15:47:57	[D]	[ble_adv_fan:063]	BleAdvFan::control - Setting direction fwd
15:47:57	[W]	[ble_adv_controller:059]	Unsupported command received: 34. Aborted.
15:47:57	[D]	[fan:120]	'Despacho Fanlamp' - Sending state:
15:47:57	[D]	[fan:121]	  State: ON
15:47:57	[D]	[fan:123]	  Speed: 1
15:47:57	[D]	[fan:129]	  Direction: FORWARD
  1. The other thing I cannot control is the "indirect light": the lamp has 2 lights: The main one (which works great), and a secondary light (facing upwards, and it doesn't allow brightness control). I tried using different index (tried with 1-5) to see if that helped... but it doesn't seem to take any effect. No errors in the logs, though.
// Turn ON
15:52:05	[D]	[light:036]	'Despacho Fanlamp Luz Indirecta' Setting:
15:52:05	[D]	[light:047]	  State: ON
15:52:05	[D]	[ble_adv_light:040]	BleAdvLight::write_state - Switch ON
15:52:05	[D]	[fanlamp_pro:220]	ID: '0x7BD40F46', tx: 12, Command: '0x10', Args: [0,0,0]
15:52:05	[D]	[ble_adv_light:048]	Corrected brightness: 100%
15:52:05	[D]	[ble_adv_controller:088]	despacho_fanlamp - start advertising: F9.08.49.13.F0.69.25.4E.31.51.BA.B2.FA.48.24.CB.3B.CC.71.DF.88.7E.94.EA.B4.4C (26)
15:52:05	[D]	[ble_adv_controller:096]	despacho_fanlamp - stop advertising

// Turn OFF
15:52:08	[D]	[light:036]	'Despacho Fanlamp Luz Indirecta' Setting:
15:52:08	[D]	[light:047]	  State: OFF
15:52:08	[D]	[ble_adv_light:032]	BleAdvLight::write_state - Switch OFF
15:52:08	[D]	[fanlamp_pro:220]	ID: '0x7BD40F46', tx: 13, Command: '0x11', Args: [0,0,0]
15:52:08	[D]	[ble_adv_controller:088]	despacho_fanlamp - start advertising: F9.08.49.13.F0.69.25.4E.31.51.BA.32.FA.48.24.CB.3B.4C.71.F7.A0.F0.BC.AD.F6.4C (26)
15:52:08	[D]	[ble_adv_controller:096]	despacho_fanlamp - stop advertising

My current config:

external_components:
  # shorthand
  source: github://NicoIIT/esphome-components

ble_adv_controller:
  - id: despacho_fanlamp
    encoding: fanlamp_pro
    variant: v1b
    duration: 500
    reversed: false
    forced_id: 0

light:
  - platform: ble_adv_controller
    ble_adv_controller_id: despacho_fanlamp
    name: Despacho Fanlamp
    index: 0
  - platform: ble_adv_controller
    ble_adv_controller_id: despacho_fanlamp
    name: Despacho Fanlamp Luz Indirecta
    index: 2

fan:
  - platform: ble_adv_controller
    ble_adv_controller_id: despacho_fanlamp
    name: Despacho Fanlamp
    speed_count: 6
    use_direction: true

button:
  - platform: ble_adv_controller
    ble_adv_controller_id: despacho_fanlamp
    name: Pair Despacho Fanlaml
    cmd: pair

Thank you again!

@14roiron
Copy link

14roiron commented Jul 24, 2024

I also noticed errors during compilation. Everything compiles and works, but if you can fix the compiler warnings, then it's better to do so.

Compiling .pioenvs/esp-childroom-chandelier/src/esphome/components/esp32_ble/ble.cpp.o
src/esphome/components/ble_adv_controller/zhijia.cpp: In function 'void esphome::bleadvcontroller::msc16_msc26::whitening_init(uint8_t, int*, size_t)':
src/esphome/components/ble_adv_controller/zhijia.cpp:72:32: warning: narrowing conversion of '((((int)val) >> 5) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:48: warning: narrowing conversion of '((((int)val) >> 4) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:64: warning: narrowing conversion of '((((int)val) >> 3) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                                     ~~~~~~~~~~~^~~
src/esphome/components/ble_adv_controller/zhijia.cpp:72:80: warning: narrowing conversion of '((((int)val) >> 2) & 1)' from 'int' to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
     uint8_t v0[] = {(val >> 5) & 1, (val >> 4) & 1, (val >> 3) & 1, (val >> 2) & 1};
                                                                     ~~~~~~~~~~~^~~
Compiling .pioenvs/esp-childroom-chandelier/src/esphome/components/esp32_ble/ble_advertising.cpp.o

I think this is from my code, I will try to correct it, But feel free to so if you want, I may have still have some test vectors to verify the change

The change in brightness also occurs without flickering, however, when the color temperature changes, the brightness increases, according to my feelings, to a level of 40-50%

I do the following sequence of steps:

  1. I set the light to cold
  2. Set the Brightness to 100%
  3. Set the Brightness to 1%
  4. Set up warm light
  5. Set the brightness to 15%

After step 4, the brightness, as I wrote, is ~40-50% After step 5, the brightness decreases and is approximately the minimum for my lamp.

I can confirmed that I had the same issue before my lamp broke, and no messages where sent, so I am wondering if there is a compensation on the app to keep the brightness perceived as constant? I had no time to investigate

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 24, 2024

@afharo, Thanks for the testing, you are the first to use Fan with fanlamp_pro v1x !
The 'direction' feature was simply not implemented for v1, I missed it 😄. It should work now with my last commit.

For the 'index' feature to handle multiple lights, I never tested it as I do not have such lights, this was a guess but this was maybe a complete wrong read of the code... Could you please use the custom service in order to make a few tests ?
The custom service is a new service, available in HA -> developer tools -> Services
Select the service "esphome: <device_name>_cmd_despacho_fanlamp"
Enter 0 for all fields, except for the "cmd":
-> 16 (and press "call service") should switch the main light ON, just for validating the service is working OK
-> 17: switch main light OFF
-> 18: switch "AssistLed" ON -> may switch ON your secondary light
-> 19: switch "AssistLed" OFF -> may switch OFF your secondary light

If this is switching your secondary light then I could implement the proper handling, else I will have to dive into java and android code and I really hate that 🥲

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 24, 2024

@14roiron, I already corrected the warning in my last commit, quite easy in fact 😄
I am not sure if you could help on another topic, but would you own ZhiJia lights using encoding v0 (MSC16) or v1 (MSC26)? I do not own any so the software is pure reverse engineering and then end user testing would be really appreciated !

@afharo
Copy link

afharo commented Jul 24, 2024

@NicoIIT

-> 18: switch "AssistLed" ON -> may switch ON your secondary light
-> 19: switch "AssistLed" OFF -> may switch OFF your secondary light

It's working! This is the way!


The 'direction' feature was simply not implemented for v1, I missed it 😄. It should work now with my last commit.

It still doesn't... 😢

FWIW, I tried the HA service with the command 21 and changing arg0 to 0 and 1, and it changed the direction. But using the direction selector in HA doesn't work.

I wonder if the code is missing a handler somewhere. Other actions, I can see lines like [fanlamp_pro:220] ID: ... Command: .... The same happens when using the service: ID: '0x7BD40F46', tx: 13, Command: '0x15', Args: [0,0,0] and ID: '0x7BD40F46', tx: 14, Command: '0x15', Args: [1,0,0]

However, when changing the direction via HA, I get this other log warning:

18:55:11	[W]	[ble_adv_controller:059]	Unsupported command received: 34. Aborted

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 24, 2024

@afharo, this is strange, you should not have this Warning anymore. I tested on my side and it is working. Are you sure you pulled the changes? AFAIK esphome is not updating repos on his own, you have to do it manually... or simply delete the content of folder config/.esphome/external_components, this should trigger a re download of the repo.

BTW I added the proper handling of the secondary light, just add the following light to your config with option 'secondary':

  - platform: ble_adv_controller
    ble_adv_controller_id: despacho_fanlamp
    name: Despacho Fanlamp Luz Indirecta
    secondary: true

and remove the 'index' option now deprecated.

voilà 😄

@afharo
Copy link

afharo commented Jul 24, 2024

@NicoIIT, I had the default refresh: 1d in the external_components 😅
After setting it to refresh: 0s, I got the latest and greatest and (drumroll) everything works!

You're a true hero! Thanks for the iterations and for the quick responses and fixes! 🧡 🧡

@cRazyK67
Copy link

cRazyK67 commented Jul 24, 2024

I had the default refresh: 1d in the external_components 😅
After setting it to refresh: 0s, I got the latest and greatest and (drumroll) everything works!

@afharo Thanks for the hint.
Now I also automatically always have the latest version

The other thing I cannot control is the "indirect light": the lamp has 2 lights: The main one (which works great), and a secondary light (facing upwards, and it doesn't allow brightness control). I tried using different index (tried with 1-5) to see if that helped... but it doesn't seem to take any effect. No errors in the logs, though.

The same button is also available in the ZhiJia application.
It is possible to implement the functionality of secondaries light for ZhiJia v2?

Do you have the same issue when brightness is 100% and you switch color temperature?

I have not tested the 'ble_adv_light' component in such detail because... it didn't work correctly for me.

Did you have the same flickering issues with the 'ble_adv_light' component ?

Yes.

Perform Test 1:
set the light to cold
set the Brightness to 100%
set the Brightness to 1%
press button "ct250". This simulates a setting of color temperature but with a max arg of 250 instead of 255
tell me if you have the same behavior than in your step 4 or a proper behavior.

[21:27:57][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:27:57][D][light:051]:   Brightness: 52%
[21:27:57][D][ble_adv_light:048]: Corrected brightness: 52%
[21:27:57][D][ble_adv_light:070]: ct: 333.000, br: 0.522
[21:27:57][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 133
[21:27:57][D][zhijia:635]: UUID: '0xC630B1', tx: 33, Command: '0xAD', Args: [133,0,0]
[21:27:57][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.D6.3D.45.37.F1.04.77.A7.6E.1B.B5.7D.79.B0.B4.3E.0A.4A.5F.85.F6.9C.A9.19 (26)
[21:27:57][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:27:57][D][ble_adv_light:048]: Corrected brightness: 52%
[21:27:58][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:28:05][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[21:28:05][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:05][D][light:066]:   Color temperature: 167.0 mireds
[21:28:05][D][light:071]:   Cold white: 100%, warm white: 1%
[21:28:05][D][ble_adv_light:048]: Corrected brightness: 52%
[21:28:05][D][ble_adv_light:070]: ct: 167.001, br: 0.522
[21:28:05][D][ble_adv_light:075]: BleAdvLight::write_state - Requested color temperature: 0
[21:28:05][D][zhijia:635]: UUID: '0xC630B1', tx: 34, Command: '0xAE', Args: [0,0,0]
[21:28:05][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.53.BB.45.37.F2.04.77.A7.6D.18.B5.7D.79.B3.B4.3D.0A.4A.5F.85.F6.9C.A9.19 (26)
[21:28:05][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:05][D][ble_adv_light:048]: Corrected brightness: 52%
[21:28:07][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:28:08][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:08][D][light:051]:   Brightness: 100%
[21:28:08][D][ble_adv_light:048]: Corrected brightness: 100%
[21:28:08][D][ble_adv_light:070]: ct: 167.001, br: 1.000
[21:28:08][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 255
[21:28:08][D][zhijia:635]: UUID: '0xC630B1', tx: 35, Command: '0xAD', Args: [255,0,0]
[21:28:08][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.AE.47.47.35.F1.06.75.A5.6E.19.B7.7F.7B.B0.B4.3C.08.4A.5F.85.F6.9C.A9.19 (26)
[21:28:08][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:08][D][ble_adv_light:048]: Corrected brightness: 100%
[21:28:09][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:28:14][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:14][D][light:051]:   Brightness: 1%
[21:28:14][D][ble_adv_light:048]: Corrected brightness: 1%
[21:28:14][D][ble_adv_light:070]: ct: 167.001, br: 0.012
[21:28:14][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 3
[21:28:14][D][zhijia:635]: UUID: '0xC630B1', tx: 36, Command: '0xAD', Args: [3,0,0]
[21:28:14][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.AA.44.BF.CD.0E.FE.8D.5D.91.E1.4F.87.83.4F.4B.C4.F0.4A.5F.85.F6.9C.A9.19 (26)
[21:28:14][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:14][D][ble_adv_light:048]: Corrected brightness: 1%
[21:28:15][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:28:28][D][button:010]: 'ct250' Pressed.
[21:28:28][D][ble_adv_button:016]: BleAdvButton::press_action called
[21:28:28][D][zhijia:635]: UUID: '0xC630B1', tx: 37, Command: '0xAE', Args: [250,0,0]
[21:28:28][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.51.BE.BD.CF.0D.FC.8F.5F.92.E0.4D.85.81.4C.4B.C5.F2.4A.5F.85.F6.9C.A9.19 (26)
[21:28:28][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[21:28:33][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:33][D][light:051]:   Brightness: 20%
[21:28:33][D][ble_adv_light:048]: Corrected brightness: 20%
[21:28:33][D][ble_adv_light:070]: ct: 167.001, br: 0.200
[21:28:33][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 51
[21:28:33][D][zhijia:635]: UUID: '0xC630B1', tx: 38, Command: '0xAD', Args: [51,0,0]
[21:28:33][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.98.74.BD.CF.0E.FC.8F.5F.91.E3.4D.85.81.4F.4B.C6.F2.4A.5F.85.F6.9C.A9.19 (26)
[21:28:33][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[21:28:33][D][ble_adv_light:048]: Corrected brightness: 20%
[21:28:33][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising

If it does not work, perform Test 2:
set the light to cold
set the Brightness to 100%
press button "br1" 2 times in a raw (in less than 1 second). This simulates a potential behavior of the app consisting in sending 2 times the message (kind of start and end...)
set color temperature to warm
tell me if you have the same behavior than in your step 4 or a proper behavior.

[22:21:20][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[22:21:20][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:20][D][light:066]:   Color temperature: 167.0 mireds
[22:21:20][D][light:071]:   Cold white: 100%, warm white: 1%
[22:21:20][D][ble_adv_light:048]: Corrected brightness: 44%
[22:21:20][D][ble_adv_light:070]: ct: 167.001, br: 0.439
[22:21:20][D][ble_adv_light:075]: BleAdvLight::write_state - Requested color temperature: 0
[22:21:20][D][zhijia:635]: UUID: '0xC630B1', tx: 16, Command: '0xAE', Args: [0,0,0]
[22:21:20][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.61.BB.77.05.F2.36.45.95.6D.2A.87.4F.4B.B3.B4.0F.38.4A.5F.85.F6.9C.A9.19 (26)
[22:21:20][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:20][D][ble_adv_light:048]: Corrected brightness: 44%
[22:21:20][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:23][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:23][D][light:051]:   Brightness: 57%
[22:21:23][D][ble_adv_light:048]: Corrected brightness: 57%
[22:21:23][D][ble_adv_light:070]: ct: 167.001, br: 0.569
[22:21:23][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 145
[22:21:23][D][zhijia:635]: UUID: '0xC630B1', tx: 17, Command: '0xAD', Args: [145,0,0]
[22:21:23][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.F2.29.75.07.F1.34.47.97.6E.2B.85.4D.49.B0.B4.0E.3A.4A.5F.85.F6.9C.A9.19 (26)
[22:21:24][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:24][D][ble_adv_light:048]: Corrected brightness: 57%
[22:21:24][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:25][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:25][D][light:051]:   Brightness: 100%
[22:21:25][D][ble_adv_light:048]: Corrected brightness: 100%
[22:21:25][D][ble_adv_light:070]: ct: 167.001, br: 1.000
[22:21:25][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 255
[22:21:25][D][zhijia:635]: UUID: '0xC630B1', tx: 18, Command: '0xAD', Args: [255,0,0]
[22:21:25][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.60.B8.89.FB.0E.C8.BB.6B.91.D7.79.B1.B5.4F.4B.F2.C6.4A.5F.85.F6.9C.A9.19 (26)
[22:21:26][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:33][D][button:010]: 'br1' Pressed.
[22:21:33][D][ble_adv_button:016]: BleAdvButton::press_action called
[22:21:33][D][zhijia:635]: UUID: '0xC630B1', tx: 19, Command: '0xAD', Args: [3,0,0]
[22:21:33][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.62.BB.77.05.F1.36.45.95.6E.29.87.4F.4B.B0.B4.0C.38.4A.5F.85.F6.9C.A9.19 (26)
[22:21:33][D][button:010]: 'br1' Pressed.
[22:21:33][D][ble_adv_button:016]: BleAdvButton::press_action called
[22:21:33][D][zhijia:635]: UUID: '0xC630B1', tx: 20, Command: '0xAD', Args: [3,0,0]
[22:21:34][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:34][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.9A.44.8F.FD.0E.CE.BD.6D.91.D1.7F.B7.B3.4F.4B.F4.C0.4A.5F.85.F6.9C.A9.19 (26)
[22:21:34][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:36][D][light:352]: 'bedroom_bedsidelamp_my' - Setting cold/warm white channels using white/color temperature values.
[22:21:36][W][light:227]: 'bedroom_bedsidelamp_my' - Color temperature value 333.00 is out of range [167.0 - 333.0]!
[22:21:36][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:36][D][light:066]:   Color temperature: 333.0 mireds
[22:21:36][D][light:071]:   Cold white: 0%, warm white: 100%
[22:21:36][D][ble_adv_light:048]: Corrected brightness: 100%
[22:21:36][D][ble_adv_light:070]: ct: 333.000, br: 1.000
[22:21:36][D][ble_adv_light:075]: BleAdvLight::write_state - Requested color temperature: 255
[22:21:36][D][zhijia:635]: UUID: '0xC630B1', tx: 21, Command: '0xAE', Args: [255,0,0]
[22:21:36][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.64.BB.8D.FF.0D.CC.BF.6F.92.D0.7D.B5.B1.4C.4B.F5.C2.4A.5F.85.F6.9C.A9.19 (26)
[22:21:36][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:36][D][ble_adv_light:048]: Corrected brightness: 100%
[22:21:36][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising
[22:21:44][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:44][D][light:051]:   Brightness: 1%
[22:21:44][D][ble_adv_light:048]: Corrected brightness: 1%
[22:21:44][D][ble_adv_light:070]: ct: 333.000, br: 0.012
[22:21:44][D][ble_adv_light:079]: BleAdvLight::write_state - Requested brightness: 3
[22:21:44][D][zhijia:635]: UUID: '0xC630B1', tx: 22, Command: '0xAD', Args: [3,0,0]
[22:21:44][D][ble_adv_controller:088]: bedroom_bedsidelamp_my - start advertising: 22.9D.98.44.8D.FF.0E.CC.BF.6F.91.D3.7D.B5.B1.4F.4B.F6.C2.4A.5F.85.F6.9C.A9.19 (26)
[22:21:44][D][light:036]: 'bedroom_bedsidelamp_my' Setting:
[22:21:44][D][ble_adv_light:048]: Corrected brightness: 1%
[22:21:44][D][ble_adv_controller:096]: bedroom_bedsidelamp_my - stop advertising


in both tests, after changing the color temperature, the brightness increases

@pantherale0
Copy link

This works nearly perfectly for my needs, thank you @NicoIIT (also, this has come just in time for when I ordered a fan from aliexpress). Only issue I'm facing at the moment is that if I tie the light with a configuration of adaptive lighting (https://github.com/basnijholt/adaptive-lighting) it seems that while the light comes on, it immediately turns off, however commands still appear to be sent as per the logs below

logs_fanlamppro-controller_logs.txt

I will adjust the settings of adaptive lighting, perhaps the brightness / mired config is not going to work for this light.

Also noted in the logs is the following warning:

[20:35:49][W][light:227]: 'Master Bedroom Main Light' - Color temperature value 333.00 is out of range [167.0 - 333.0]!

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 24, 2024

@pantherale0, from what I saw in the logs the component you are using is sending a lot of commands to the light in a very short time-frame, and this not recommended.

I updated the doc to highlight the technical limitations of this kind of lights due to the BLE Advertising technology they are using.

To make it work you will either need:

  • to decrease (a lot) the duration (the minimum recommended by the BLE standard is 20ms) but it may have consequences (see the doc)
  • to configure the adaptive-lighting component so that it sends updates less frequently
    Hope you will manage to make it work!

@pantherale0
Copy link

Nice, yeah I had assumed it was sending too much too quickly and sending the controller into another universe. Thank you 🙂

I pulled my controller apart earlier to see what sort of chips are inside (all I saw in the ad was a picture of an app so had assumed there was some tuya based chip inside as thats what we normally get). There are 4 pads exposed on the bottom of my controller, I'm assuming its for programming purposes but maybe there are some signals that can be sent/received over this (I'd suggest some UART protocol but probably too advanced for the chips on these).

@pantherale0
Copy link

With the above information in mind, I think I've got adaptive lighting working. In case anyone else comes across this, within the config for adaptive-lighting:

transition: 0 (disable transitions)
initial_transition: 0 (disable transitions)
min_color_temp: 3000 (167 mired)
max_color_temp: 6000 (333 mired)
sleep_transition: 0 (disable transitions)
detect_non_ha_changes: false (no point having this on as documented, HA won't be aware of external changes anyway)
seperate_turn_on_commands: true (allows us to set a delay between sent commands to overcome BLE shenanigans)
send_split_delay: 50 (wait 50ms after sending each command)
adapt_delay: 2 (waits 2s after turning the light on to start adaptations)
intercept: true (intercepts light.turn_on service calls so that the adaptive lighting controller will start work straight away)
multi_light_intercept: true (same as above, only needs to be set if multiple lights are configured in the controller)

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 24, 2024

@cRazyK67, I added the secondary light control for ZhiJia, based on the fact the name in the software would be the same than for FanLamp Pro (Aid, do not ask me why 😄). Could you please test and tell me if it is working ?

Concerning the flickering issue, the next steps to progress on this will be quite huge:

  • Capture real traffic from ZhiJia App when the scenario is executed
  • Implement un-whitening of ZhiJia encoding (whatever v0/v1 or v2) in order to decode the packets and see what is effectively generated by the ZhiJia phone app. @14roiron , @aronsky : any idea how to do that ?

Given the amount of work, that the problem was already pre-existing and that the fix is not in the direct scope of this PR, I would recommend that we open a dedicated issue for this to track investigation progresses and resolution.

Sorry for not being able to solve this issue within the scope of this change 😢.

@14roiron 14roiron mentioned this pull request Jul 25, 2024
@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 29, 2024

@cRazyK67, as it is working, I pushed the changes to main.

You can have a look to the Dynamic Configuration section and try to use the Pairing using all variants, as done by the Phone App.

EDIT: and for the flickering problem, you could try to use the light transition which should work much better now and would do quite the same than what is done by the Phone App if you set default_transition_length to 200ms (in fact to the same value than what you have for duration)

@pantherale0
Copy link

@NicoIIT - all working perfect again now, thanks for all of this.

Was thinking, at the moment we default everything to an off state, perhaps we could store the last known state and use that instead? I've chucked the remote in the bottom of a drawer so assuming the state is suitable for my config as a bunch of automation is responsible for controlling the fan and light.

If you did think about implementing that, would probably recommend as a separate PR as this one seems quite big lol.

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 30, 2024

@Privet-zdarova, @cRazyK67, I just corrected a potential issue on ZhiJia encoding when changing brightness / color temperature that might be a cause of the flickering you experienced. To be tested to see if it helps.

@cRazyK67
Copy link

@NicoIIT It didn't help me.
The flickering when switching color temperature remained.

I have an n52840 dongle, When I have time I will intercept packets from the application

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Jul 31, 2024

@cRazyK67 , I am searching if the issue is coming from my software or was already existing in the software I used as a basis. Could you please try to use the component from this repo and attempt a pairing?

  • If it works it means I did not properly implement the encoder from this base repo, which could be the case as I did small changes to standardize the stack
  • if it does not work it means something was forgotten when the protocol was initially taken from the phone app, and that it is the direction to go

I found small differences for both cases, and I would appreciate if you could help.

Base configuration:

external_components:
  # shorthand
  source: github://flicker581/esphome-lampsmart

lampsmart_pro_light:

light:
  - platform: lampsmart_pro_light
    name: V1A light
    duration: 200
    variant: v1a

Please remove any reference to other source repo as it could conflict during the build.

EDIT: I corrected the first point, so just tell me if you manage to pair with this correction in fact 😄

@14roiron 14roiron mentioned this pull request Aug 1, 2024
@cRazyK67
Copy link

cRazyK67 commented Aug 3, 2024

@NicoIIT Im use this config https://github.com/flicker581/esphome-lampsmart/blob/main/example_lampsmart_pro_light.yaml

my chandelier still not pairing for all variants (v1..v3).

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Aug 13, 2024

@pantherale0, in fact this is already implemented by base ESPHome Light, using restore_mode: RESTORE_DEFAULT_OFF 😄.

@NicoIIT
Copy link
Contributor Author

NicoIIT commented Aug 20, 2024

@aronsky , I think the fork is now very far from this initial repo with 600 new lines of docs and 2700 new lines of code, so I will continue the work there.
Thanks a lot for the initial work, I hope you did not spent too much time reviewing this (very changing huge) PR 😄
@ All testers and participants in this thread, thanks for your help testing this new software, should you want to continue making it evolve do not hesitate to open Issues to request for bug fixes or new features!

@NicoIIT NicoIIT closed this Aug 20, 2024
@Privet-zdarova
Copy link

Privet-zdarova commented Aug 20, 2024

@Privet-zdarova, @cRazyK67, I just corrected a potential issue on ZhiJia encoding when changing brightness / color temperature that might be a cause of the flickering you experienced. To be tested to see if it helps.

Thank you very much!)) Everything works great for me with both zhijia v0 and zhijia v2!

The only problem is that I can't select "v0" in the latest update. But this can be solved by selecting "v1" in the configuration file. And after loading, you can change it to "v0" in the device settings.

This is the only small problem, everything else is great!
Screenshot_40

Screenshot_41

@aronsky
Copy link
Owner

aronsky commented Aug 22, 2024

@NicoIIT, I didn't get a chance to follow this discussion thoroughly, but taking a look at the fork, it certainly looks like you've done a fantastic job. I am considering merging this PR as is, as it seems that the discussion here is much more of a review than I could ever have done :)

Additionally, I've started work on transitioning this component to Home Assistant directly (without an explicit need for ESPHome). That's in early stages, we'll see how it goes. It'll be a lot easier for end users, I believe (and open up the component to people who have Bluetooth on their hub, but no ESP32 devices). But I imagine that translating all the encoding functions to Python is going to be a colossal PITA.

@aronsky aronsky reopened this Aug 22, 2024
@aronsky aronsky merged commit 43fe118 into aronsky:main Aug 22, 2024
@NicoIIT
Copy link
Contributor Author

NicoIIT commented Aug 23, 2024

@aronsky , good luck to port this to home assistant! With the work done on the encoders/decoders this should be much more easy (only standard 'crc16' functions, only 2 'whiten' simple functions).

The main issue will be with the bluetooth stack that is much less stable in a linux docker environment, I let you refer to this comment. I personally tried to implement some standard BLE Leds in HA component requiring to maintain a BLE connection and it never worked ok, whereas I needed 20 lines of code to do it in ESP32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants