Move Blink trigger_camera service to camera platform (#35635)

This commit is contained in:
Kevin Fronczak 2020-06-02 20:25:12 -04:00 committed by GitHub
parent 763ab79e6c
commit 94a9b364b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 20 deletions

View File

@ -25,12 +25,10 @@ from .const import (
SERVICE_REFRESH, SERVICE_REFRESH,
SERVICE_SAVE_VIDEO, SERVICE_SAVE_VIDEO,
SERVICE_SEND_PIN, SERVICE_SEND_PIN,
SERVICE_TRIGGER,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SERVICE_TRIGGER_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
SERVICE_SAVE_VIDEO_SCHEMA = vol.Schema( SERVICE_SAVE_VIDEO_SCHEMA = vol.Schema(
{vol.Required(CONF_NAME): cv.string, vol.Required(CONF_FILENAME): cv.string} {vol.Required(CONF_NAME): cv.string, vol.Required(CONF_FILENAME): cv.string}
) )
@ -106,14 +104,6 @@ async def async_setup_entry(hass, entry):
hass.config_entries.async_forward_entry_setup(entry, component) hass.config_entries.async_forward_entry_setup(entry, component)
) )
def trigger_camera(call):
"""Trigger a camera."""
cameras = hass.data[DOMAIN][entry.entry_id].cameras
name = call.data[CONF_NAME]
if name in cameras:
cameras[name].snap_picture()
blink_refresh()
def blink_refresh(event_time=None): def blink_refresh(event_time=None):
"""Call blink to refresh info.""" """Call blink to refresh info."""
hass.data[DOMAIN][entry.entry_id].refresh(force_cache=True) hass.data[DOMAIN][entry.entry_id].refresh(force_cache=True)
@ -130,9 +120,6 @@ async def async_setup_entry(hass, entry):
) )
hass.services.async_register(DOMAIN, SERVICE_REFRESH, blink_refresh) hass.services.async_register(DOMAIN, SERVICE_REFRESH, blink_refresh)
hass.services.async_register(
DOMAIN, SERVICE_TRIGGER, trigger_camera, schema=SERVICE_TRIGGER_SCHEMA
)
hass.services.async_register( hass.services.async_register(
DOMAIN, SERVICE_SAVE_VIDEO, async_save_video, schema=SERVICE_SAVE_VIDEO_SCHEMA DOMAIN, SERVICE_SAVE_VIDEO, async_save_video, schema=SERVICE_SAVE_VIDEO_SCHEMA
) )
@ -163,7 +150,6 @@ async def async_unload_entry(hass, entry):
return True return True
hass.services.async_remove(DOMAIN, SERVICE_REFRESH) hass.services.async_remove(DOMAIN, SERVICE_REFRESH)
hass.services.async_remove(DOMAIN, SERVICE_TRIGGER)
hass.services.async_remove(DOMAIN, SERVICE_SAVE_VIDEO_SCHEMA) hass.services.async_remove(DOMAIN, SERVICE_SAVE_VIDEO_SCHEMA)
hass.services.async_remove(DOMAIN, SERVICE_SEND_PIN) hass.services.async_remove(DOMAIN, SERVICE_SEND_PIN)

View File

@ -1,15 +1,22 @@
"""Support for Blink system camera.""" """Support for Blink system camera."""
import logging import logging
from homeassistant.components.camera import Camera import voluptuous as vol
from .const import DEFAULT_BRAND, DOMAIN from homeassistant.components.camera import Camera
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from .const import DEFAULT_BRAND, DOMAIN, SERVICE_TRIGGER
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_VIDEO_CLIP = "video" ATTR_VIDEO_CLIP = "video"
ATTR_IMAGE = "image" ATTR_IMAGE = "image"
SERVICE_TRIGGER_SCHEMA = vol.Schema({vol.Optional(ATTR_ENTITY_ID): cv.comp_entity_ids})
async def async_setup_entry(hass, config, async_add_entities): async def async_setup_entry(hass, config, async_add_entities):
"""Set up a Blink Camera.""" """Set up a Blink Camera."""
@ -20,6 +27,12 @@ async def async_setup_entry(hass, config, async_add_entities):
async_add_entities(entities) async_add_entities(entities)
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SERVICE_TRIGGER, SERVICE_TRIGGER_SCHEMA, "trigger_camera"
)
class BlinkCamera(Camera): class BlinkCamera(Camera):
"""An implementation of a Blink Camera.""" """An implementation of a Blink Camera."""
@ -69,6 +82,11 @@ class BlinkCamera(Camera):
"""Return the camera brand.""" """Return the camera brand."""
return DEFAULT_BRAND return DEFAULT_BRAND
def trigger_camera(self):
"""Trigger camera to take a snapshot."""
self._camera.snap_picture()
self.data.refresh()
def camera_image(self): def camera_image(self):
"""Return a still image response from the camera.""" """Return a still image response from the camera."""
return self._camera.image_from_cache.content return self._camera.image_from_cache.content

View File

@ -4,11 +4,11 @@ blink_update:
description: Force a refresh. description: Force a refresh.
trigger_camera: trigger_camera:
description: Request named camera to take new image. description: Request camera to take new image.
fields: fields:
name: entity_id:
description: Name of camera to take new image. description: Name(s) of camera entities to take new image.
example: "Living Room" example: "camera.living_room_camera"
save_video: save_video:
description: Save last recorded video clip to local file. description: Save last recorded video clip to local file.