Add EntityFeature enum to Camera (#69072)

This commit is contained in:
Franck Nijhof 2022-04-01 18:38:03 +02:00 committed by GitHub
parent 7f1b90a51c
commit 93571c2d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View file

@ -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

View file

@ -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."""

View file

@ -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