2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Verisure cameras."""
|
2021-03-06 00:37:56 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-22 21:01:12 +02:00
|
|
|
import errno
|
2016-10-21 22:41:17 +02:00
|
|
|
import os
|
|
|
|
|
2021-03-15 22:50:28 +01:00
|
|
|
from verisure import Error as VerisureError
|
|
|
|
|
2016-10-21 22:41:17 +02:00
|
|
|
from homeassistant.components.camera import Camera
|
2021-03-15 20:30:44 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2016-10-21 22:41:17 +02:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2021-03-06 00:37:56 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 22:36:48 +01:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import (
|
|
|
|
AddEntitiesCallback,
|
|
|
|
async_get_current_platform,
|
|
|
|
)
|
2021-03-11 19:41:01 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2021-03-15 23:59:41 +01:00
|
|
|
from .const import CONF_GIID, DOMAIN, LOGGER, SERVICE_CAPTURE_SMARTCAM
|
2021-03-14 10:38:09 +01:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2016-10-21 22:41:17 +02:00
|
|
|
|
|
|
|
|
2021-03-15 20:30:44 +01:00
|
|
|
async def async_setup_entry(
|
2021-03-06 00:37:56 +01:00
|
|
|
hass: HomeAssistant,
|
2021-03-15 20:30:44 +01:00
|
|
|
entry: ConfigEntry,
|
2021-05-04 22:36:48 +01:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-11 19:41:01 +01:00
|
|
|
) -> None:
|
2021-03-15 20:30:44 +01:00
|
|
|
"""Set up Verisure sensors based on a config entry."""
|
|
|
|
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-03-06 00:37:56 +01:00
|
|
|
|
2021-05-03 18:34:28 +02:00
|
|
|
platform = async_get_current_platform()
|
2021-03-15 22:50:28 +01:00
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_CAPTURE_SMARTCAM,
|
|
|
|
{},
|
|
|
|
VerisureSmartcam.capture_smartcam.__name__,
|
|
|
|
)
|
|
|
|
|
2021-03-14 10:38:09 +01:00
|
|
|
assert hass.config.config_dir
|
2021-03-15 20:30:44 +01:00
|
|
|
async_add_entities(
|
2021-04-05 03:22:25 -07:00
|
|
|
VerisureSmartcam(coordinator, serial_number, hass.config.config_dir)
|
2021-03-15 20:30:44 +01:00
|
|
|
for serial_number in coordinator.data["cameras"]
|
2021-03-11 19:41:01 +01:00
|
|
|
)
|
2020-12-30 09:55:18 +01:00
|
|
|
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2022-03-21 14:20:35 +01:00
|
|
|
class VerisureSmartcam(CoordinatorEntity[VerisureDataUpdateCoordinator], Camera):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Representation of a Verisure camera."""
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2022-07-11 18:14:17 +02:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: VerisureDataUpdateCoordinator,
|
2021-03-14 10:38:09 +01:00
|
|
|
serial_number: str,
|
2021-03-11 19:41:01 +01:00
|
|
|
directory_path: str,
|
2021-05-20 17:00:19 +02:00
|
|
|
) -> None:
|
2016-10-21 22:41:17 +02:00
|
|
|
"""Initialize Verisure File Camera component."""
|
2021-03-11 19:41:01 +01:00
|
|
|
super().__init__(coordinator)
|
2021-04-08 17:00:49 +02:00
|
|
|
Camera.__init__(self)
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2021-05-22 18:13:50 +02:00
|
|
|
self._attr_unique_id = serial_number
|
|
|
|
|
2021-03-14 10:38:09 +01:00
|
|
|
self.serial_number = serial_number
|
2016-10-21 22:41:17 +02:00
|
|
|
self._directory_path = directory_path
|
2022-01-19 22:56:31 +01:00
|
|
|
self._image: str | None = None
|
2023-03-26 19:32:25 +02:00
|
|
|
self._image_id: str | None = None
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2021-03-15 23:59:41 +01:00
|
|
|
@property
|
2021-05-02 00:37:19 +02:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-03-15 23:59:41 +01:00
|
|
|
"""Return device information about this entity."""
|
2023-03-26 19:32:25 +02:00
|
|
|
area = self.coordinator.data["cameras"][self.serial_number]["device"]["area"]
|
2021-10-15 00:35:09 +02:00
|
|
|
return DeviceInfo(
|
|
|
|
name=area,
|
|
|
|
suggested_area=area,
|
|
|
|
manufacturer="Verisure",
|
|
|
|
model="SmartCam",
|
|
|
|
identifiers={(DOMAIN, self.serial_number)},
|
|
|
|
via_device=(DOMAIN, self.coordinator.entry.data[CONF_GIID]),
|
|
|
|
configuration_url="https://mypages.verisure.com",
|
|
|
|
)
|
2021-03-15 23:59:41 +01:00
|
|
|
|
2021-08-10 19:33:06 -05:00
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2016-10-21 22:41:17 +02:00
|
|
|
"""Return image response."""
|
|
|
|
self.check_imagelist()
|
|
|
|
if not self._image:
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("No image to display")
|
2022-01-19 22:56:31 +01:00
|
|
|
return None
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("Trying to open %s", self._image)
|
2019-07-31 12:25:30 -07:00
|
|
|
with open(self._image, "rb") as file:
|
2016-10-21 22:41:17 +02:00
|
|
|
return file.read()
|
|
|
|
|
2021-03-06 00:37:56 +01:00
|
|
|
def check_imagelist(self) -> None:
|
2016-10-21 22:41:17 +02:00
|
|
|
"""Check the contents of the image list."""
|
2021-03-11 19:41:01 +01:00
|
|
|
self.coordinator.update_smartcam_imageseries()
|
2021-03-14 10:38:09 +01:00
|
|
|
|
2023-03-26 19:32:25 +02:00
|
|
|
new_image = None
|
|
|
|
for image in self.coordinator.imageseries:
|
2021-03-14 10:38:09 +01:00
|
|
|
if image["deviceLabel"] == self.serial_number:
|
2023-03-26 19:32:25 +02:00
|
|
|
new_image = image
|
2021-03-14 10:38:09 +01:00
|
|
|
break
|
|
|
|
|
2023-03-26 19:32:25 +02:00
|
|
|
if not new_image:
|
2016-10-21 22:41:17 +02:00
|
|
|
return
|
2021-03-14 10:38:09 +01:00
|
|
|
|
2023-03-26 19:32:25 +02:00
|
|
|
new_image_id = new_image["mediaId"]
|
2019-07-31 12:25:30 -07:00
|
|
|
if new_image_id in ("-1", self._image_id):
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("The image is the same, or loading image_id")
|
2016-10-21 22:41:17 +02:00
|
|
|
return
|
2021-03-14 10:38:09 +01:00
|
|
|
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("Download new image %s", new_image_id)
|
2017-06-26 22:30:25 +02:00
|
|
|
new_image_path = os.path.join(
|
2019-07-31 12:25:30 -07:00
|
|
|
self._directory_path, "{}{}".format(new_image_id, ".jpg")
|
|
|
|
)
|
2023-03-26 19:32:25 +02:00
|
|
|
new_image_url = new_image["contentUrl"]
|
|
|
|
self.coordinator.verisure.download_image(new_image_url, new_image_path)
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("Old image_id=%s", self._image_id)
|
2021-03-06 00:37:56 +01:00
|
|
|
self.delete_image()
|
2016-10-21 22:41:17 +02:00
|
|
|
|
|
|
|
self._image_id = new_image_id
|
2017-06-26 22:30:25 +02:00
|
|
|
self._image = new_image_path
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2021-04-05 03:22:25 -07:00
|
|
|
def delete_image(self, _=None) -> None:
|
2016-10-21 22:41:17 +02:00
|
|
|
"""Delete an old image."""
|
2017-04-30 07:04:49 +02:00
|
|
|
remove_image = os.path.join(
|
2019-07-31 12:25:30 -07:00
|
|
|
self._directory_path, "{}{}".format(self._image_id, ".jpg")
|
|
|
|
)
|
2016-10-22 21:01:12 +02:00
|
|
|
try:
|
|
|
|
os.remove(remove_image)
|
2020-12-30 09:55:18 +01:00
|
|
|
LOGGER.debug("Deleting old image %s", remove_image)
|
2016-10-22 21:01:12 +02:00
|
|
|
except OSError as error:
|
|
|
|
if error.errno != errno.ENOENT:
|
|
|
|
raise
|
2016-10-21 22:41:17 +02:00
|
|
|
|
2021-03-15 22:50:28 +01:00
|
|
|
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)
|
2021-04-05 03:22:25 -07:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Entity added to Home Assistant."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.delete_image)
|