Add services to ezviz integration (#48984)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
515bd18ddd
commit
22b8dc16c2
10 changed files with 274 additions and 30 deletions
|
@ -1,29 +1,43 @@
|
|||
"""Support ezviz camera devices."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from haffmpeg.tools import IMAGE_JPEG, ImageFrame
|
||||
from pyezviz.exceptions import HTTPError, InvalidHost, PyEzvizError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.camera import PLATFORM_SCHEMA, SUPPORT_STREAM, Camera
|
||||
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
||||
from homeassistant.config_entries import SOURCE_DISCOVERY, SOURCE_IGNORE, SOURCE_IMPORT
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
ATTR_DIRECTION,
|
||||
ATTR_ENABLE,
|
||||
ATTR_LEVEL,
|
||||
ATTR_SERIAL,
|
||||
ATTR_SPEED,
|
||||
ATTR_TYPE,
|
||||
CONF_CAMERAS,
|
||||
CONF_FFMPEG_ARGUMENTS,
|
||||
DATA_COORDINATOR,
|
||||
DEFAULT_CAMERA_USERNAME,
|
||||
DEFAULT_FFMPEG_ARGUMENTS,
|
||||
DEFAULT_RTSP_PORT,
|
||||
DIR_DOWN,
|
||||
DIR_LEFT,
|
||||
DIR_RIGHT,
|
||||
DIR_UP,
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
SERVICE_ALARM_SOUND,
|
||||
SERVICE_ALARM_TRIGER,
|
||||
SERVICE_DETECTION_SENSITIVITY,
|
||||
SERVICE_PTZ,
|
||||
SERVICE_WAKE_DEVICE,
|
||||
)
|
||||
|
||||
CAMERA_SCHEMA = vol.Schema(
|
||||
|
@ -40,8 +54,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
MIN_TIME_BETWEEN_SESSION_RENEW = timedelta(seconds=90)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up a Ezviz IP Camera from platform config."""
|
||||
|
@ -157,6 +169,46 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
|
||||
async_add_entities(camera_entities)
|
||||
|
||||
platform = entity_platform.current_platform.get()
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_PTZ,
|
||||
{
|
||||
vol.Required(ATTR_DIRECTION): vol.In(
|
||||
[DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT]
|
||||
),
|
||||
vol.Required(ATTR_SPEED): cv.positive_int,
|
||||
},
|
||||
"perform_ptz",
|
||||
)
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_ALARM_TRIGER,
|
||||
{
|
||||
vol.Required(ATTR_ENABLE): cv.positive_int,
|
||||
},
|
||||
"perform_sound_alarm",
|
||||
)
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_WAKE_DEVICE, {}, "perform_wake_device"
|
||||
)
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_ALARM_SOUND,
|
||||
{vol.Required(ATTR_LEVEL): cv.positive_int},
|
||||
"perform_alarm_sound",
|
||||
)
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_DETECTION_SENSITIVITY,
|
||||
{
|
||||
vol.Required(ATTR_LEVEL): cv.positive_int,
|
||||
vol.Required(ATTR_TYPE): cv.positive_int,
|
||||
},
|
||||
"perform_set_alarm_detection_sensibility",
|
||||
)
|
||||
|
||||
|
||||
class EzvizCamera(CoordinatorEntity, Camera, RestoreEntity):
|
||||
"""An implementation of a Ezviz security camera."""
|
||||
|
@ -232,6 +284,24 @@ class EzvizCamera(CoordinatorEntity, Camera, RestoreEntity):
|
|||
"""Camera Motion Detection Status."""
|
||||
return self.coordinator.data[self._idx]["alarm_notify"]
|
||||
|
||||
def enable_motion_detection(self):
|
||||
"""Enable motion detection in camera."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.set_camera_defence(self._serial, 1)
|
||||
|
||||
except InvalidHost as err:
|
||||
_LOGGER.error("Error enabling motion detection")
|
||||
raise InvalidHost from err
|
||||
|
||||
def disable_motion_detection(self):
|
||||
"""Disable motion detection."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.set_camera_defence(self._serial, 0)
|
||||
|
||||
except InvalidHost as err:
|
||||
_LOGGER.error("Error disabling motion detection")
|
||||
raise InvalidHost from err
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the name of this camera."""
|
||||
|
@ -271,3 +341,52 @@ class EzvizCamera(CoordinatorEntity, Camera, RestoreEntity):
|
|||
self._rtsp_stream = rtsp_stream_source
|
||||
return rtsp_stream_source
|
||||
return None
|
||||
|
||||
def perform_ptz(self, direction, speed):
|
||||
"""Perform a PTZ action on the camera."""
|
||||
_LOGGER.debug("PTZ action '%s' on %s", direction, self._name)
|
||||
try:
|
||||
self.coordinator.ezviz_client.ptz_control(
|
||||
str(direction).upper(), self._serial, "START", speed
|
||||
)
|
||||
self.coordinator.ezviz_client.ptz_control(
|
||||
str(direction).upper(), self._serial, "STOP", speed
|
||||
)
|
||||
|
||||
except HTTPError as err:
|
||||
_LOGGER.error("Cannot perform PTZ")
|
||||
raise HTTPError from err
|
||||
|
||||
def perform_sound_alarm(self, enable):
|
||||
"""Sound the alarm on a camera."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.sound_alarm(self._serial, enable)
|
||||
except HTTPError as err:
|
||||
_LOGGER.debug("Cannot sound alarm")
|
||||
raise HTTPError from err
|
||||
|
||||
def perform_wake_device(self):
|
||||
"""Basically wakes the camera by querying the device."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.get_detection_sensibility(self._serial)
|
||||
except (HTTPError, PyEzvizError) as err:
|
||||
_LOGGER.error("Cannot wake device")
|
||||
raise PyEzvizError from err
|
||||
|
||||
def perform_alarm_sound(self, level):
|
||||
"""Enable/Disable movement sound alarm."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.alarm_sound(self._serial, level, 1)
|
||||
except HTTPError as err:
|
||||
_LOGGER.error("Cannot set alarm sound level for on movement detected")
|
||||
raise HTTPError from err
|
||||
|
||||
def perform_set_alarm_detection_sensibility(self, level, type_value):
|
||||
"""Set camera detection sensibility level service."""
|
||||
try:
|
||||
self.coordinator.ezviz_client.detection_sensibility(
|
||||
self._serial, level, type_value
|
||||
)
|
||||
except (HTTPError, PyEzvizError) as err:
|
||||
_LOGGER.error("Cannot set detection sensitivity level")
|
||||
raise PyEzvizError from err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue