Camera entity
Camera entity 可以显示 images,还可以选项性地显示 video stream。从 homeassistant.components.camera.Camera 派生 platform entity。
属性
Properties 应该只从内存返回信息,而不要执行 I/O(如网络请求)。请实现 update() 或 async_update() 来获取数据。
状态
State 通过设置上述 properties 来定义。Resulting state 使用 CameraState enum 返回以下成员之一。
支持的功能
Supported features 通过使用 CameraEntityFeature enum 中的值来定义,
并使用按位或(|)运算符组合。
方法
摄像头图像
当传入 width 和 height 时,应在最佳基础上进行 scaling。如果 camera 无法进行 scaling,UI 将回退到在 display 层进行 scaling。
-
返回满足最小 width 和最小 height 的最小 image。
-
在对 image 进行 scaling 时,必须保留 aspect ratio。如果 aspect ratio 与请求的 height 或 width 不同,则返回 image 的 width 和/或 height 预期大于请求值。
-
如果底层 camera 能够对 image 进行 scaling,则传递 width 和 height。
-
如果集成无法对 image 进行 scaling 且返回 jpeg image,则在请求时它会自动由 camera 集成进行 scaling。
流源
Stream source 应返回一个 ffmpeg 可用的 URL(例如 RTSP URL)。需要 CameraEntityFeature.STREAM。
具有 stream source 的 camera entity 默认使用 StreamType.HLS 来告诉 frontend 使用带有 stream component 的 HLS feed。该 stream source 也会用于 stream 的 recording。
Camera entity 渲染 still image 的一种常见方式是将 stream source 传递给 ffmpeg component 中的 async_get_image。
WebRTC 流
WebRTC-enabled cameras 可以通过与 Home Assistant frontend 建立直接连接来使用。这种用法需要 CameraEntityFeature.STREAM,并且集成必须实现以下两个 methods 以支持原生 WebRTC:
async_handle_async_webrtc_offer:初始化 WebRTC stream。所有异步传入的消息/错误应通过send_messagecallback 转发到 frontend。async_on_webrtc_candidate:Frontend 会在发送 offer 后用所有传入的 candidate 调用它。 以下 method 可以选项性地实现:close_webrtc_session(可选):Frontend 会在 stream 关闭时调用它。可用于清理资源。
WebRTC streams 不使用 stream component,也不支持 recording。
通过实现 WebRTC methods,frontend 假设 camera 仅支持 WebRTC,因此不会回退到 HLS。
WebRTC 提供商
集成可以使用 homeassistant.components.camera.webrtc 中的库,从现有 camera 的 stream source 提供 WebRTC stream。集成可以实现 CameraWebRTCProvider 并通过 async_register_webrtc_provider 注册它。

