diff --git a/.coveragerc b/.coveragerc index e10a23e9c31..703523ed364 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/ezviz/__init__.py b/homeassistant/components/ezviz/__init__.py index 9386a407acb..9aeba56360e 100644 --- a/homeassistant/components/ezviz/__init__.py +++ b/homeassistant/components/ezviz/__init__.py @@ -37,6 +37,7 @@ PLATFORMS_BY_TYPE: dict[str, list] = { Platform.CAMERA, Platform.LIGHT, Platform.NUMBER, + Platform.SELECT, Platform.SENSOR, Platform.SWITCH, Platform.UPDATE, diff --git a/homeassistant/components/ezviz/camera.py b/homeassistant/components/ezviz/camera.py index 6150a657c1a..01e8425c13b 100644 --- a/homeassistant/components/ezviz/camera.py +++ b/homeassistant/components/ezviz/camera.py @@ -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: diff --git a/homeassistant/components/ezviz/select.py b/homeassistant/components/ezviz/select.py new file mode 100644 index 00000000000..0f6a52ef578 --- /dev/null +++ b/homeassistant/components/ezviz/select.py @@ -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 diff --git a/homeassistant/components/ezviz/sensor.py b/homeassistant/components/ezviz/sensor.py index 11412c1fc70..075fe6bd6d1 100644 --- a/homeassistant/components/ezviz/sensor.py +++ b/homeassistant/components/ezviz/sensor.py @@ -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", diff --git a/homeassistant/components/ezviz/strings.json b/homeassistant/components/ezviz/strings.json index 5355fcc377c..aec1f892b1f 100644 --- a/homeassistant/components/ezviz/strings.json +++ b/homeassistant/components/ezviz/strings.json @@ -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": {