From 93571c2d01cc88739b9dcf64e998e964a1ba27aa Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 1 Apr 2022 18:38:03 +0200 Subject: [PATCH] Add EntityFeature enum to Camera (#69072) --- homeassistant/components/camera/__init__.py | 22 ++++++++++++++++----- homeassistant/components/demo/camera.py | 4 ++-- tests/components/camera/test_init.py | 4 ++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index f3258681b52..c9045f7975d 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -8,6 +8,7 @@ from collections.abc import Awaitable, Callable, Iterable 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 @@ -88,7 +89,16 @@ STATE_RECORDING: Final = "recording" 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 @@ -499,7 +509,7 @@ class Camera(Entity): """ 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 @@ -535,7 +545,8 @@ class Camera(Entity): 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 @@ -543,7 +554,8 @@ class Camera(Entity): 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. """ @@ -682,7 +694,7 @@ class Camera(Entity): 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 diff --git a/homeassistant/components/demo/camera.py b/homeassistant/components/demo/camera.py index c1bf54d4629..25026bce11b 100644 --- a/homeassistant/components/demo/camera.py +++ b/homeassistant/components/demo/camera.py @@ -3,7 +3,7 @@ from __future__ import annotations 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 @@ -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.""" diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index e72941ef488..1b30facf1de 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -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 @@ -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