From f86cc34644ea97f823ffb666185a0efedca874e3 Mon Sep 17 00:00:00 2001 From: RenierM26 <66512715+RenierM26@users.noreply.github.com> Date: Thu, 25 May 2023 11:09:47 +0200 Subject: [PATCH] Add Ezviz number entity (#93366) * Initial commit * Add number entity. * update coveragerc * Add services back and add depreciation repair. * Remove redundant typing declaration. * Case change in strings. * Apply cleanups from simular pull request. * Commit suggestions. --- .coveragerc | 1 + homeassistant/components/ezviz/__init__.py | 1 + homeassistant/components/ezviz/camera.py | 16 +++- homeassistant/components/ezviz/number.py | 98 +++++++++++++++++++++ homeassistant/components/ezviz/sensor.py | 1 - homeassistant/components/ezviz/strings.json | 6 ++ 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/ezviz/number.py diff --git a/.coveragerc b/.coveragerc index 30df1694a25c9..928061407b8ce 100644 --- a/.coveragerc +++ b/.coveragerc @@ -327,6 +327,7 @@ omit = homeassistant/components/ezviz/binary_sensor.py homeassistant/components/ezviz/camera.py homeassistant/components/ezviz/coordinator.py + homeassistant/components/ezviz/number.py homeassistant/components/ezviz/entity.py homeassistant/components/ezviz/sensor.py homeassistant/components/ezviz/switch.py diff --git a/homeassistant/components/ezviz/__init__.py b/homeassistant/components/ezviz/__init__.py index 05857abbac7be..2966c339f9579 100644 --- a/homeassistant/components/ezviz/__init__.py +++ b/homeassistant/components/ezviz/__init__.py @@ -35,6 +35,7 @@ ATTR_TYPE_CLOUD: [ Platform.BINARY_SENSOR, Platform.CAMERA, + Platform.NUMBER, Platform.SENSOR, Platform.SWITCH, Platform.UPDATE, diff --git a/homeassistant/components/ezviz/camera.py b/homeassistant/components/ezviz/camera.py index 0456e7ade9e6d..57be995a4892c 100644 --- a/homeassistant/components/ezviz/camera.py +++ b/homeassistant/components/ezviz/camera.py @@ -17,7 +17,11 @@ ) 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 +from homeassistant.helpers import ( + config_validation as cv, + discovery_flow, + issue_registry as ir, +) from homeassistant.helpers.entity_platform import ( AddEntitiesCallback, async_get_current_platform, @@ -303,3 +307,13 @@ def perform_set_alarm_detection_sensibility( ) except (HTTPError, PyEzvizError) as err: raise PyEzvizError("Cannot set detection sensitivity level") from err + + ir.async_create_issue( + self.hass, + DOMAIN, + "service_depreciation_detection_sensibility", + breaks_in_ha_version="2023.8.0", + is_fixable=False, + severity=ir.IssueSeverity.WARNING, + translation_key="service_depreciation_detection_sensibility", + ) diff --git a/homeassistant/components/ezviz/number.py b/homeassistant/components/ezviz/number.py new file mode 100644 index 0000000000000..849bf2c400b6b --- /dev/null +++ b/homeassistant/components/ezviz/number.py @@ -0,0 +1,98 @@ +"""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.exceptions import HomeAssistantError +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 = 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, NUMBER_TYPES) + for camera in coordinator.data + for sensor, value in coordinator.data[camera].items() + if sensor in NUMBER_TYPES.key + if value + ) + + +class EzvizSensor(EzvizEntity, NumberEntity): + """Representation of a EZVIZ number entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: EzvizDataUpdateCoordinator, + serial: str, + sensor: str, + description: NumberEntityDescription, + ) -> 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 = description + + @property + def native_value(self) -> float | None: + """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 HomeAssistantError( + f"Cannot set detection sensitivity level on {self.name}" + ) from err diff --git a/homeassistant/components/ezviz/sensor.py b/homeassistant/components/ezviz/sensor.py index 8e617aa3b3eb1..11412c1fc70d0 100644 --- a/homeassistant/components/ezviz/sensor.py +++ b/homeassistant/components/ezviz/sensor.py @@ -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", diff --git a/homeassistant/components/ezviz/strings.json b/homeassistant/components/ezviz/strings.json index 5e258e4270574..6f00568cf2b6b 100644 --- a/homeassistant/components/ezviz/strings.json +++ b/homeassistant/components/ezviz/strings.json @@ -58,5 +58,11 @@ } } } + }, + "issues": { + "service_depreciation_detection_sensibility": { + "title": "Ezviz Detection sensitivity service is being removed", + "description": "Ezviz Detection sensitivity service is deprecated and will be removed in Home Assistant 2023.8; Please adjust the automation or script that uses the service and select submit below to mark this issue as resolved." + } } }