Skip to content

Add quirk for SONOFF SNZB-03PR2 motion sensor - #4714

Open
viys wants to merge 12 commits into
zigpy:devfrom
viys:dev
Open

Add quirk for SONOFF SNZB-03PR2 motion sensor#4714
viys wants to merge 12 commits into
zigpy:devfrom
viys:dev

Conversation

@viys

@viys viys commented Feb 2, 2026

Copy link
Copy Markdown
  • Add support for SONOFF SNZB-03PR2 occupancy and illuminance reporting
  • Implement custom cluster for illuminance compensation

Proposed change

This PR adds a new quirk for the SONOFF SNZB-03PR2 motion sensor.

While the device identifies as a standard motion sensor, it requires specific cluster handling for:

  • Occupancy (Motion) Reporting: Proper mapping of the occupancy cluster to ensure consistent state updates in Home Assistant.
  • Illuminance Reporting: Implementation of the IlluminanceMeasurement cluster to provide real-time light level data.
  • Custom Attributes: Added support for local configuration of Detection Interval and Illumination Offset.
  • Attribute Synchronization: Implemented logic to ensure that when these values are adjusted physically or via other local methods, the ZHA UI (and underlying attributes) stay synchronized with the device's actual state.

Additional information

Device Type: Zigbee End Device (Motion & Light Sensor).

Testing: Verified that the motion entity resets correctly according to the defined "Detection Interval" and that the illuminance values reflect the "Illumination Offset" set in the attributes.

Dependencies: This quirk is compatible with recent zigpy versions (tested with 0.91.5+ style method signatures).

Device diagnostics

sonoff_snzb03pr2_diagnostics.json

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
  • Device diagnostics data has been attached

* Add support for SONOFF SNZB-03PR2 occupancy and illuminance reporting
* Implement custom cluster for illuminance compensation
@codecov

codecov Bot commented Feb 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.60%. Comparing base (b139f81) to head (b82f757).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #4714   +/-   ##
=======================================
  Coverage   92.60%   92.60%           
=======================================
  Files         424      425    +1     
  Lines       14667    14679   +12     
=======================================
+ Hits        13582    13594   +12     
  Misses       1085     1085           

☔ View full report in Codecov by Harness.
📢 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.

Comment thread zhaquirks/sonoff/snzb03pr2.py Outdated
Comment thread zhaquirks/sonoff/snzb03pr2.py Outdated
class AttributeDefs(OccupancySensing.AttributeDefs):
"""Custom occupancy-related attributes for SNZB-03PR2."""

pir_occupied_to_unoccupied_delay = ZCLAttributeDef(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already exists in the ZCL on the normal OccupancySensing cluster as pir_o_to_u_delay.

Comment thread zhaquirks/sonoff/snzb03pr2.py Outdated
id=SONOFF_ILLUM_COMP_ATTR_ID,
type=t.int16s,
access="rw",
is_manufacturer_specific=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've moving from is_manufacturer_specific=True to explicit manufacturer codes per attribute/command. From your diagnostics, it looks like it should be manufacturer_code=0x1286.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger: zhaquirks
Source: /usr/local/lib/python3.13/site-packages/zhaquirks/__init__.py:481
First appearance: 10:16:41 (1 occurrence)
Last record: 10:16:41

Unexpected exception importing custom quirk 'snzb03pr2'
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/zhaquirks/__init__.py", line 479, in setup
    spec.loader.exec_module(module)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 1023, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/config/zha_quirks/snzb03pr2.py", line 11, in <module>
    class SonoffPrivateCluster(CustomCluster):
    ...<13 lines>...
            )
  File "/config/zha_quirks/snzb03pr2.py", line 17, in SonoffPrivateCluster
    class AttributeDefs(BaseAttributeDefs):
    ...<7 lines>...
        )
  File "/config/zha_quirks/snzb03pr2.py", line 20, in AttributeDefs
    illumination_compensation = ZCLAttributeDef(
        id=0x2018,
    ...<2 lines>...
        manufacturer_code=None,
    )
TypeError: ZCLAttributeDef.__init__() got an unexpected keyword argument 'manufacturer_code'

Hello, I encountered an error after changing is_manufacturer_specific to manufacturer_code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you're running the latest Home Assistant beta, it will be in the stable release in a few days.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oki, i`ll check

Remove custom OccupancySensing cluster in favor of standard zigpy
attribute. Clean up constant definitions and explicitly set
manufacturer_code for private cluster attribute.
@viys

viys commented Feb 3, 2026

Copy link
Copy Markdown
Author

hello, I have made the changes and completed the testing.

@TheJulianJES TheJulianJES left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine in general. I'll get back to you on whether we want to do the pir_o_to_u_delay number entity per v2 quirk or just generally in ZHA for all devices.

Comment thread zhaquirks/sonoff/snzb03pr2.py
Comment thread zhaquirks/sonoff/snzb03pr2.py
Co-authored-by: TheJulianJES <TheJulianJES@users.noreply.github.com>

@viys viys left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have completed the changes.

@viys

viys commented Feb 5, 2026

Copy link
Copy Markdown
Author

This PR has completed code changes and testing, CI is all green, and all review comments have been addressed. Are there any further actions required? If everything is in order, when can this PR be merged into the dev branch?

@newash newash mentioned this pull request Feb 10, 2026
4 tasks
Comment thread zhaquirks/sonoff/snzb03pr2.py
@TheJulianJES TheJulianJES added priority: medium This should be addressed or looked at soon manufacturer This request was made by the device's manufacturer labels May 8, 2026
@viys

viys commented Jun 7, 2026

Copy link
Copy Markdown
Author

Hi,

I would like to ask about the current status of this PR. The driver has been tested and appears to work correctly, but it has not yet been merged into the main branch. Could you please let me know if there are any outstanding issues, required changes, or additional reviews needed before it can be accepted? I’m happy to make adjustments if necessary.

Thank you for your time and support!

@zigpy-review-bot zigpy-review-bot added bot: needs changes PR needs changes per LLM bot: 2.0 migration needed PR needs ZHA/quirks 2.0.0 migration per LLM new quirk Adds support for a new device bot: small PR PR is small per LLM labels Jul 19, 2026
@zigpy-review-bot zigpy-review-bot removed the bot: small PR PR is small per LLM label Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: needs changes PR needs changes per LLM bot: 2.0 migration needed PR needs ZHA/quirks 2.0.0 migration per LLM manufacturer This request was made by the device's manufacturer new quirk Adds support for a new device priority: medium This should be addressed or looked at soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants