Skip to content

Add option to disable channel-number auto-detection#579

Merged
rroller merged 2 commits into
rroller:mainfrom
YusDyr:feat/optional-channel-autodetect
May 31, 2026
Merged

Add option to disable channel-number auto-detection#579
rroller merged 2 commits into
rroller:mainfrom
YusDyr:feat/optional-channel-autodetect

Conversation

@YusDyr

@YusDyr YusDyr commented May 24, 2026

Copy link
Copy Markdown
Contributor

Add option to disable channel-number auto-detection

Closes #578.

Problem

The coordinator's _async_update_data probes async_get_snapshot(0) and, on success, resets self._channel_number = self._channel (line ~272 in __init__.py). The heuristic exists because some Dahua firmwares index channels from 0 instead of 1, and the camera's HTTP CGI endpoint reveals which family it belongs to.

On some cameras (full details in the linked issue) the HTTP snapshot endpoint cheerfully returns a JPEG at index 0 even though RTSP only streams on channel=1. The heuristic then mis-classifies the camera as 0-indexed and the resulting rtsp://.../cam/realmonitor?channel=0&subtype=0 returns HTTP 404 forever after.

There's currently no way for affected users to opt out.

Change

Add an auto_detect_channel option to the integration's OptionsFlow (default True, identical to current behavior). When disabled, the snapshot probe and channel reset are skipped, and _channel_number stays at channel + 1.

Existing installs are not affected — the new key isn't present in their options dict, so options.get(CONF_AUTO_DETECT_CHANNEL, True) returns the default True and the original code path runs.

Files

  • const.py — new CONF_AUTO_DETECT_CHANNEL constant.
  • __init__.py — gate the snapshot reset on the new option, mention the toggle in the DEBUG log.
  • config_flow.py — surface the toggle in the existing DahuaOptionsFlowHandler schema alongside the platform booleans.

How to use

After updating, Settings → Devices & services → Dahua (the affected camera) → Configure → uncheck "auto_detect_channel". The change takes effect on the next coordinator refresh (or immediately if the entry is reloaded).

Testing

Verified on a real install: with the toggle off, _channel_number stays at 1, RTSP stream comes up, camera.record produces a valid MP4. Other cameras in the same install (which rely on the heuristic) keep the option on and are unaffected. python -c "import ast; ast.parse(open(f).read())" clean on the three changed files.

YusDyr added 2 commits May 24, 2026 23:04
Some Dahua firmwares respond to HTTP snapshot at index 0 but only stream
RTSP on channel=1. The existing heuristic in _async_update_data assumes
that a successful snapshot(0) means the camera is 0-indexed and resets
_channel_number = _channel (e.g. 0), which then produces RTSP URLs with
channel=0 and HTTP 404 from the camera.

Add a new options-flow toggle "auto_detect_channel" (default True, so
existing setups are unaffected). When disabled, the channel-reset block
is skipped and _channel_number stays at channel + 1, letting users
recover when the heuristic misfires on their model.

- const.py: new CONF_AUTO_DETECT_CHANNEL constant
- __init__.py: gate the snapshot reset on the option, log its state
- config_flow.py: surface the toggle in the OptionsFlow alongside the
  platform enable/disable switches
Without this entry the new checkbox renders as the raw snake_case key in
the English UI. Other locales fall back to en.json so a single
translation update is enough.
@YusDyr

YusDyr commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

N.B. Prepared with Claude Code usage (opus 4.7 model).

@rroller
rroller merged commit c2b8b41 into rroller:main May 31, 2026
3 checks passed
@rroller

rroller commented May 31, 2026

Copy link
Copy Markdown
Owner

thanks!

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.

RTSP fails with channel=0 when camera also responds to HTTP snapshot at index 0

2 participants