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.pull/93535/head
parent
1b5d207984
commit
f86cc34644
|
@ -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
|
||||
|
|
|
@ -35,6 +35,7 @@ PLATFORMS_BY_TYPE: dict[str, list] = {
|
|||
ATTR_TYPE_CLOUD: [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.CAMERA,
|
||||
Platform.NUMBER,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
Platform.UPDATE,
|
||||
|
|
|
@ -17,7 +17,11 @@ from homeassistant.config_entries import (
|
|||
)
|
||||
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 @@ class EzvizCamera(EzvizEntity, Camera):
|
|||
)
|
||||
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",
|
||||
)
|
||||
|
|
|
@ -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
|
|
@ -25,7 +25,6 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|||
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",
|
||||
|
|
|
@ -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."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue