Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions homeassistant/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from contextlib import suppress
from dataclasses import dataclass
from datetime import datetime, timedelta
from enum import IntEnum
from functools import partial
import hashlib
import logging
Expand Down Expand Up @@ -88,7 +89,16 @@
STATE_STREAMING: Final = "streaming"
STATE_IDLE: Final = "idle"

# Bitfield of features supported by the camera entity

class CameraEntityFeature(IntEnum):
"""Supported features of the camera entity."""

ON_OFF = 1
STREAM = 2


# 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

Expand Down Expand Up @@ -499,7 +509,7 @@ def frontend_stream_type(self) -> str | None:
"""
if hasattr(self, "_attr_frontend_stream_type"):
return self._attr_frontend_stream_type
if not self.supported_features & SUPPORT_STREAM:
if not self.supported_features & CameraEntityFeature.STREAM:
return None
if self._rtsp_to_webrtc:
return STREAM_TYPE_WEB_RTC
Expand Down Expand Up @@ -535,15 +545,17 @@ async def async_create_stream(self) -> Stream | None:
async def stream_source(self) -> str | None:
"""Return the source of the stream.

This is used by cameras with SUPPORT_STREAM and STREAM_TYPE_HLS.
This is used by cameras with CameraEntityFeature.STREAM
and STREAM_TYPE_HLS.
"""
# pylint: disable=no-self-use
return None

async def async_handle_web_rtc_offer(self, offer_sdp: str) -> str | None:
"""Handle the WebRTC offer and return an answer.

This is used by cameras with SUPPORT_STREAM and STREAM_TYPE_WEB_RTC.
This is used by cameras with CameraEntityFeature.STREAM
and STREAM_TYPE_WEB_RTC.

Integrations can override with a native WebRTC implementation.
"""
Expand Down Expand Up @@ -682,7 +694,7 @@ async def async_refresh_providers(self) -> None:

async def _async_use_rtsp_to_webrtc(self) -> bool:
"""Determine if a WebRTC provider can be used for the camera."""
if not self.supported_features & SUPPORT_STREAM:
if not self.supported_features & CameraEntityFeature.STREAM:
return False
if DATA_RTSP_TO_WEB_RTC not in self.hass.data:
return False
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/demo/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pathlib import Path

from homeassistant.components.camera import SUPPORT_ON_OFF, Camera
from homeassistant.components.camera import Camera, CameraEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -39,7 +39,7 @@ class DemoCamera(Camera):

_attr_is_streaming = True
_attr_motion_detection_enabled = False
_attr_supported_features = SUPPORT_ON_OFF
_attr_supported_features = CameraEntityFeature.ON_OFF

def __init__(self, name, content_type):
"""Initialize demo camera component."""
Expand Down
4 changes: 2 additions & 2 deletions tests/components/camera/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def mock_stream_source_fixture():
return_value=STREAM_SOURCE,
) as mock_stream_source, patch(
"homeassistant.components.camera.Camera.supported_features",
return_value=camera.SUPPORT_STREAM,
return_value=camera.CameraEntityFeature.STREAM,
):
yield mock_stream_source

Expand All @@ -71,7 +71,7 @@ async def mock_hls_stream_source_fixture():
return_value=HLS_STREAM_SOURCE,
) as mock_hls_stream_source, patch(
"homeassistant.components.camera.Camera.supported_features",
return_value=camera.SUPPORT_STREAM,
return_value=camera.CameraEntityFeature.STREAM,
):
yield mock_hls_stream_source

Expand Down