Ezviz battery camera work mode (#130478)

* Add support for EzViz Battery Camera work mode

* feat: address review comment, add 'battery' to work mode string

* feat: optimize entity addition for Ezviz select component

* refactor: streamline error handling in Ezviz select actions

* Update library

* update library

* Bump api to pin mqtt to compatable version

* fix after rebase

* Update code owners

* codeowners

* Add support for EzViz Battery Camera work mode

* feat: address review comment, add 'battery' to work mode string

* feat: optimize entity addition for Ezviz select component

* refactor: streamline error handling in Ezviz select actions

* feat: address review item simplify Ezviz select actions by removing base class and moving methods

* chore: fix ruff lint

* feat: check for SupportExt before adding battery select

* chore: cleanup logging

* feat: restored battery work mode, separated defnitions for sound and battery selects, check SupportExt with type casting

* Apply suggestions from code review

---------

Co-authored-by: Pierre-Jean Buffard <pierre-jean.buffard@dataiku.com>
Co-authored-by: Renier Moorcroft <66512715+RenierM26@users.noreply.github.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Simone Rescio 2025-06-25 09:41:18 +02:00 committed by GitHub
parent f800248c10
commit f4b95ff5f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 113 additions and 23 deletions

View File

@ -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,13 +117,27 @@ 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):
"""Representation of a EZVIZ select entity."""
@ -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

View File

@ -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": {