mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Change Ezviz detection sensitivity to update per entity (#93995)
* Split detection sensitivity updates to entity instead of coordinator. * Detection Sensitivity entity individual poll. * Api return None instead of "unkown" when unkown. * Only add entity if camera supports * Cleanup detection type * Commit suggestions. --------- Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
parent
d431a692e5
commit
6af1beb6bf
@ -38,3 +38,32 @@ class EzvizEntity(CoordinatorEntity[EzvizDataUpdateCoordinator], Entity):
|
|||||||
def data(self) -> dict[str, Any]:
|
def data(self) -> dict[str, Any]:
|
||||||
"""Return coordinator data for this entity."""
|
"""Return coordinator data for this entity."""
|
||||||
return self.coordinator.data[self._serial]
|
return self.coordinator.data[self._serial]
|
||||||
|
|
||||||
|
|
||||||
|
class EzvizBaseEntity(Entity):
|
||||||
|
"""Generic entity for EZVIZ individual poll entities."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: EzvizDataUpdateCoordinator,
|
||||||
|
serial: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the entity."""
|
||||||
|
self._serial = serial
|
||||||
|
self.coordinator = coordinator
|
||||||
|
self._camera_name = self.data["name"]
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, serial)},
|
||||||
|
connections={
|
||||||
|
(CONNECTION_NETWORK_MAC, self.data["mac_address"]),
|
||||||
|
},
|
||||||
|
manufacturer=MANUFACTURER,
|
||||||
|
model=self.data["device_sub_category"],
|
||||||
|
name=self.data["name"],
|
||||||
|
sw_version=self.data["version"],
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self) -> dict[str, Any]:
|
||||||
|
"""Return coordinator data for this entity."""
|
||||||
|
return self.coordinator.data[self._serial]
|
||||||
|
@ -7,5 +7,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/ezviz",
|
"documentation": "https://www.home-assistant.io/integrations/ezviz",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["paho_mqtt", "pyezviz"],
|
"loggers": ["paho_mqtt", "pyezviz"],
|
||||||
"requirements": ["pyezviz==0.2.0.15"]
|
"requirements": ["pyezviz==0.2.0.17"]
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,18 @@
|
|||||||
"""Support for EZVIZ number controls."""
|
"""Support for EZVIZ number controls."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pyezviz.constants import DeviceCatagories
|
from dataclasses import dataclass
|
||||||
from pyezviz.exceptions import HTTPError, PyEzvizError
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from pyezviz.constants import SupportExt
|
||||||
|
from pyezviz.exceptions import (
|
||||||
|
EzvizAuthTokenExpired,
|
||||||
|
EzvizAuthVerificationCode,
|
||||||
|
HTTPError,
|
||||||
|
InvalidURL,
|
||||||
|
PyEzvizError,
|
||||||
|
)
|
||||||
|
|
||||||
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -13,17 +23,37 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN
|
from .const import DATA_COORDINATOR, DOMAIN
|
||||||
from .coordinator import EzvizDataUpdateCoordinator
|
from .coordinator import EzvizDataUpdateCoordinator
|
||||||
from .entity import EzvizEntity
|
from .entity import EzvizBaseEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
SCAN_INTERVAL = timedelta(seconds=3600)
|
||||||
|
PARALLEL_UPDATES = 0
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
NUMBER_TYPES = NumberEntityDescription(
|
|
||||||
|
@dataclass
|
||||||
|
class EzvizNumberEntityDescriptionMixin:
|
||||||
|
"""Mixin values for EZVIZ Number entities."""
|
||||||
|
|
||||||
|
supported_ext: str
|
||||||
|
supported_ext_value: list
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class EzvizNumberEntityDescription(
|
||||||
|
NumberEntityDescription, EzvizNumberEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Describe a EZVIZ Number."""
|
||||||
|
|
||||||
|
|
||||||
|
NUMBER_TYPE = EzvizNumberEntityDescription(
|
||||||
key="detection_sensibility",
|
key="detection_sensibility",
|
||||||
name="Detection sensitivity",
|
name="Detection sensitivity",
|
||||||
icon="mdi:eye",
|
icon="mdi:eye",
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
native_min_value=0,
|
native_min_value=0,
|
||||||
native_step=1,
|
native_step=1,
|
||||||
|
supported_ext=str(SupportExt.SupportSensibilityAdjust.value),
|
||||||
|
supported_ext_value=["1", "3"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -36,15 +66,18 @@ async def async_setup_entry(
|
|||||||
]
|
]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
EzvizSensor(coordinator, camera, sensor, NUMBER_TYPES)
|
[
|
||||||
for camera in coordinator.data
|
EzvizSensor(coordinator, camera, value, entry.entry_id)
|
||||||
for sensor, value in coordinator.data[camera].items()
|
for camera in coordinator.data
|
||||||
if sensor in NUMBER_TYPES.key
|
for capibility, value in coordinator.data[camera]["supportExt"].items()
|
||||||
if value
|
if capibility == NUMBER_TYPE.supported_ext
|
||||||
|
if value in NUMBER_TYPE.supported_ext_value
|
||||||
|
],
|
||||||
|
update_before_add=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EzvizSensor(EzvizEntity, NumberEntity):
|
class EzvizSensor(EzvizBaseEntity, NumberEntity):
|
||||||
"""Representation of a EZVIZ number entity."""
|
"""Representation of a EZVIZ number entity."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
@ -53,46 +86,57 @@ class EzvizSensor(EzvizEntity, NumberEntity):
|
|||||||
self,
|
self,
|
||||||
coordinator: EzvizDataUpdateCoordinator,
|
coordinator: EzvizDataUpdateCoordinator,
|
||||||
serial: str,
|
serial: str,
|
||||||
sensor: str,
|
value: str,
|
||||||
description: NumberEntityDescription,
|
config_entry_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator, serial)
|
super().__init__(coordinator, serial)
|
||||||
self._sensor_name = sensor
|
self.sensitivity_type = 3 if value == "3" else 0
|
||||||
self.battery_cam_type = bool(
|
self._attr_native_max_value = 100 if value == "3" else 6
|
||||||
self.data["device_category"]
|
self._attr_unique_id = f"{serial}_{NUMBER_TYPE.key}"
|
||||||
== DeviceCatagories.BATTERY_CAMERA_DEVICE_CATEGORY.value
|
self.entity_description = NUMBER_TYPE
|
||||||
)
|
self.config_entry_id = config_entry_id
|
||||||
self._attr_unique_id = f"{serial}_{sensor}"
|
self.sensor_value: int | None = None
|
||||||
self._attr_native_max_value = 100 if self.battery_cam_type else 6
|
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> float | None:
|
||||||
"""Return the state of the entity."""
|
"""Return the state of the entity."""
|
||||||
try:
|
if self.sensor_value is not None:
|
||||||
return float(self.data[self._sensor_name])
|
return float(self.sensor_value)
|
||||||
except ValueError:
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
def set_native_value(self, value: float) -> None:
|
def set_native_value(self, value: float) -> None:
|
||||||
"""Set camera detection sensitivity."""
|
"""Set camera detection sensitivity."""
|
||||||
level = int(value)
|
level = int(value)
|
||||||
try:
|
try:
|
||||||
if self.battery_cam_type:
|
self.coordinator.ezviz_client.detection_sensibility(
|
||||||
self.coordinator.ezviz_client.detection_sensibility(
|
self._serial,
|
||||||
self._serial,
|
level,
|
||||||
level,
|
self.sensitivity_type,
|
||||||
3,
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
self.coordinator.ezviz_client.detection_sensibility(
|
|
||||||
self._serial,
|
|
||||||
level,
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
|
|
||||||
except (HTTPError, PyEzvizError) as err:
|
except (HTTPError, PyEzvizError) as err:
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
f"Cannot set detection sensitivity level on {self.name}"
|
f"Cannot set detection sensitivity level on {self.name}"
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
|
self.sensor_value = level
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
"""Fetch data from EZVIZ."""
|
||||||
|
_LOGGER.debug("Updating %s", self.name)
|
||||||
|
try:
|
||||||
|
self.sensor_value = self.coordinator.ezviz_client.get_detection_sensibility(
|
||||||
|
self._serial,
|
||||||
|
str(self.sensitivity_type),
|
||||||
|
)
|
||||||
|
|
||||||
|
except (EzvizAuthTokenExpired, EzvizAuthVerificationCode):
|
||||||
|
_LOGGER.debug("Failed to login to EZVIZ API")
|
||||||
|
self.hass.async_create_task(
|
||||||
|
self.hass.config_entries.async_reload(self.config_entry_id)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
except (InvalidURL, HTTPError, PyEzvizError) as error:
|
||||||
|
raise HomeAssistantError(f"Invalid response from API: {error}") from error
|
||||||
|
@ -1664,7 +1664,7 @@ pyeverlights==0.1.0
|
|||||||
pyevilgenius==2.0.0
|
pyevilgenius==2.0.0
|
||||||
|
|
||||||
# homeassistant.components.ezviz
|
# homeassistant.components.ezviz
|
||||||
pyezviz==0.2.0.15
|
pyezviz==0.2.0.17
|
||||||
|
|
||||||
# homeassistant.components.fibaro
|
# homeassistant.components.fibaro
|
||||||
pyfibaro==0.7.1
|
pyfibaro==0.7.1
|
||||||
|
@ -1222,7 +1222,7 @@ pyeverlights==0.1.0
|
|||||||
pyevilgenius==2.0.0
|
pyevilgenius==2.0.0
|
||||||
|
|
||||||
# homeassistant.components.ezviz
|
# homeassistant.components.ezviz
|
||||||
pyezviz==0.2.0.15
|
pyezviz==0.2.0.17
|
||||||
|
|
||||||
# homeassistant.components.fibaro
|
# homeassistant.components.fibaro
|
||||||
pyfibaro==0.7.1
|
pyfibaro==0.7.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user