Move Verisure services to entity services (#47905)

This commit is contained in:
Franck Nijhof 2021-03-15 22:50:28 +01:00 committed by GitHub
parent 40c12997ed
commit 9fd973d8e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 62 deletions

View file

@ -5,7 +5,6 @@ import asyncio
import os
from typing import Any
from verisure import Error as VerisureError
import voluptuous as vol
from homeassistant.components.alarm_control_panel import (
@ -29,7 +28,6 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.storage import STORAGE_DIR
from .const import (
ATTR_DEVICE_SERIAL,
CONF_CODE_DIGITS,
CONF_DEFAULT_LOCK_CODE,
CONF_GIID,
@ -38,9 +36,6 @@ from .const import (
DEFAULT_LOCK_CODE_DIGITS,
DOMAIN,
LOGGER,
SERVICE_CAPTURE_SMARTCAM,
SERVICE_DISABLE_AUTOLOCK,
SERVICE_ENABLE_AUTOLOCK,
)
from .coordinator import VerisureDataUpdateCoordinator
@ -73,9 +68,6 @@ CONFIG_SCHEMA = vol.Schema(
)
DEVICE_SERIAL_SCHEMA = vol.Schema({vol.Required(ATTR_DEVICE_SERIAL): cv.string})
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
"""Set up the Verisure integration."""
if DOMAIN in config:
@ -151,44 +143,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.config_entries.async_forward_entry_setup(entry, platform)
)
async def capture_smartcam(service):
"""Capture a new picture from a smartcam."""
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(coordinator.smartcam_capture, device_id)
LOGGER.debug("Capturing new image from %s", ATTR_DEVICE_SERIAL)
except VerisureError as ex:
LOGGER.error("Could not capture image, %s", ex)
hass.services.async_register(
DOMAIN, SERVICE_CAPTURE_SMARTCAM, capture_smartcam, schema=DEVICE_SERIAL_SCHEMA
)
async def disable_autolock(service):
"""Disable autolock on a doorlock."""
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(coordinator.disable_autolock, device_id)
LOGGER.debug("Disabling autolock on%s", ATTR_DEVICE_SERIAL)
except VerisureError as ex:
LOGGER.error("Could not disable autolock, %s", ex)
hass.services.async_register(
DOMAIN, SERVICE_DISABLE_AUTOLOCK, disable_autolock, schema=DEVICE_SERIAL_SCHEMA
)
async def enable_autolock(service):
"""Enable autolock on a doorlock."""
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(coordinator.enable_autolock, device_id)
LOGGER.debug("Enabling autolock on %s", ATTR_DEVICE_SERIAL)
except VerisureError as ex:
LOGGER.error("Could not enable autolock, %s", ex)
hass.services.async_register(
DOMAIN, SERVICE_ENABLE_AUTOLOCK, enable_autolock, schema=DEVICE_SERIAL_SCHEMA
)
return True

View file

@ -5,14 +5,17 @@ import errno
import os
from typing import Callable, Iterable
from verisure import Error as VerisureError
from homeassistant.components.camera import Camera
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import current_platform
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, LOGGER
from .const import DOMAIN, LOGGER, SERVICE_CAPTURE_SMARTCAM
from .coordinator import VerisureDataUpdateCoordinator
@ -24,6 +27,13 @@ async def async_setup_entry(
"""Set up Verisure sensors based on a config entry."""
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
platform = current_platform.get()
platform.async_register_entity_service(
SERVICE_CAPTURE_SMARTCAM,
{},
VerisureSmartcam.capture_smartcam.__name__,
)
assert hass.config.config_dir
async_add_entities(
VerisureSmartcam(hass, coordinator, serial_number, hass.config.config_dir)
@ -114,3 +124,11 @@ class VerisureSmartcam(CoordinatorEntity, Camera):
def unique_id(self) -> str:
"""Return the unique ID for this camera."""
return self.serial_number
def capture_smartcam(self) -> None:
"""Capture a new picture from a smartcam."""
try:
self.coordinator.smartcam_capture(self.serial_number)
LOGGER.debug("Capturing new image from %s", self.serial_number)
except VerisureError as ex:
LOGGER.error("Could not capture image, %s", ex)

View file

@ -6,8 +6,6 @@ DOMAIN = "verisure"
LOGGER = logging.getLogger(__package__)
ATTR_DEVICE_SERIAL = "device_serial"
CONF_GIID = "giid"
CONF_LOCK_CODE_DIGITS = "lock_code_digits"
CONF_LOCK_DEFAULT_CODE = "lock_default_code"

View file

@ -112,11 +112,3 @@ class VerisureDataUpdateCoordinator(DataUpdateCoordinator):
def smartcam_capture(self, device_id: str) -> None:
"""Capture a new image from a smartcam."""
self.verisure.capture_image(device_id)
def disable_autolock(self, device_id: str) -> None:
"""Disable autolock."""
self.verisure.set_lock_config(device_id, auto_lock_enabled=False)
def enable_autolock(self, device_id: str) -> None:
"""Enable autolock."""
self.verisure.set_lock_config(device_id, auto_lock_enabled=True)

View file

@ -4,11 +4,14 @@ from __future__ import annotations
import asyncio
from typing import Callable, Iterable
from verisure import Error as VerisureError
from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CODE, STATE_LOCKED, STATE_UNLOCKED
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import current_platform
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
@ -17,6 +20,8 @@ from .const import (
DEFAULT_LOCK_CODE_DIGITS,
DOMAIN,
LOGGER,
SERVICE_DISABLE_AUTOLOCK,
SERVICE_ENABLE_AUTOLOCK,
)
from .coordinator import VerisureDataUpdateCoordinator
@ -28,6 +33,19 @@ async def async_setup_entry(
) -> None:
"""Set up Verisure alarm control panel from a config entry."""
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
platform = current_platform.get()
platform.async_register_entity_service(
SERVICE_DISABLE_AUTOLOCK,
{},
VerisureDoorlock.disable_autolock.__name__,
)
platform.async_register_entity_service(
SERVICE_ENABLE_AUTOLOCK,
{},
VerisureDoorlock.enable_autolock.__name__,
)
async_add_entities(
VerisureDoorlock(coordinator, serial_number)
for serial_number in coordinator.data["locks"]
@ -127,3 +145,23 @@ class VerisureDoorlock(CoordinatorEntity, LockEntity):
await asyncio.sleep(0.5)
if transaction["result"] == "OK":
self._state = state
def disable_autolock(self) -> None:
"""Disable autolock on a doorlock."""
try:
self.coordinator.verisure.set_lock_config(
self.serial_number, auto_lock_enabled=False
)
LOGGER.debug("Disabling autolock on %s", self.serial_number)
except VerisureError as ex:
LOGGER.error("Could not disable autolock, %s", ex)
def enable_autolock(self) -> None:
"""Enable autolock on a doorlock."""
try:
self.coordinator.verisure.set_lock_config(
self.serial_number, auto_lock_enabled=True
)
LOGGER.debug("Enabling autolock on %s", self.serial_number)
except VerisureError as ex:
LOGGER.error("Could not enable autolock, %s", ex)

View file

@ -1,6 +1,23 @@
capture_smartcam:
description: Capture a new image from a smartcam.
fields:
device_serial:
description: The serial number of the smartcam you want to capture an image from.
example: 2DEU AT5Z
name: Capture SmartCam image
description: Capture a new image from a Verisure SmartCam
target:
entity:
integration: verisure
domain: camera
enable_autolock:
name: Enable autolock
description: Enable autolock of a Verisure Lockguard Smartlock
target:
entity:
integration: verisure
domain: lock
disable_autolock:
name: Disable autolock
description: Disable autolock of a Verisure Lockguard Smartlock
target:
entity:
integration: verisure
domain: lock