From 9fd973d8e8c00adbd31d816cad85af5ec46add6a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 15 Mar 2021 22:50:28 +0100 Subject: [PATCH] Move Verisure services to entity services (#47905) --- homeassistant/components/verisure/__init__.py | 46 ------------------- homeassistant/components/verisure/camera.py | 20 +++++++- homeassistant/components/verisure/const.py | 2 - .../components/verisure/coordinator.py | 8 ---- homeassistant/components/verisure/lock.py | 38 +++++++++++++++ .../components/verisure/services.yaml | 27 +++++++++-- 6 files changed, 79 insertions(+), 62 deletions(-) diff --git a/homeassistant/components/verisure/__init__.py b/homeassistant/components/verisure/__init__.py index e34bf5c5650..5f8b2119310 100644 --- a/homeassistant/components/verisure/__init__.py +++ b/homeassistant/components/verisure/__init__.py @@ -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 diff --git a/homeassistant/components/verisure/camera.py b/homeassistant/components/verisure/camera.py index ee9fe6577de..776211b5cf3 100644 --- a/homeassistant/components/verisure/camera.py +++ b/homeassistant/components/verisure/camera.py @@ -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) diff --git a/homeassistant/components/verisure/const.py b/homeassistant/components/verisure/const.py index 3e00eb1ddb3..107d146e708 100644 --- a/homeassistant/components/verisure/const.py +++ b/homeassistant/components/verisure/const.py @@ -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" diff --git a/homeassistant/components/verisure/coordinator.py b/homeassistant/components/verisure/coordinator.py index 63eb5ac2f68..b118979f586 100644 --- a/homeassistant/components/verisure/coordinator.py +++ b/homeassistant/components/verisure/coordinator.py @@ -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) diff --git a/homeassistant/components/verisure/lock.py b/homeassistant/components/verisure/lock.py index 816243a454d..0431d0aee41 100644 --- a/homeassistant/components/verisure/lock.py +++ b/homeassistant/components/verisure/lock.py @@ -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) diff --git a/homeassistant/components/verisure/services.yaml b/homeassistant/components/verisure/services.yaml index 885b8597549..2a4e2a008be 100644 --- a/homeassistant/components/verisure/services.yaml +++ b/homeassistant/components/verisure/services.yaml @@ -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