Skip to content

Commit

Permalink
Deprecate deprecated camera constants (home-assistant#106095)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Dec 23, 2023
1 parent 3404bd4 commit 20ba764
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
23 changes: 18 additions & 5 deletions homeassistant/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE,
)
from homeassistant.helpers.deprecation import (
DeprecatedConstantEnum,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import async_track_time_interval
Expand All @@ -60,6 +65,8 @@
from homeassistant.loader import bind_hass

from .const import ( # noqa: F401
_DEPRECATED_STREAM_TYPE_HLS,
_DEPRECATED_STREAM_TYPE_WEB_RTC,
CAMERA_IMAGE_TIMEOUT,
CAMERA_STREAM_SOURCE_TIMEOUT,
CONF_DURATION,
Expand All @@ -70,8 +77,6 @@
PREF_ORIENTATION,
PREF_PRELOAD_STREAM,
SERVICE_RECORD,
STREAM_TYPE_HLS,
STREAM_TYPE_WEB_RTC,
StreamType,
)
from .img_util import scale_jpeg_camera_image
Expand Down Expand Up @@ -105,8 +110,16 @@ class CameraEntityFeature(IntFlag):

# These SUPPORT_* constants are deprecated as of Home Assistant 2022.5.
# Pleease use the CameraEntityFeature enum instead.
SUPPORT_ON_OFF: Final = 1
SUPPORT_STREAM: Final = 2
_DEPRECATED_SUPPORT_ON_OFF: Final = DeprecatedConstantEnum(
CameraEntityFeature.ON_OFF, "2025.1"
)
_DEPRECATED_SUPPORT_STREAM: Final = DeprecatedConstantEnum(
CameraEntityFeature.STREAM, "2025.1"
)

# Both can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(dir_with_deprecated_constants, module_globals=globals())

RTSP_PREFIXES = {"rtsp://", "rtsps://", "rtmp://"}

Expand Down Expand Up @@ -215,7 +228,7 @@ async def _async_get_stream_image(
height: int | None = None,
wait_for_next_keyframe: bool = False,
) -> bytes | None:
if not camera.stream and camera.supported_features & SUPPORT_STREAM:
if not camera.stream and camera.supported_features & CameraEntityFeature.STREAM:
camera.stream = await camera.async_create_stream()
if camera.stream:
return await camera.stream.async_get_image(
Expand Down
16 changes: 14 additions & 2 deletions homeassistant/components/camera/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Constants for Camera component."""
from enum import StrEnum
from functools import partial
from typing import Final

from homeassistant.helpers.deprecation import (
DeprecatedConstantEnum,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)

DOMAIN: Final = "camera"

DATA_CAMERA_PREFS: Final = "camera_prefs"
Expand Down Expand Up @@ -36,5 +43,10 @@ class StreamType(StrEnum):

# These constants are deprecated as of Home Assistant 2022.5
# Please use the StreamType enum instead.
STREAM_TYPE_HLS = "hls"
STREAM_TYPE_WEB_RTC = "web_rtc"
_DEPRECATED_STREAM_TYPE_HLS = DeprecatedConstantEnum(StreamType.HLS, "2025.1")
_DEPRECATED_STREAM_TYPE_WEB_RTC = DeprecatedConstantEnum(StreamType.WEB_RTC, "2025.1")


# Both can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(dir_with_deprecated_constants, module_globals=globals())
39 changes: 37 additions & 2 deletions tests/components/camera/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
from http import HTTPStatus
import io
from types import ModuleType
from unittest.mock import AsyncMock, Mock, PropertyMock, mock_open, patch

import pytest
Expand All @@ -26,6 +27,7 @@

from .common import EMPTY_8_6_JPEG, WEBRTC_ANSWER, mock_turbo_jpeg

from tests.common import import_and_test_deprecated_constant_enum
from tests.typing import ClientSessionGenerator, WebSocketGenerator

STREAM_SOURCE = "rtsp://127.0.0.1/stream"
Expand Down Expand Up @@ -939,7 +941,7 @@ async def test_use_stream_for_stills(
# Test when the integration does not provide a stream_source should fail
with patch(
"homeassistant.components.demo.camera.DemoCamera.supported_features",
return_value=camera.SUPPORT_STREAM,
return_value=camera.CameraEntityFeature.STREAM,
):
resp = await client.get("/api/camera_proxy/camera.demo_camera")
await hass.async_block_till_done()
Expand All @@ -953,7 +955,7 @@ async def test_use_stream_for_stills(
"homeassistant.components.camera.create_stream"
) as mock_create_stream, patch(
"homeassistant.components.demo.camera.DemoCamera.supported_features",
return_value=camera.SUPPORT_STREAM,
return_value=camera.CameraEntityFeature.STREAM,
), patch(
"homeassistant.components.demo.camera.DemoCamera.use_stream_for_stills",
return_value=True,
Expand All @@ -971,3 +973,36 @@ async def test_use_stream_for_stills(
mock_stream.async_get_image.assert_called_once()
assert resp.status == HTTPStatus.OK
assert await resp.read() == b"stream_keyframe_image"


@pytest.mark.parametrize(
"enum",
list(camera.const.StreamType),
)
@pytest.mark.parametrize(
"module",
[camera, camera.const],
)
def test_deprecated_stream_type_constants(
caplog: pytest.LogCaptureFixture,
enum: camera.const.StreamType,
module: ModuleType,
) -> None:
"""Test deprecated stream type constants."""
import_and_test_deprecated_constant_enum(
caplog, module, enum, "STREAM_TYPE_", "2025.1"
)


@pytest.mark.parametrize(
"entity_feature",
list(camera.CameraEntityFeature),
)
def test_deprecated_support_constants(
caplog: pytest.LogCaptureFixture,
entity_feature: camera.CameraEntityFeature,
) -> None:
"""Test deprecated support constants."""
import_and_test_deprecated_constant_enum(
caplog, camera, entity_feature, "SUPPORT_", "2025.1"
)
2 changes: 1 addition & 1 deletion tests/components/rtsp_to_webrtc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def mock_camera(hass) -> AsyncGenerator[None, None]:
return_value=STREAM_SOURCE,
), patch(
"homeassistant.components.camera.Camera.supported_features",
return_value=camera.SUPPORT_STREAM,
return_value=camera.CameraEntityFeature.STREAM,
):
yield

Expand Down

0 comments on commit 20ba764

Please sign in to comment.