diff --git a/homeassistant/components/ezviz/select.py b/homeassistant/components/ezviz/select.py index 44f80ad6cd1..24842f45b68 100644 --- a/homeassistant/components/ezviz/select.py +++ b/homeassistant/components/ezviz/select.py @@ -2,9 +2,17 @@ from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass +from typing import cast -from pyezvizapi.constants import DeviceSwitchType, SoundMode +from pyezvizapi.constants import ( + BatteryCameraWorkMode, + DeviceCatagories, + DeviceSwitchType, + SoundMode, + SupportExt, +) from pyezvizapi.exceptions import HTTPError, PyEzvizError from homeassistant.components.select import SelectEntity, SelectEntityDescription @@ -24,17 +32,83 @@ class EzvizSelectEntityDescription(SelectEntityDescription): """Describe a EZVIZ Select entity.""" supported_switch: int + current_option: Callable[[EzvizSelect], str | None] + select_option: Callable[[EzvizSelect, str, str], None] -SELECT_TYPE = EzvizSelectEntityDescription( +def alarm_sound_mode_current_option(ezvizSelect: EzvizSelect) -> str | None: + """Return the selected entity option to represent the entity state.""" + sound_mode_value = getattr( + SoundMode, ezvizSelect.data[ezvizSelect.entity_description.key] + ).value + if sound_mode_value in [0, 1, 2]: + return ezvizSelect.options[sound_mode_value] + + return None + + +def alarm_sound_mode_select_option( + ezvizSelect: EzvizSelect, serial: str, option: str +) -> None: + """Change the selected option.""" + sound_mode_value = ezvizSelect.options.index(option) + ezvizSelect.coordinator.ezviz_client.alarm_sound(serial, sound_mode_value, 1) + + +ALARM_SOUND_MODE_SELECT_TYPE = EzvizSelectEntityDescription( key="alarm_sound_mod", translation_key="alarm_sound_mode", entity_category=EntityCategory.CONFIG, options=["soft", "intensive", "silent"], supported_switch=DeviceSwitchType.ALARM_TONE.value, + current_option=alarm_sound_mode_current_option, + select_option=alarm_sound_mode_select_option, ) +def battery_work_mode_current_option(ezvizSelect: EzvizSelect) -> str | None: + """Return the selected entity option to represent the entity state.""" + battery_work_mode = getattr( + BatteryCameraWorkMode, + ezvizSelect.data[ezvizSelect.entity_description.key], + BatteryCameraWorkMode.UNKNOWN, + ) + if battery_work_mode == BatteryCameraWorkMode.UNKNOWN: + return None + + return battery_work_mode.name.lower() + + +def battery_work_mode_select_option( + ezvizSelect: EzvizSelect, serial: str, option: str +) -> None: + """Change the selected option.""" + battery_work_mode = getattr(BatteryCameraWorkMode, option.upper()) + ezvizSelect.coordinator.ezviz_client.set_battery_camera_work_mode( + serial, battery_work_mode.value + ) + + +BATTERY_WORK_MODE_SELECT_TYPE = EzvizSelectEntityDescription( + key="battery_camera_work_mode", + translation_key="battery_camera_work_mode", + icon="mdi:battery-sync", + entity_category=EntityCategory.CONFIG, + options=[ + "plugged_in", + "high_performance", + "power_save", + "super_power_save", + "custom", + ], + supported_switch=-1, + current_option=battery_work_mode_current_option, + select_option=battery_work_mode_select_option, +) + +SELECT_TYPES = [ALARM_SOUND_MODE_SELECT_TYPE, BATTERY_WORK_MODE_SELECT_TYPE] + + async def async_setup_entry( hass: HomeAssistant, entry: EzvizConfigEntry, @@ -43,12 +117,26 @@ async def async_setup_entry( """Set up EZVIZ select entities based on a config entry.""" coordinator = entry.runtime_data - async_add_entities( - EzvizSelect(coordinator, camera) + entities = [ + EzvizSelect(coordinator, camera, ALARM_SOUND_MODE_SELECT_TYPE) for camera in coordinator.data for switch in coordinator.data[camera]["switches"] - if switch == SELECT_TYPE.supported_switch - ) + if switch == ALARM_SOUND_MODE_SELECT_TYPE.supported_switch + ] + + for camera in coordinator.data: + device_category = coordinator.data[camera].get("device_category") + supportExt = coordinator.data[camera].get("supportExt") + if ( + device_category == DeviceCatagories.BATTERY_CAMERA_DEVICE_CATEGORY.value + and supportExt + and str(SupportExt.SupportBatteryManage.value) in supportExt + ): + entities.append( + EzvizSelect(coordinator, camera, BATTERY_WORK_MODE_SELECT_TYPE) + ) + + async_add_entities(entities) class EzvizSelect(EzvizEntity, SelectEntity): @@ -58,31 +146,23 @@ class EzvizSelect(EzvizEntity, SelectEntity): self, coordinator: EzvizDataUpdateCoordinator, serial: str, + description: EzvizSelectEntityDescription, ) -> None: - """Initialize the sensor.""" + """Initialize the select entity.""" super().__init__(coordinator, serial) - self._attr_unique_id = f"{serial}_{SELECT_TYPE.key}" - self.entity_description = SELECT_TYPE + self._attr_unique_id = f"{serial}_{description.key}" + self.entity_description = description @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 + desc = cast(EzvizSelectEntityDescription, self.entity_description) + return desc.current_option(self) def select_option(self, option: str) -> None: """Change the selected option.""" - sound_mode_value = self.options.index(option) - + desc = cast(EzvizSelectEntityDescription, self.entity_description) try: - self.coordinator.ezviz_client.alarm_sound(self._serial, sound_mode_value, 1) - + return desc.select_option(self, self._serial, option) except (HTTPError, PyEzvizError) as err: - raise HomeAssistantError( - f"Cannot set Warning sound level for {self.entity_id}" - ) from err + raise HomeAssistantError(f"Cannot select option for {desc.key}") from err diff --git a/homeassistant/components/ezviz/strings.json b/homeassistant/components/ezviz/strings.json index cd8bbc9d199..b03a5dbc61a 100644 --- a/homeassistant/components/ezviz/strings.json +++ b/homeassistant/components/ezviz/strings.json @@ -68,6 +68,16 @@ "intensive": "Intensive", "silent": "Silent" } + }, + "battery_camera_work_mode": { + "name": "Battery work mode", + "state": { + "plugged_in": "Plugged in", + "high_performance": "High performance", + "power_save": "Power save", + "super_power_save": "Super power saving", + "custom": "Custom" + } } }, "image": {