Skip to content

Commit

Permalink
Add number entity and remove old detection sensitivity service.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed May 21, 2023
1 parent 40a3fe3 commit d0481e6
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 69 deletions.
1 change: 1 addition & 0 deletions custom_components/ezviz_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Platform.ALARM_CONTROL_PANEL,
Platform.BINARY_SENSOR,
Platform.CAMERA,
Platform.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
Expand Down
42 changes: 7 additions & 35 deletions custom_components/ezviz_cloud/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging

from pyezviz.exceptions import HTTPError, InvalidHost, PyEzvizError
import voluptuous as vol

from homeassistant.components import ffmpeg
Expand All @@ -16,20 +17,18 @@
)
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
config_validation as cv,
discovery_flow,
entity_platform,
from homeassistant.helpers import config_validation as cv, discovery_flow
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
async_get_current_platform,
)
from pyezviz.exceptions import HTTPError, InvalidHost, PyEzvizError

from .const import (
ATTR_DIRECTION,
ATTR_ENABLE,
ATTR_LEVEL,
ATTR_SERIAL,
ATTR_SPEED,
ATTR_TYPE,
CONF_FFMPEG_ARGUMENTS,
DATA_COORDINATOR,
DEFAULT_CAMERA_USERNAME,
Expand All @@ -41,7 +40,6 @@
DOMAIN,
SERVICE_ALARM_SOUND,
SERVICE_ALARM_TRIGGER,
SERVICE_DETECTION_SENSITIVITY,
SERVICE_PTZ,
SERVICE_WAKE_DEVICE,
)
Expand All @@ -52,9 +50,7 @@


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

Expand All @@ -71,15 +67,13 @@ async def async_setup_entry(
]

for camera, value in coordinator.data.items():

camera_rtsp_entry = [
item
for item in hass.config_entries.async_entries(DOMAIN)
if item.unique_id == camera and item.source != SOURCE_IGNORE
]

if camera_rtsp_entry:

ffmpeg_arguments = camera_rtsp_entry[0].options[CONF_FFMPEG_ARGUMENTS]
camera_username = camera_rtsp_entry[0].data[CONF_USERNAME]
camera_password = camera_rtsp_entry[0].data[CONF_PASSWORD]
Expand All @@ -94,7 +88,6 @@ async def async_setup_entry(
)

else:

discovery_flow.async_create_flow(
hass,
DOMAIN,
Expand All @@ -106,7 +99,6 @@ async def async_setup_entry(
)

if camera not in cameras_ignored:

_LOGGER.warning(
"Found camera with serial %s without configuration. Please go to integration to complete setup",
camera,
Expand All @@ -132,7 +124,7 @@ async def async_setup_entry(

async_add_entities(camera_entities)

platform = entity_platform.async_get_current_platform()
platform = async_get_current_platform()

platform.async_register_entity_service(
SERVICE_PTZ,
Expand Down Expand Up @@ -163,15 +155,6 @@ async def async_setup_entry(
"perform_alarm_sound",
)

platform.async_register_entity_service(
SERVICE_DETECTION_SENSITIVITY,
{
vol.Required(ATTR_LEVEL): cv.positive_int,
vol.Required(ATTR_TYPE): cv.positive_int,
},
"perform_set_alarm_detection_sensibility",
)


class EzvizCamera(EzvizEntity, Camera):
"""An implementation of a EZVIZ security camera."""
Expand Down Expand Up @@ -302,14 +285,3 @@ def perform_alarm_sound(self, level: int) -> None:
raise HTTPError(
"Cannot set alarm sound level for on movement detected"
) from err

def perform_set_alarm_detection_sensibility(
self, level: int, type_value: int
) -> None:
"""Set camera detection sensibility level service."""
try:
self.coordinator.ezviz_client.detection_sensibility(
self._serial, level, type_value
)
except (HTTPError, PyEzvizError) as err:
raise PyEzvizError("Cannot set detection sensitivity level") from err
2 changes: 1 addition & 1 deletion custom_components/ezviz_cloud/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "ezviz_cloud",
"name": "Ezviz(Beta)",
"version": "0.1.0.6",
"version": "0.1.0.7",
"documentation": "https://www.home-assistant.io/integrations/ezviz",
"dependencies": ["ffmpeg"],
"codeowners": ["@RenierM26", "@baqs"],
Expand Down
96 changes: 96 additions & 0 deletions custom_components/ezviz_cloud/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Support for EZVIZ number controls."""
from __future__ import annotations

from pyezviz.constants import DeviceCatagories
from pyezviz.exceptions import HTTPError, PyEzvizError

from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

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

PARALLEL_UPDATES = 1

NUMBER_TYPES: dict[str, NumberEntityDescription] = {
"detection_sensibility": NumberEntityDescription(
key="detection_sensibility",
name="Detection Sensitivity",
icon="mdi:eye",
entity_category=EntityCategory.CONFIG,
native_min_value=0,
native_step=1,
)
}


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EZVIZ sensors based on a config entry."""
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]

async_add_entities(
[
EzvizSensor(coordinator, camera, sensor)
for camera in coordinator.data
for sensor, value in coordinator.data[camera].items()
if sensor in NUMBER_TYPES
if value is not None
]
)


class EzvizSensor(EzvizEntity, NumberEntity):
"""Representation of a EZVIZ number entity."""

coordinator: EzvizDataUpdateCoordinator
_attr_has_entity_name = True

def __init__(
self, coordinator: EzvizDataUpdateCoordinator, serial: str, sensor: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, serial)
self._sensor_name = sensor
self.battery_cam_type = bool(
self.data["device_category"]
== DeviceCatagories.BATTERY_CAMERA_DEVICE_CATEGORY.value
)
self._attr_unique_id = f"{serial}_{sensor}"
self._attr_native_max_value = 100 if self.battery_cam_type else 6
self.entity_description = NUMBER_TYPES[sensor]

@property
def native_value(self) -> float:
"""Return the state of the entity."""
try:
return float(self.data[self._sensor_name])
except ValueError:
return None

def set_native_value(self, value: float) -> None:
"""Set camera detection sensitivity."""
level = int(value)
try:
if self.battery_cam_type:
self.coordinator.ezviz_client.detection_sensibility(
self._serial,
level,
3,
)
else:
self.coordinator.ezviz_client.detection_sensibility(
self._serial,
level,
0,
)

except (HTTPError, PyEzvizError) as err:
raise PyEzvizError("Cannot set detection sensitivity level") from err
1 change: 0 additions & 1 deletion custom_components/ezviz_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
device_class=SensorDeviceClass.BATTERY,
),
"alarm_sound_mod": SensorEntityDescription(key="alarm_sound_mod"),
"detection_sensibility": SensorEntityDescription(key="detection_sensibility"),
"last_alarm_time": SensorEntityDescription(key="last_alarm_time"),
"Seconds_Last_Trigger": SensorEntityDescription(
key="Seconds_Last_Trigger",
Expand Down
32 changes: 0 additions & 32 deletions custom_components/ezviz_cloud/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,6 @@ ptz:
max: 9
step: 1
mode: box
set_alarm_detection_sensibility:
name: Detection sensitivity
description: Sets the detection sensibility level.
target:
entity:
integration: ezviz_cloud
domain: camera
fields:
level:
name: Sensitivity Level
description: "Sensibility level (1-6) for type 0 (Normal camera)
or (1-100) for type 3 (PIR sensor camera)."
required: true
example: 3
default: 3
selector:
number:
min: 1
max: 100
step: 1
mode: box
type_value:
name: Detection type
description: "Type of detection. Options : 0 - Camera or 3 - PIR Sensor Camera"
required: true
example: "0"
default: "0"
selector:
select:
options:
- "0"
- "3"
sound_alarm:
name: Sound Alarm
description: Sounds the alarm on your camera.
Expand Down

0 comments on commit d0481e6

Please sign in to comment.