Skip to content

Conversation

MindFreeze
Copy link
Contributor

@MindFreeze MindFreeze commented Jul 19, 2025

Proposed change

Add the detached relay and related entities for ZBM5 but there is a catch.
This switch has 2 modes because the neutral wire is optional. And if there is no neutral wire, detached relay is not supported. This can be determined from the "work_mode" sensor in this PR but I don't know how to conditionally add the detached relay entities only if work_mode=1

The Sonoff site says that detached relay is only supported when there is a Neutral wire provided but after testing with 2 dual switches, this seems to be false. It works fine without a Neutral. I only tested it with a 20W light though so it may not work as well with a smaller load.

Additional information

Checklist

  • The changes are tested and work correctly
  • pre-commit checks pass / the code has been formatted using Black
  • Tests have been added to verify that the new code works

Copy link

codecov bot commented Jul 19, 2025

Codecov Report

❌ Patch coverage is 60.78431% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.32%. Comparing base (aaf89ff) to head (b7091d0).
⚠️ Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
zhaquirks/sonoff/zbm5.py 60.78% 20 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #4198      +/-   ##
==========================================
- Coverage   91.46%   91.32%   -0.14%     
==========================================
  Files         341      342       +1     
  Lines       11203    11255      +52     
==========================================
+ Hits        10247    10279      +32     
- Misses        956      976      +20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MindFreeze MindFreeze requested a review from puddly July 21, 2025 05:40
@MindFreeze MindFreeze marked this pull request as draft July 25, 2025 12:51
@TheJulianJES TheJulianJES added the needs review This PR should be reviewed soon, as it generally looks good. label Jul 26, 2025
@MindFreeze MindFreeze marked this pull request as ready for review July 26, 2025 12:15
@MindFreeze MindFreeze marked this pull request as draft July 26, 2025 12:15
@MindFreeze MindFreeze marked this pull request as ready for review July 26, 2025 12:31
@Jolanrensen
Copy link

Hi! Thanks for taking the time to make the quirk :)

Personally I have the ZBM5-3C and -2C (Firmware: 0x00001003) and I'm running 2025.8 beta 1. I tried adding your version of the quirk to my Hass instance and while I am able to change the detach_relay_mask, the relay_x_detached only report "None".
I assume this is also why the 3 "Detach Relay" switches are disabled in the device.

Is there something I could test?

@MindFreeze
Copy link
Contributor Author

That was an old version. On the latest, you shouldn't see a mask. Only switches. And they are disabled by default. You have to enable them.

@Jolanrensen
Copy link

@MindFreeze I meant in the "Manage Zigbee Device" screen, but gotcha, enabling the switches successfully allows me to toggle detach mode indeed!

I don't see anything being logged when I press one of the buttons in detached mode, though. Does that work on your end? Can you respond to any physical button presses?

@MindFreeze
Copy link
Contributor Author

Yes. On my switches it works as expected. The button toggles the switch in HA without changing the state of the physical relay

@Jolanrensen
Copy link

Jolanrensen commented Aug 1, 2025

Yes. On my switches it works as expected. The button toggles the switch in HA without changing the state of the physical relay

So, you get 3 hass switches per sonoff button?

  • Detach Relay x Switch
  • switch responding to physical button x
  • switch corresponding to physical relay x

Might be slightly different on my end as I have the 120 version

@MindFreeze
Copy link
Contributor Author

Yes. On my switches it works as expected. The button toggles the switch in HA without changing the state of the physical relay

So, you get 3 hass switches per sonoff button?

  • Detach Relay x Switch
  • switch responding to physical button x
  • switch corresponding to physical relay x

Might be slightly different on my end as I have the 120 version

No, I get 2 switches per button. 1 for the button/relay and 1 to detach the relay. It works as described in zigpy/zigpy#1612 (comment)

@Jolanrensen
Copy link

Jolanrensen commented Aug 7, 2025

No, I get 2 switches per button. 1 for the button/relay and 1 to detach the relay. It works as described in zigpy/zigpy#1612 (comment)

Ah I see :) They have an "Opening" sensor that changes state whenever detached mode is enabled and a button is pressed. I also have an ZBMINIR2 where this works as expected. Unfortunately, there's no sensor for my ZBM5-3C-120, so there's no way to detect a button press in detached mode :/

I was kind of expecting that it worked similarly to my Aqara Switch. This doesn't have a "sensor" in detached mdoe, but it simply fires events whenever a button is pressed or double-pressed, which you can hook into in automations.

@sl33nyc
Copy link

sl33nyc commented Aug 7, 2025

FWIW, I successfully used this quirk from this PR after manually adding it to my 2025.8.0 installation. My use case is to turn one of the two available button+relay into just a button, while the other button+relay remains button+relay.

Yeah, unlike the ZBMINIR2, which I also have, there isn't a separate Opening entity.

Screenshot 2025-08-07 at 11 05 22 AM

where:

  • Garage Door events represent just button presses.
  • Driveway events represent button presses + relay on/off.

:shipit:

@Jolanrensen
Copy link

@sl33nyc nice, but if you don't use the relay of the first button, then you don't even need detached mode, do you?

In proper detached mode, you should be able to handle the relays and buttons completely separately.
For example, you should be able to press button 1, and, using an automation, only switch relay 2 without changing relay 1. Or you could double-press button 2, relay 2 stays unchanged, but you turn on a completely different light in the house.

In my case, that's what I'm trying to achieve. The 3 relays all have lights attached to them, but a combination of presses, or a doublepress, should control all lights in my living room at once.

@MindFreeze
Copy link
Contributor Author

These sonoff switches don't support that. I don't even see an output cluster for OnOff, so you can't bind it to a light, which is the original use case for detached relay.

@Jolanrensen
Copy link

@MindFreeze That's a shame :/ But I think you're right. Sonoff gives as a usecase: "With the detach relay enabled, smart lights won’t lose power when the wall switch is turned off, allowing them to remain smart at all times. *Supports connecting smart lights only in the neutral required mode." However, if you want to supply permanent power to a smart lightbulb anyway, why connect it to the smart switch in the first place? XD
Z2M also doesn't expose any actions (in contrast to my aqara switch).

I wonder if there's a way to differentiate between "turned X" and "turned X triggered by action" in automations... That would allow me to build a workaround in detached mode. Because "turned X" means a button press, which doesn't change the relay, and "turned X triggered by action" means an action taken by home assistant, which does change the relay.
image

Anyway, I'll figure it out. Looks like the PR can achieve everything that's possible :) thanks! For me it can be merged too

@0x55555555
Copy link

Hello,

I've applied this locally and tested Detached Relay mode works as expected.

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review This PR should be reviewed soon, as it generally looks good.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Device Support Request] Sonoff ZBM5 (Specifically "Detached Relay" Support)
6 participants