Camera platform back-compat for custom components without CameraEntityFeature (#106529)

This commit is contained in:
J. Nick Koston 2023-12-28 13:45:35 -10:00 committed by GitHub
parent 552d4e49f0
commit 6506a8d511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -530,6 +530,19 @@ class Camera(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Flag supported features."""
return self._attr_supported_features
@property
def supported_features_compat(self) -> CameraEntityFeature:
"""Return the supported features as CameraEntityFeature.
Remove this compatibility shim in 2025.1 or later.
"""
features = self.supported_features
if type(features) is int: # noqa: E721
new_features = CameraEntityFeature(features)
self._report_deprecated_supported_features_values(new_features)
return new_features
return features
@cached_property
def is_recording(self) -> bool:
"""Return true if the device is recording."""
@ -570,7 +583,7 @@ class Camera(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""
if hasattr(self, "_attr_frontend_stream_type"):
return self._attr_frontend_stream_type
if CameraEntityFeature.STREAM not in self.supported_features:
if CameraEntityFeature.STREAM not in self.supported_features_compat:
return None
if self._rtsp_to_webrtc:
return StreamType.WEB_RTC
@ -758,7 +771,7 @@ class Camera(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
async def _async_use_rtsp_to_webrtc(self) -> bool:
"""Determine if a WebRTC provider can be used for the camera."""
if CameraEntityFeature.STREAM not in self.supported_features:
if CameraEntityFeature.STREAM not in self.supported_features_compat:
return False
if DATA_RTSP_TO_WEB_RTC not in self.hass.data:
return False

View File

@ -993,3 +993,23 @@ def test_deprecated_support_constants(
import_and_test_deprecated_constant_enum(
caplog, camera, entity_feature, "SUPPORT_", "2025.1"
)
def test_deprecated_supported_features_ints(caplog: pytest.LogCaptureFixture) -> None:
"""Test deprecated supported features ints."""
class MockCamera(camera.Camera):
@property
def supported_features(self) -> int:
"""Return supported features."""
return 1
entity = MockCamera()
assert entity.supported_features_compat is camera.CameraEntityFeature(1)
assert "MockCamera" in caplog.text
assert "is using deprecated supported features values" in caplog.text
assert "Instead it should use" in caplog.text
assert "CameraEntityFeature.ON_OFF" in caplog.text
caplog.clear()
assert entity.supported_features_compat is camera.CameraEntityFeature(1)
assert "is using deprecated supported features values" not in caplog.text