mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Refresh camera stream source of Synology DSM connected cameras (#70938)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
784fbf3291
commit
e300908a8e
@ -23,6 +23,7 @@ from homeassistant.const import CONF_MAC, CONF_SCAN_INTERVAL, CONF_VERIFY_SSL
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .common import SynoApi
|
from .common import SynoApi
|
||||||
@ -36,6 +37,7 @@ from .const import (
|
|||||||
EXCEPTION_DETAILS,
|
EXCEPTION_DETAILS,
|
||||||
EXCEPTION_UNKNOWN,
|
EXCEPTION_UNKNOWN,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
|
SIGNAL_CAMERA_SOURCE_CHANGED,
|
||||||
SYNO_API,
|
SYNO_API,
|
||||||
SYSTEM_LOADED,
|
SYSTEM_LOADED,
|
||||||
UNDO_UPDATE_LISTENER,
|
UNDO_UPDATE_LISTENER,
|
||||||
@ -123,6 +125,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
surveillance_station = api.surveillance_station
|
surveillance_station = api.surveillance_station
|
||||||
|
current_data: dict[str, SynoCamera] = {
|
||||||
|
camera.id: camera for camera in surveillance_station.get_all_cameras()
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(30):
|
async with async_timeout.timeout(30):
|
||||||
@ -130,12 +135,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except SynologyDSMAPIErrorException as err:
|
except SynologyDSMAPIErrorException as err:
|
||||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
||||||
|
|
||||||
return {
|
new_data: dict[str, SynoCamera] = {
|
||||||
"cameras": {
|
camera.id: camera for camera in surveillance_station.get_all_cameras()
|
||||||
camera.id: camera for camera in surveillance_station.get_all_cameras()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for cam_id, cam_data_new in new_data.items():
|
||||||
|
if (
|
||||||
|
(cam_data_current := current_data.get(cam_id)) is not None
|
||||||
|
and cam_data_current.live_view.rtsp != cam_data_new.live_view.rtsp
|
||||||
|
):
|
||||||
|
async_dispatcher_send(
|
||||||
|
hass,
|
||||||
|
f"{SIGNAL_CAMERA_SOURCE_CHANGED}_{entry.entry_id}_{cam_id}",
|
||||||
|
cam_data_new.live_view.rtsp,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"cameras": new_data}
|
||||||
|
|
||||||
async def async_coordinator_update_data_central() -> None:
|
async def async_coordinator_update_data_central() -> None:
|
||||||
"""Fetch all device and sensor data from api."""
|
"""Fetch all device and sensor data from api."""
|
||||||
try:
|
try:
|
||||||
|
@ -16,7 +16,8 @@ from homeassistant.components.camera import (
|
|||||||
CameraEntityFeature,
|
CameraEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
@ -27,6 +28,7 @@ from .const import (
|
|||||||
COORDINATOR_CAMERAS,
|
COORDINATOR_CAMERAS,
|
||||||
DEFAULT_SNAPSHOT_QUALITY,
|
DEFAULT_SNAPSHOT_QUALITY,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
SIGNAL_CAMERA_SOURCE_CHANGED,
|
||||||
SYNO_API,
|
SYNO_API,
|
||||||
)
|
)
|
||||||
from .entity import SynologyDSMBaseEntity, SynologyDSMEntityDescription
|
from .entity import SynologyDSMBaseEntity, SynologyDSMEntityDescription
|
||||||
@ -130,6 +132,29 @@ class SynoDSMCamera(SynologyDSMBaseEntity, Camera):
|
|||||||
"""Return the camera motion detection status."""
|
"""Return the camera motion detection status."""
|
||||||
return self.camera_data.is_motion_detection_enabled # type: ignore[no-any-return]
|
return self.camera_data.is_motion_detection_enabled # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
def _listen_source_updates(self) -> None:
|
||||||
|
"""Listen for camera source changed events."""
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_signal(url: str) -> None:
|
||||||
|
if self.stream:
|
||||||
|
_LOGGER.debug("Update stream URL for camera %s", self.camera_data.name)
|
||||||
|
self.stream.update_source(url)
|
||||||
|
|
||||||
|
assert self.platform
|
||||||
|
assert self.platform.config_entry
|
||||||
|
self.async_on_remove(
|
||||||
|
async_dispatcher_connect(
|
||||||
|
self.hass,
|
||||||
|
f"{SIGNAL_CAMERA_SOURCE_CHANGED}_{self.platform.config_entry.entry_id}_{self.camera_data.id}",
|
||||||
|
_handle_signal,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Subscribe to signal."""
|
||||||
|
self._listen_source_updates()
|
||||||
|
|
||||||
def camera_image(
|
def camera_image(
|
||||||
self, width: int | None = None, height: int | None = None
|
self, width: int | None = None, height: int | None = None
|
||||||
) -> bytes | None:
|
) -> bytes | None:
|
||||||
@ -162,6 +187,7 @@ class SynoDSMCamera(SynologyDSMBaseEntity, Camera):
|
|||||||
)
|
)
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return self.camera_data.live_view.rtsp # type: ignore[no-any-return]
|
return self.camera_data.live_view.rtsp # type: ignore[no-any-return]
|
||||||
|
|
||||||
def enable_motion_detection(self) -> None:
|
def enable_motion_detection(self) -> None:
|
||||||
|
@ -43,6 +43,9 @@ DEFAULT_SNAPSHOT_QUALITY = SNAPSHOT_PROFILE_BALANCED
|
|||||||
|
|
||||||
ENTITY_UNIT_LOAD = "load"
|
ENTITY_UNIT_LOAD = "load"
|
||||||
|
|
||||||
|
# Signals
|
||||||
|
SIGNAL_CAMERA_SOURCE_CHANGED = "synology_dsm.camera_stream_source_changed"
|
||||||
|
|
||||||
# Services
|
# Services
|
||||||
SERVICE_REBOOT = "reboot"
|
SERVICE_REBOOT = "reboot"
|
||||||
SERVICE_SHUTDOWN = "shutdown"
|
SERVICE_SHUTDOWN = "shutdown"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user