Skip to content

Fix: Prevent TypeError in ws_poster when image is None - #961

Merged
AlexxIT merged 1 commit into
AlexxIT:masterfrom
skircr115:master
Oct 6, 2026
Merged

AlexxIT merged 1 commit into
AlexxIT:masterfrom
skircr115:master

Conversation

@skircr115

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug in ws_poster where an unhandled TypeError is thrown if the camera entity fails to provide an image.

Motivation and Context

Currently, if image evaluates to None, the debug logger attempts to execute len(image). This crashes the HTTP request and dumps the following traceback into the Home Assistant logs:

TypeError: object of type 'NoneType' has no len()

This prevents a 500 Internal Server Error and stops log spam for users whose cameras occasionally drop offline or fail to serve a thumbnail.

Changes

  • Updated the _LOGGER.debug call in ws_poster to safely check if image exists before checking its length.

Gracefully handle cases where the camera image cannot be fetched (evaluates to None). Previously, attempting to log `len(image)` caused an unhandled TypeError, resulting in a 500 Internal Server Error.
@fuzzybear62

Copy link
Copy Markdown

Confirming this fix. I get the same TypeError: object of type 'NoneType' has no len()
whenever a camera briefly fails to serve a thumbnail, and @skircr115's guard is the right one:
the len(image) in the _LOGGER.debug call has to check that image exists first — otherwise
the debug line itself crashes the request into a 500 and spams the HA log, on what is only a
logging statement.

I carry the identical guard in production:

_LOGGER.debug(f"webrtc image_entity: {image_entity} - {len(image) if image else 0}")

For my own use, to run my camera fleet, I developed a fully-compatible drop-in fork
(https://github.com/fuzzybear62/webrtc) that — among other things — includes this fix.
Fair warning: I maintain it for my own multi-camera fleet, so it's provided as-is /
best-effort, not a supported product.

davidcoulson added a commit to davidcoulson/WebRTC that referenced this pull request Sep 11, 2026
Cherry-picked 5 unmerged bug-fix PRs from upstream AlexxIT/WebRTC
(stale since 2025-11-26):
- PR AlexxIT#938 (vinnybad): fix InvalidStateError crash in MSE updateend handler
- PR AlexxIT#956 (joyjit): reject expired/invalid stream authSig with 403 instead
  of 401, avoiding HA's own login-ban middleware locking out the household
- PR AlexxIT#961 (skircr115): prevent TypeError in ws_poster when image is None
- PR AlexxIT#942 (mayerwin): fix media_player.play_media via go2rtc /api/ffmpeg
- PR AlexxIT#951 (daveyijzermans): only fall back to muted playback on
  NotAllowedError, not on every transient play() rejection
davidcoulson added a commit to davidcoulson/WebRTC that referenced this pull request Sep 11, 2026
Correctness:
- ws_poster/_get_image_from_entity_id: the cherry-picked PR AlexxIT#961 fix only
  silenced the crash in the debug log line, but still returned HTTP 200
  with an empty body when an image entity has no image. Now raises
  HTTPNotFound so callers see a real failure instead of a false success.
  Also switched the bare Exception in _get_image_from_entity_id to
  HTTPNotFound for the same reason.
- media_player.py: replaced assert r.ok (stripped under python -O, no
  user-facing message) with HomeAssistantError including the status
  code, in both async_play_media and async_media_stop.
- media_player.py async_update: a bare except clause was catching
  asyncio.CancelledError (a BaseException) along with everything else,
  which can interfere with clean task cancellation. Narrowed to the
  actual expected failure modes (ClientError, TimeoutError, KeyError,
  ValueError) and logs at debug level instead of silently swallowing.
- config_flow.py: the go2rtc.yaml write was a blocking file write inside
  an async config flow step; moved to hass.async_add_executor_job.
- __init__.py: HomeAssistant was imported from
  homeassistant.components.binary_sensor (fragile, unrelated module);
  now imported from homeassistant.core directly.

HA design-language alignment:
- Added strings.json as the canonical English source (was previously
  only in translations/en.json, which hassfest flags as outdated).
- Added a visual (GUI) card editor via ha-form + entity/text/boolean
  selectors, registered through getConfigElement(), covering the common
  config fields (entity, url, title, mode, muted, background, ui).
  Advanced options (streams array, digital PTZ, etc.) remain available
  through the YAML editor, which Hass still offers alongside this one.
@AlexxIT AlexxIT self-assigned this Oct 6, 2026
@AlexxIT AlexxIT added the enhancement New feature or request label Oct 6, 2026
@AlexxIT
AlexxIT merged commit b5fdb5c into AlexxIT:master Oct 6, 2026
@AlexxIT

AlexxIT commented Oct 6, 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

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants