Add Ezviz select entity (#93625)

* Initial commit

* Add select entity

* coveragerc

* Cleanup

* Commit suggestions.

* Raise issue before try except

* Add translation key

* Update camera.py

* Update camera.py

* Disable old sensor by default instead of removing.

* Apply suggestions from code review

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* IR fix flow

* Fix conflict

* run black

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
pull/96693/head
RenierM26 2023-07-12 18:33:56 +02:00 committed by GitHub
parent 5792301cf1
commit 899adfa74c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 1 deletions

View File

@ -321,6 +321,7 @@ omit =
homeassistant/components/ezviz/coordinator.py
homeassistant/components/ezviz/number.py
homeassistant/components/ezviz/entity.py
homeassistant/components/ezviz/select.py
homeassistant/components/ezviz/sensor.py
homeassistant/components/ezviz/switch.py
homeassistant/components/ezviz/update.py

View File

@ -37,6 +37,7 @@ PLATFORMS_BY_TYPE: dict[str, list] = {
Platform.CAMERA,
Platform.LIGHT,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,

View File

@ -290,6 +290,16 @@ class EzvizCamera(EzvizEntity, Camera):
def perform_alarm_sound(self, level: int) -> None:
"""Enable/Disable movement sound alarm."""
ir.async_create_issue(
self.hass,
DOMAIN,
"service_deprecation_alarm_sound_level",
breaks_in_ha_version="2024.2.0",
is_fixable=True,
is_persistent=True,
severity=ir.IssueSeverity.WARNING,
translation_key="service_deprecation_alarm_sound_level",
)
try:
self.coordinator.ezviz_client.alarm_sound(self._serial, level, 1)
except HTTPError as err:

View File

@ -0,0 +1,99 @@
"""Support for EZVIZ select controls."""
from __future__ import annotations
from dataclasses import dataclass
from pyezviz.constants import DeviceSwitchType, SoundMode
from pyezviz.exceptions import HTTPError, PyEzvizError
from homeassistant.components.select import SelectEntity, SelectEntityDescription
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
@dataclass
class EzvizSelectEntityDescriptionMixin:
"""Mixin values for EZVIZ Select entities."""
supported_switch: int
@dataclass
class EzvizSelectEntityDescription(
SelectEntityDescription, EzvizSelectEntityDescriptionMixin
):
"""Describe a EZVIZ Select entity."""
SELECT_TYPE = EzvizSelectEntityDescription(
key="alarm_sound_mod",
translation_key="alarm_sound_mode",
icon="mdi:alarm",
entity_category=EntityCategory.CONFIG,
options=["soft", "intensive", "silent"],
supported_switch=DeviceSwitchType.ALARM_TONE.value,
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EZVIZ select entities based on a config entry."""
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
async_add_entities(
EzvizSensor(coordinator, camera)
for camera in coordinator.data
for switch in coordinator.data[camera]["switches"]
if switch == SELECT_TYPE.supported_switch
)
class EzvizSensor(EzvizEntity, SelectEntity):
"""Representation of a EZVIZ select entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: EzvizDataUpdateCoordinator,
serial: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, serial)
self._attr_unique_id = f"{serial}_{SELECT_TYPE.key}"
self.entity_description = SELECT_TYPE
@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
sound_mode_value = getattr(
SoundMode, self.data[self.entity_description.key]
).value
if sound_mode_value in [0, 1, 2]:
return self.options[sound_mode_value]
return None
def select_option(self, option: str) -> None:
"""Change the selected option."""
sound_mode_value = self.options.index(option)
try:
self.coordinator.ezviz_client.alarm_sound(self._serial, sound_mode_value, 1)
except (HTTPError, PyEzvizError) as err:
raise HomeAssistantError(
f"Cannot set Warning sound level for {self.entity_id}"
) from err

View File

@ -24,7 +24,10 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
),
"alarm_sound_mod": SensorEntityDescription(key="alarm_sound_mod"),
"alarm_sound_mod": SensorEntityDescription(
key="alarm_sound_mod",
entity_registry_enabled_default=False,
),
"last_alarm_time": SensorEntityDescription(key="last_alarm_time"),
"Seconds_Last_Trigger": SensorEntityDescription(
key="Seconds_Last_Trigger",

View File

@ -70,6 +70,29 @@
}
}
}
},
"service_deprecation_alarm_sound_level": {
"title": "Ezviz Alarm sound level service is being removed",
"fix_flow": {
"step": {
"confirm": {
"title": "[%key:component::ezviz::issues::service_deprecation_alarm_sound_level::title%]",
"description": "Ezviz Alarm sound level service is deprecated and will be removed in Home Assistant 2024.2.\nTo set the Alarm sound level, you can instead use the `select.select_option` service targetting the Warning sound entity.\n\nPlease remove the use of this service from your automations and scripts and select **submit** to close this issue."
}
}
}
}
},
"entity": {
"select": {
"alarm_sound_mode": {
"name": "Warning sound",
"state": {
"soft": "Soft",
"intensive": "Intensive",
"silent": "Silent"
}
}
}
},
"services": {