Skip to content

Commit

Permalink
Add Ezviz number entity (#93366)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
RenierM26 committed May 25, 2023
1 parent 1b5d207 commit f86cc34
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
Expand Down
16 changes: 15 additions & 1 deletion homeassistant/components/ezviz/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
)
98 changes: 98 additions & 0 deletions homeassistant/components/ezviz/number.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion homeassistant/components/ezviz/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
6 changes: 6 additions & 0 deletions homeassistant/components/ezviz/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}

0 comments on commit f86cc34

Please sign in to comment.