2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-16 11:41:45 +00:00
|
|
|
from collections.abc import Mapping
|
2017-03-07 22:26:53 +00:00
|
|
|
import logging
|
2023-10-16 11:41:45 +00:00
|
|
|
from typing import Any
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2022-04-27 05:22:03 +00:00
|
|
|
from requests.exceptions import ChunkedEncodingError
|
2023-12-28 18:56:40 +00:00
|
|
|
import voluptuous as vol
|
2022-04-27 05:22:03 +00:00
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-12-28 18:56:40 +00:00
|
|
|
from homeassistant.const import CONF_FILE_PATH, CONF_FILENAME
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-12-28 18:56:40 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
2020-06-03 00:25:12 +00:00
|
|
|
from homeassistant.helpers import entity_platform
|
2023-12-28 18:56:40 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-10-23 13:34:28 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2023-12-28 18:56:40 +00:00
|
|
|
from .const import (
|
|
|
|
DEFAULT_BRAND,
|
|
|
|
DOMAIN,
|
2024-05-13 02:35:01 +00:00
|
|
|
SERVICE_RECORD,
|
2023-12-28 18:56:40 +00:00
|
|
|
SERVICE_SAVE_RECENT_CLIPS,
|
|
|
|
SERVICE_SAVE_VIDEO,
|
|
|
|
SERVICE_TRIGGER,
|
|
|
|
)
|
2023-10-23 13:34:28 +00:00
|
|
|
from .coordinator import BlinkUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_VIDEO_CLIP = "video"
|
|
|
|
ATTR_IMAGE = "image"
|
2023-10-25 04:50:10 +00:00
|
|
|
PARALLEL_UPDATES = 1
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
|
2022-01-03 12:22:41 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a Blink Camera."""
|
2023-11-04 15:21:10 +00:00
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
coordinator: BlinkUpdateCoordinator = hass.data[DOMAIN][config.entry_id]
|
2021-05-03 12:57:11 +00:00
|
|
|
entities = [
|
2023-10-23 13:34:28 +00:00
|
|
|
BlinkCamera(coordinator, name, camera)
|
|
|
|
for name, camera in coordinator.api.cameras.items()
|
2021-05-03 12:57:11 +00:00
|
|
|
]
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2023-10-16 15:41:56 +00:00
|
|
|
async_add_entities(entities)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2021-05-03 16:34:28 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2024-05-13 02:35:01 +00:00
|
|
|
platform.async_register_entity_service(SERVICE_RECORD, {}, "record")
|
2021-05-03 12:57:11 +00:00
|
|
|
platform.async_register_entity_service(SERVICE_TRIGGER, {}, "trigger_camera")
|
2023-12-28 18:56:40 +00:00
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_SAVE_RECENT_CLIPS,
|
|
|
|
{vol.Required(CONF_FILE_PATH): cv.string},
|
|
|
|
"save_recent_clips",
|
|
|
|
)
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_SAVE_VIDEO,
|
|
|
|
{vol.Required(CONF_FILENAME): cv.string},
|
|
|
|
"save_video",
|
|
|
|
)
|
2020-06-03 00:25:12 +00:00
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
class BlinkCamera(CoordinatorEntity[BlinkUpdateCoordinator], Camera):
|
2017-03-07 22:26:53 +00:00
|
|
|
"""An implementation of a Blink Camera."""
|
|
|
|
|
2023-07-11 18:12:16 +00:00
|
|
|
_attr_has_entity_name = True
|
2023-06-26 16:29:33 +00:00
|
|
|
_attr_name = None
|
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
def __init__(self, coordinator: BlinkUpdateCoordinator, name, camera) -> None:
|
2017-03-07 22:26:53 +00:00
|
|
|
"""Initialize a camera."""
|
2023-10-23 13:34:28 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
Camera.__init__(self)
|
2018-10-03 02:17:14 +00:00
|
|
|
self._camera = camera
|
2021-07-18 21:21:12 +00:00
|
|
|
self._attr_unique_id = f"{camera.serial}-camera"
|
2021-12-21 11:06:08 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, camera.serial)},
|
2023-10-24 09:38:54 +00:00
|
|
|
serial_number=camera.serial,
|
2024-01-27 18:45:13 +00:00
|
|
|
sw_version=camera.version,
|
2021-12-21 11:06:08 +00:00
|
|
|
name=name,
|
|
|
|
manufacturer=DEFAULT_BRAND,
|
|
|
|
model=camera.camera_type,
|
|
|
|
)
|
2023-12-28 18:56:40 +00:00
|
|
|
_LOGGER.debug("Initialized blink camera %s", self._camera.name)
|
2018-10-17 06:38:03 +00:00
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
@property
|
2023-10-16 11:41:45 +00:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
2018-10-03 02:17:14 +00:00
|
|
|
"""Return the camera attributes."""
|
|
|
|
return self._camera.attributes
|
|
|
|
|
2023-10-16 11:41:45 +00:00
|
|
|
async def async_enable_motion_detection(self) -> None:
|
2018-10-03 02:17:14 +00:00
|
|
|
"""Enable motion detection for the camera."""
|
2023-10-16 11:41:45 +00:00
|
|
|
try:
|
|
|
|
await self._camera.async_arm(True)
|
2024-02-05 10:31:33 +00:00
|
|
|
except TimeoutError as er:
|
2024-04-21 19:19:48 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="failed_arm",
|
|
|
|
) from er
|
2023-10-23 13:34:28 +00:00
|
|
|
|
|
|
|
self._camera.motion_enabled = True
|
2023-11-04 15:21:10 +00:00
|
|
|
await self.coordinator.async_refresh()
|
2018-10-03 02:17:14 +00:00
|
|
|
|
2023-10-16 11:41:45 +00:00
|
|
|
async def async_disable_motion_detection(self) -> None:
|
2018-10-03 02:17:14 +00:00
|
|
|
"""Disable motion detection for the camera."""
|
2023-10-16 11:41:45 +00:00
|
|
|
try:
|
|
|
|
await self._camera.async_arm(False)
|
2024-02-05 10:31:33 +00:00
|
|
|
except TimeoutError as er:
|
2024-04-21 19:19:48 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="failed_disarm",
|
|
|
|
) from er
|
2023-10-23 13:34:28 +00:00
|
|
|
|
|
|
|
self._camera.motion_enabled = False
|
2023-11-04 15:21:10 +00:00
|
|
|
await self.coordinator.async_refresh()
|
2018-10-03 02:17:14 +00:00
|
|
|
|
|
|
|
@property
|
2022-08-19 07:54:13 +00:00
|
|
|
def motion_detection_enabled(self) -> bool:
|
2018-10-03 02:17:14 +00:00
|
|
|
"""Return the state of the camera."""
|
2021-10-27 18:24:55 +00:00
|
|
|
return self._camera.arm
|
2018-10-03 02:17:14 +00:00
|
|
|
|
|
|
|
@property
|
2023-10-16 11:41:45 +00:00
|
|
|
def brand(self) -> str | None:
|
2018-10-03 02:17:14 +00:00
|
|
|
"""Return the camera brand."""
|
|
|
|
return DEFAULT_BRAND
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2024-05-13 02:35:01 +00:00
|
|
|
async def record(self) -> None:
|
|
|
|
"""Trigger camera to record a clip."""
|
|
|
|
try:
|
|
|
|
await self._camera.record()
|
|
|
|
except TimeoutError as er:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="failed_clip",
|
|
|
|
) from er
|
|
|
|
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2023-10-16 11:41:45 +00:00
|
|
|
async def trigger_camera(self) -> None:
|
2020-06-03 00:25:12 +00:00
|
|
|
"""Trigger camera to take a snapshot."""
|
2024-04-21 19:19:48 +00:00
|
|
|
try:
|
2023-10-16 11:41:45 +00:00
|
|
|
await self._camera.snap_picture()
|
2024-04-21 19:19:48 +00:00
|
|
|
except TimeoutError as er:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="failed_snap",
|
|
|
|
) from er
|
|
|
|
|
2023-10-16 15:41:56 +00:00
|
|
|
self.async_write_ha_state()
|
2020-06-03 00:25:12 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2022-04-27 05:22:03 +00:00
|
|
|
try:
|
2023-10-16 11:41:45 +00:00
|
|
|
return self._camera.image_from_cache
|
2022-04-27 05:22:03 +00:00
|
|
|
except ChunkedEncodingError:
|
|
|
|
_LOGGER.debug("Could not retrieve image for %s", self._camera.name)
|
|
|
|
return None
|
|
|
|
except TypeError:
|
|
|
|
_LOGGER.debug("No cached image for %s", self._camera.name)
|
|
|
|
return None
|
2023-12-28 18:56:40 +00:00
|
|
|
|
|
|
|
async def save_recent_clips(self, file_path) -> None:
|
|
|
|
"""Save multiple recent clips to output directory."""
|
|
|
|
if not self.hass.config.is_allowed_path(file_path):
|
|
|
|
raise ServiceValidationError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="no_path",
|
|
|
|
translation_placeholders={"target": file_path},
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._camera.save_recent_clips(output_dir=file_path)
|
|
|
|
except OSError as err:
|
|
|
|
raise ServiceValidationError(
|
|
|
|
str(err),
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="cant_write",
|
|
|
|
) from err
|
|
|
|
|
|
|
|
async def save_video(self, filename) -> None:
|
|
|
|
"""Handle save video service calls."""
|
|
|
|
if not self.hass.config.is_allowed_path(filename):
|
|
|
|
raise ServiceValidationError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="no_path",
|
|
|
|
translation_placeholders={"target": filename},
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._camera.video_to_file(filename)
|
|
|
|
except OSError as err:
|
|
|
|
raise ServiceValidationError(
|
|
|
|
str(err),
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="cant_write",
|
|
|
|
) from err
|