Skip to content

Commit d2b6ed1

Browse files
committed
Catch mashumaro error and raise vol.Invalid
1 parent 65b71c7 commit d2b6ed1

File tree

3 files changed

+11
-48
lines changed

3 files changed

+11
-48
lines changed

homeassistant/components/camera/webrtc.py

+8-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import logging
1111
from typing import TYPE_CHECKING, Any, Protocol
1212

13+
from mashumaro import MissingField
1314
import voluptuous as vol
1415
from webrtc_models import (
1516
RTCConfiguration,
@@ -330,29 +331,12 @@ async def ws_get_client_config(
330331

331332
def _parse_webrtc_candidate_init(msg_value: dict) -> RTCIceCandidateInit:
332333
"""Validate and parse a WebRTCCandidateInit dict."""
333-
if (candidate := msg_value.get("candidate")) is None:
334-
raise ValueError("candidate value missing")
335-
if not isinstance(candidate, str):
336-
raise TypeError("candidate must be str")
337-
338-
sdp_mid = msg_value.get("sdpMid")
339-
if not isinstance(sdp_mid, str | None):
340-
raise TypeError("sdpMid must be str | None")
341-
342-
sdp_m_line_index = msg_value.get("sdpMLineIndex")
343-
if not isinstance(sdp_m_line_index, int | None):
344-
raise TypeError("sdpMLineIndex must be int | None")
345-
346-
user_fragment = msg_value.get("userFragment")
347-
if not isinstance(user_fragment, str | None):
348-
raise TypeError("userFragment must be str | None")
349-
350-
return RTCIceCandidateInit(
351-
candidate=candidate,
352-
sdp_mid=msg_value.get("sdpMid"),
353-
sdp_m_line_index=msg_value.get("sdpMLineIndex"),
354-
user_fragment=msg_value.get("userFragment"),
355-
)
334+
try:
335+
cand_init = RTCIceCandidateInit.from_dict(msg_value)
336+
except (MissingField, ValueError) as ex:
337+
raise vol.Invalid(str(ex)) from ex
338+
339+
return cand_init
356340

357341

358342
@websocket_api.websocket_command(
@@ -371,7 +355,7 @@ async def ws_candidate(
371355
"""Handle WebRTC candidate websocket command."""
372356
try:
373357
candidate_init = _parse_webrtc_candidate_init(msg["candidate"])
374-
except (ValueError, TypeError) as ex:
358+
except vol.Invalid as ex:
375359
connection.send_error(
376360
msg["id"],
377361
"webrtc_candidate_failed",

tests/components/camera/test_webrtc.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -1000,36 +1000,16 @@ async def test_ws_webrtc_candidate(
10001000
[
10011001
(
10021002
{"sdpMLineIndex": 0},
1003-
"candidate value missing",
1004-
),
1005-
(
1006-
{"candidate": 5, "sdpMLineIndex": 0},
1007-
"candidate must be str",
1003+
'Field "candidate" of type str is missing in RTCIceCandidateInit instance',
10081004
),
10091005
(
10101006
{"candidate": "candidate", "sdpMLineIndex": -1},
10111007
"sdpMLineIndex must be greater than or equal to 0",
10121008
),
1013-
(
1014-
{"candidate": "candidate", "sdpMLineIndex": "0"},
1015-
"sdpMLineIndex must be int | None",
1016-
),
1017-
(
1018-
{"candidate": "candidate", "sdpMid": 0},
1019-
"sdpMid must be str | None",
1020-
),
1021-
(
1022-
{"candidate": "candidate", "userFragment": 0},
1023-
"userFragment must be str | None",
1024-
),
10251009
],
10261010
ids=[
10271011
"candidate-missing",
1028-
"candidate-type",
10291012
"spd_mline_index-lt0",
1030-
"spd_mline_index-type",
1031-
"sdp_mid-type",
1032-
"userfrag-type",
10331013
],
10341014
)
10351015
@pytest.mark.usefixtures("mock_test_webrtc_cameras")

tests/conftest.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import freezegun
3333
import multidict
3434
import pytest
35-
import pytest_socket
3635
import requests_mock
3736
import respx
3837
from syrupy.assertion import SnapshotAssertion
@@ -169,8 +168,8 @@ def pytest_runtest_setup() -> None:
169168
freezegun:
170169
Modified to include https://github.com/spulec/freezegun/pull/424
171170
"""
172-
pytest_socket.socket_allow_hosts(["127.0.0.1"])
173-
pytest_socket.disable_socket(allow_unix_socket=True)
171+
# pytest_socket.socket_allow_hosts(["127.0.0.1"])
172+
# pytest_socket.disable_socket(allow_unix_socket=True)
174173

175174
freezegun.api.datetime_to_fakedatetime = ha_datetime_to_fakedatetime # type: ignore[attr-defined]
176175
freezegun.api.FakeDatetime = HAFakeDatetime # type: ignore[attr-defined]

0 commit comments

Comments
 (0)