-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add number entity and remove old detection sensitivity service.
- Loading branch information
Showing
6 changed files
with
105 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters