Move Sonoff occupancy config entities to quirks, or expose the ultrasonic delay for all occupancy sensors

Open
#1,081 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

The entities are in zha/application/platforms/number/init.py (SonoffPresenceSenorTimeout, PIROccupiedToUnoccupiedDelayConfigurationEntity) and zha/application/platforms/select.py (SonoffPresenceDetectionSensitivity). Compare them with the SNZB-06P and SNZB-03P quirks in zhaquirks/sonoff/, then check which snapshots in tests/data/devices hold ultrasonic_o_to_u_delay. Done when the chosen option is agreed in the thread and the affected snapshot tests still pass.

Written by the indexing model from the issue text.

Description

entities quirks
Problem

ZHA creates the "presence detection timeout" number entity only for two Sonoff models. SonoffPresenceSenorTimeout is a ZHA-native entity on OccupancySensing.ultrasonic_o_to_u_delay (0x0020), matched by models=frozenset({"SNZB-06P", "SNZB-03P"}), range 15–60 s, unique ID {ieee}-1-1030-presence_detection_timeout. Its sibling SonoffPresenceDetectionSensitivity (ultrasonic_u_to_o_threshold, unique ID suffix detection_sensitivity) uses the same model match.

So device-specific config lives in the ZHA library, and quirks have to work around it. The SNZB-03P quirk in zigpy/zha-device-handlers#4628 imports SonoffPresenceSenorTimeout from zha.application.platforms.number and uses .replaces_entity() only to change the minimum to 5 s and add a unit and device class. Meanwhile any other sensor exposing ultrasonic_o_to_u_delay gets no entity for it, while the PIR equivalent, PIROccupiedToUnoccupiedDelayConfigurationEntity, matches every OccupancySensing server cluster (0–65535 s, created when the attribute is supported and cached).

Raised in zigpy/zha-device-handlers#4628 (review thread on zhaquirks/sonoff/snzb03p.py). We need to decide between two directions:

Option A: move it to quirks entirely
  • The SNZB-06P quirk (zhaquirks/sonoff/snzb06p.py) and the SNZB-03P quirk from zigpy/zha-device-handlers#4628 declare the timeout with .number(...) on ultrasonic_o_to_u_delay and the sensitivity with .enum(...) on ultrasonic_u_to_o_threshold, each with its own range.
  • Existing entity IDs are kept with unique_id_suffix="1030-presence_detection_timeout" and unique_id_suffix="1030-detection_sensitivity". v2 IDs don't include the cluster ID, so it has to be in the suffix.
  • After that quirks release, ZHA removes both Sonoff classes and SonoffPresenceDetectionSensitivityEnum. This is the same coupled order used for the Inovelli and STARKVIND moves: quirks first, then ZHA bumps its quirks pin and drops the classes.
  • The ZHA match is on the model string alone (eWeLink SNZB-03P, SONOFF SNZB-06P in the snapshots). Check that the quirks cover every manufacturer string that currently gets the entities.
Option B: a generic ultrasonic delay entity for all occupancy sensors
  • Add an UltrasonicOccupiedToUnoccupiedDelayConfigurationEntity to ZHA next to the PIR one: no model filter, 0–65535 s, duration device class, seconds. Optionally add the u→o delay too, like the PIR pair.
  • The Sonoff devices must not end up with both the generic and the Sonoff entity, and their existing unique ID (presence_detection_timeout) has to stay. Either the generic class takes over and keeps that suffix for those models, or the Sonoff-specific range moves to the quirks (Option A for the range only).
  • Other devices gain the entity. Of the current snapshots in tests/data/devices, ultrasonic_o_to_u_delay is supported and cached on sunricher-hk-dim-0x00000036 (value 0) and tze200-jva8ink8-ts0601 (value 600, a Tuya TS0601 whose occupancy cluster comes from the tuya_motion quirk). For the Tuya one, check whether a write reaches the device at all before exposing it there.
  • The sensitivity select stays Sonoff-specific. Its 1/2/3 enum is not a generic meaning of ultrasonic_u_to_o_threshold.
Either way

zigpy/zha-device-handlers#4628 imports SonoffPresenceSenorTimeout from ZHA. Removing or renaming that class breaks the quirk's import, so the quirk has to be updated in the same release step.

Dominant language
Python
Stars
96
Forks
84
Avg merge
2d 4h
Merged PRs (30d)
30

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zigpy/zha

All issues in zigpy/zha

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.