2022-12-27 20:15:53 +00:00
|
|
|
"""This component provides support for Reolink IP cameras."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-01-01 22:32:17 +00:00
|
|
|
from . import ReolinkData
|
2022-12-27 20:15:53 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .entity import ReolinkCoordinatorEntity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
2023-01-01 22:32:17 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-12-27 20:15:53 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up a Reolink IP Camera."""
|
2023-01-01 22:32:17 +00:00
|
|
|
reolink_data: ReolinkData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
host = reolink_data.host
|
2022-12-27 20:15:53 +00:00
|
|
|
|
|
|
|
cameras = []
|
|
|
|
for channel in host.api.channels:
|
|
|
|
streams = ["sub", "main", "snapshots"]
|
2023-01-10 11:16:48 +00:00
|
|
|
if host.api.protocol in ["rtmp", "flv"]:
|
2022-12-27 20:15:53 +00:00
|
|
|
streams.append("ext")
|
|
|
|
|
|
|
|
for stream in streams:
|
2023-01-01 22:32:17 +00:00
|
|
|
cameras.append(ReolinkCamera(reolink_data, config_entry, channel, stream))
|
2022-12-27 20:15:53 +00:00
|
|
|
|
2023-01-01 22:32:17 +00:00
|
|
|
async_add_entities(cameras, update_before_add=True)
|
2022-12-27 20:15:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReolinkCamera(ReolinkCoordinatorEntity, Camera):
|
|
|
|
"""An implementation of a Reolink IP camera."""
|
|
|
|
|
|
|
|
_attr_supported_features: CameraEntityFeature = CameraEntityFeature.STREAM
|
2023-01-01 22:32:17 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reolink_data: ReolinkData,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
channel: int,
|
|
|
|
stream: str,
|
|
|
|
) -> None:
|
2022-12-27 20:15:53 +00:00
|
|
|
"""Initialize Reolink camera stream."""
|
2023-01-01 22:32:17 +00:00
|
|
|
ReolinkCoordinatorEntity.__init__(self, reolink_data, config_entry, channel)
|
2022-12-27 20:15:53 +00:00
|
|
|
Camera.__init__(self)
|
|
|
|
|
|
|
|
self._stream = stream
|
|
|
|
|
2023-01-01 22:32:17 +00:00
|
|
|
self._attr_name = self._stream
|
2022-12-27 20:15:53 +00:00
|
|
|
self._attr_unique_id = f"{self._host.unique_id}_{self._channel}_{self._stream}"
|
|
|
|
self._attr_entity_registry_enabled_default = stream == "sub"
|
|
|
|
|
|
|
|
async def stream_source(self) -> str | None:
|
|
|
|
"""Return the source of the stream."""
|
|
|
|
return await self._host.api.get_stream_source(self._channel, self._stream)
|
|
|
|
|
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
|
|
|
"""Return a still image response from the camera."""
|
|
|
|
return await self._host.api.get_snapshot(self._channel)
|