Skip to content

Commit

Permalink
Add Ezviz last motion picture image entity (#94421)
Browse files Browse the repository at this point in the history
* Initial commit

* Update camera.py

* ignore type mismatch on append.

* Use new image entity.

* coveragerc update

* Remove all changes to camera in this pull.

* Fix docstring.

* remove old "last_alarm_pic" sensor

* Update image entity

* bypass for content check error

* Fix last updated not sting object

* Remove redundant url change check.

* Remove debug string

* Check url change on coordinator data update.

* Add translation key for name.

* simplify update check

* Rebase EzvizLastMotion ImageEntity

* Change logging to debug.
  • Loading branch information
RenierM26 authored Jul 14, 2023
1 parent 0e8c85c commit 3e429ae
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ omit =
homeassistant/components/ezviz/__init__.py
homeassistant/components/ezviz/binary_sensor.py
homeassistant/components/ezviz/camera.py
homeassistant/components/ezviz/image.py
homeassistant/components/ezviz/light.py
homeassistant/components/ezviz/coordinator.py
homeassistant/components/ezviz/number.py
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/ezviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ATTR_TYPE_CLOUD: [
Platform.BINARY_SENSOR,
Platform.CAMERA,
Platform.IMAGE,
Platform.LIGHT,
Platform.NUMBER,
Platform.SELECT,
Expand Down
88 changes: 88 additions & 0 deletions homeassistant/components/ezviz/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Support EZVIZ last motion image."""
from __future__ import annotations

import logging

from homeassistant.components.image import Image, ImageEntity, ImageEntityDescription
from homeassistant.config_entries import (
ConfigEntry,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
)
from homeassistant.util import dt as dt_util

from .const import (
DATA_COORDINATOR,
DOMAIN,
)
from .coordinator import EzvizDataUpdateCoordinator
from .entity import EzvizEntity

_LOGGER = logging.getLogger(__name__)
GET_IMAGE_TIMEOUT = 10

IMAGE_TYPE = ImageEntityDescription(
key="last_motion_image",
translation_key="last_motion_image",
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EZVIZ image entities based on a config entry."""

coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]

async_add_entities(
EzvizLastMotion(hass, coordinator, camera) for camera in coordinator.data
)


class EzvizLastMotion(EzvizEntity, ImageEntity):
"""Return Last Motion Image from Ezviz Camera."""

_attr_has_entity_name = True

def __init__(
self, hass: HomeAssistant, coordinator: EzvizDataUpdateCoordinator, serial: str
) -> None:
"""Initialize a image entity."""
super().__init__(coordinator, serial)
ImageEntity.__init__(self, hass)
self._attr_unique_id = f"{serial}_{IMAGE_TYPE.key}"
self.entity_description = IMAGE_TYPE
self._attr_image_url = self.data["last_alarm_pic"]
self._attr_image_last_updated = dt_util.parse_datetime(
str(self.data["last_alarm_time"])
)

async def _async_load_image_from_url(self, url: str) -> Image | None:
"""Load an image by url."""
if response := await self._fetch_url(url):
return Image(
content=response.content,
content_type="image/jpeg", # Actually returns binary/octet-stream
)
return None

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if (
self.data["last_alarm_pic"]
and self.data["last_alarm_pic"] != self._attr_image_url
):
_LOGGER.debug("Image url changed to %s", self.data["last_alarm_pic"])

self._attr_image_url = self.data["last_alarm_pic"]
self._cached_image = None
self._attr_image_last_updated = dt_util.parse_datetime(
str(self.data["last_alarm_time"])
)

super()._handle_coordinator_update()
1 change: 0 additions & 1 deletion homeassistant/components/ezviz/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
key="Seconds_Last_Trigger",
entity_registry_enabled_default=False,
),
"last_alarm_pic": SensorEntityDescription(key="last_alarm_pic"),
"supported_channels": SensorEntityDescription(key="supported_channels"),
"local_ip": SensorEntityDescription(key="local_ip"),
"wan_ip": SensorEntityDescription(key="wan_ip"),
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/ezviz/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
"silent": "Silent"
}
}
},
"image": {
"last_motion_image": {
"name": "Last motion image"
}
}
},
"services": {
Expand Down

0 comments on commit 3e429ae

Please sign in to comment.