core/homeassistant/components/blink/camera.py

203 lines
6.6 KiB
Python
Raw Normal View History

"""Support for Blink system camera."""
from __future__ import annotations
from collections.abc import Mapping
import logging
from typing import Any
2022-04-27 05:22:03 +00:00
from requests.exceptions import ChunkedEncodingError
import voluptuous as vol
2022-04-27 05:22:03 +00:00
from homeassistant.components.camera import Camera
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_FILE_PATH, CONF_FILENAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2023-10-23 13:34:28 +00:00
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
DEFAULT_BRAND,
DOMAIN,
SERVICE_RECORD,
SERVICE_SAVE_RECENT_CLIPS,
SERVICE_SAVE_VIDEO,
SERVICE_TRIGGER,
)
2023-10-23 13:34:28 +00:00
from .coordinator import BlinkUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_VIDEO_CLIP = "video"
ATTR_IMAGE = "image"
PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up a Blink Camera."""
2023-10-23 13:34:28 +00:00
coordinator: BlinkUpdateCoordinator = hass.data[DOMAIN][config.entry_id]
entities = [
2023-10-23 13:34:28 +00:00
BlinkCamera(coordinator, name, camera)
for name, camera in coordinator.api.cameras.items()
]
async_add_entities(entities)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(SERVICE_RECORD, {}, "record")
platform.async_register_entity_service(SERVICE_TRIGGER, {}, "trigger_camera")
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",
)
2023-10-23 13:34:28 +00:00
class BlinkCamera(CoordinatorEntity[BlinkUpdateCoordinator], Camera):
"""An implementation of a Blink Camera."""
2023-07-11 18:12:16 +00:00
_attr_has_entity_name = True
_attr_name = None
2023-10-23 13:34:28 +00:00
def __init__(self, coordinator: BlinkUpdateCoordinator, name, camera) -> None:
"""Initialize a camera."""
2023-10-23 13:34:28 +00:00
super().__init__(coordinator)
Camera.__init__(self)
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
self._camera = camera
self._attr_unique_id = f"{camera.serial}-camera"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, camera.serial)},
2023-10-24 09:38:54 +00:00
serial_number=camera.serial,
sw_version=camera.version,
name=name,
manufacturer=DEFAULT_BRAND,
model=camera.camera_type,
)
_LOGGER.debug("Initialized blink camera %s", self._camera.name)
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
"""Return the camera attributes."""
return self._camera.attributes
async def async_enable_motion_detection(self) -> None:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
"""Enable motion detection for the camera."""
try:
await self._camera.async_arm(True)
except TimeoutError as er:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="failed_arm",
) from er
2023-10-23 13:34:28 +00:00
self._camera.motion_enabled = True
await self.coordinator.async_refresh()
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
async def async_disable_motion_detection(self) -> None:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
"""Disable motion detection for the camera."""
try:
await self._camera.async_arm(False)
except TimeoutError as er:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="failed_disarm",
) from er
2023-10-23 13:34:28 +00:00
self._camera.motion_enabled = False
await self.coordinator.async_refresh()
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
@property
2022-08-19 07:54:13 +00:00
def motion_detection_enabled(self) -> bool:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
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
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
@property
def brand(self) -> str | None:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
"""Return the camera brand."""
return DEFAULT_BRAND
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()
async def trigger_camera(self) -> None:
"""Trigger camera to take a snapshot."""
try:
await self._camera.snap_picture()
except TimeoutError as er:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="failed_snap",
) from er
self.async_write_ha_state()
def camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return a still image response from the camera."""
2022-04-27 05:22:03 +00:00
try:
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
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