2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure cameras."""
|
2021-03-05 23:37:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-22 19:01:12 +00:00
|
|
|
import errno
|
2016-10-21 20:41:17 +00:00
|
|
|
import os
|
2021-03-11 18:41:01 +00:00
|
|
|
from typing import Any, Callable
|
2016-10-21 20:41:17 +00:00
|
|
|
|
|
|
|
from homeassistant.components.camera import Camera
|
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-11 18:41:01 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
from .const import CONF_SMARTCAM, DOMAIN, LOGGER
|
2021-03-14 09:38:09 +00:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2016-10-21 20:41:17 +00:00
|
|
|
|
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
2021-03-14 09:38:09 +00:00
|
|
|
add_entities: Callable[[list[VerisureSmartcam]], None],
|
2021-03-05 23:37:56 +00:00
|
|
|
discovery_info: dict[str, Any] | None = None,
|
2021-03-11 18:41:01 +00:00
|
|
|
) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Verisure Camera."""
|
2021-03-14 09:38:09 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN]
|
2021-03-11 18:41:01 +00:00
|
|
|
if not int(coordinator.config.get(CONF_SMARTCAM, 1)):
|
|
|
|
return
|
2021-03-05 23:37:56 +00:00
|
|
|
|
2021-03-14 09:38:09 +00:00
|
|
|
assert hass.config.config_dir
|
2021-03-11 18:41:01 +00:00
|
|
|
add_entities(
|
|
|
|
[
|
2021-03-14 09:38:09 +00:00
|
|
|
VerisureSmartcam(hass, coordinator, serial_number, hass.config.config_dir)
|
|
|
|
for serial_number in coordinator.data["cameras"]
|
2021-03-11 18:41:01 +00:00
|
|
|
]
|
|
|
|
)
|
2020-12-30 08:55:18 +00:00
|
|
|
|
2016-10-21 20:41:17 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureSmartcam(CoordinatorEntity, Camera):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a Verisure camera."""
|
2016-10-21 20:41:17 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator = VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
coordinator: VerisureDataUpdateCoordinator,
|
2021-03-14 09:38:09 +00:00
|
|
|
serial_number: str,
|
2021-03-11 18:41:01 +00:00
|
|
|
directory_path: str,
|
|
|
|
):
|
2016-10-21 20:41:17 +00:00
|
|
|
"""Initialize Verisure File Camera component."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2016-10-21 20:41:17 +00:00
|
|
|
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number = serial_number
|
2016-10-21 20:41:17 +00:00
|
|
|
self._directory_path = directory_path
|
|
|
|
self._image = None
|
|
|
|
self._image_id = None
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.delete_image)
|
2016-10-21 20:41:17 +00:00
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def camera_image(self) -> bytes | None:
|
2016-10-21 20:41:17 +00:00
|
|
|
"""Return image response."""
|
|
|
|
self.check_imagelist()
|
|
|
|
if not self._image:
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("No image to display")
|
2016-10-21 20:41:17 +00:00
|
|
|
return
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("Trying to open %s", self._image)
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(self._image, "rb") as file:
|
2016-10-21 20:41:17 +00:00
|
|
|
return file.read()
|
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def check_imagelist(self) -> None:
|
2016-10-21 20:41:17 +00:00
|
|
|
"""Check the contents of the image list."""
|
2021-03-11 18:41:01 +00:00
|
|
|
self.coordinator.update_smartcam_imageseries()
|
2021-03-14 09:38:09 +00:00
|
|
|
|
|
|
|
images = self.coordinator.imageseries.get("imageSeries", [])
|
|
|
|
new_image_id = None
|
|
|
|
for image in images:
|
|
|
|
if image["deviceLabel"] == self.serial_number:
|
|
|
|
new_image_id = image["image"][0]["imageId"]
|
|
|
|
break
|
|
|
|
|
|
|
|
if not new_image_id:
|
2016-10-21 20:41:17 +00:00
|
|
|
return
|
2021-03-14 09:38:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if new_image_id in ("-1", self._image_id):
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("The image is the same, or loading image_id")
|
2016-10-21 20:41:17 +00:00
|
|
|
return
|
2021-03-14 09:38:09 +00:00
|
|
|
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("Download new image %s", new_image_id)
|
2017-06-26 20:30:25 +00:00
|
|
|
new_image_path = os.path.join(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._directory_path, "{}{}".format(new_image_id, ".jpg")
|
|
|
|
)
|
2021-03-14 09:38:09 +00:00
|
|
|
self.coordinator.verisure.download_image(
|
|
|
|
self.serial_number, new_image_id, new_image_path
|
2021-03-11 18:41:01 +00:00
|
|
|
)
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("Old image_id=%s", self._image_id)
|
2021-03-05 23:37:56 +00:00
|
|
|
self.delete_image()
|
2016-10-21 20:41:17 +00:00
|
|
|
|
|
|
|
self._image_id = new_image_id
|
2017-06-26 20:30:25 +00:00
|
|
|
self._image = new_image_path
|
2016-10-21 20:41:17 +00:00
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def delete_image(self) -> None:
|
2016-10-21 20:41:17 +00:00
|
|
|
"""Delete an old image."""
|
2017-04-30 05:04:49 +00:00
|
|
|
remove_image = os.path.join(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._directory_path, "{}{}".format(self._image_id, ".jpg")
|
|
|
|
)
|
2016-10-22 19:01:12 +00:00
|
|
|
try:
|
|
|
|
os.remove(remove_image)
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("Deleting old image %s", remove_image)
|
2016-10-22 19:01:12 +00:00
|
|
|
except OSError as error:
|
|
|
|
if error.errno != errno.ENOENT:
|
|
|
|
raise
|
2016-10-21 20:41:17 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2016-10-21 20:41:17 +00:00
|
|
|
"""Return the name of this camera."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return self.coordinator.data["cameras"][self.serial_number]["area"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this camera."""
|
|
|
|
return self.serial_number
|