mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 05:47:10 +00:00
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:
parent
f800248c10
commit
f4b95ff5f1
@ -2,9 +2,17 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
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 pyezvizapi.exceptions import HTTPError, PyEzvizError
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||||
@ -24,17 +32,83 @@ class EzvizSelectEntityDescription(SelectEntityDescription):
|
|||||||
"""Describe a EZVIZ Select entity."""
|
"""Describe a EZVIZ Select entity."""
|
||||||
|
|
||||||
supported_switch: int
|
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",
|
key="alarm_sound_mod",
|
||||||
translation_key="alarm_sound_mode",
|
translation_key="alarm_sound_mode",
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
options=["soft", "intensive", "silent"],
|
options=["soft", "intensive", "silent"],
|
||||||
supported_switch=DeviceSwitchType.ALARM_TONE.value,
|
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(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: EzvizConfigEntry,
|
entry: EzvizConfigEntry,
|
||||||
@ -43,13 +117,27 @@ async def async_setup_entry(
|
|||||||
"""Set up EZVIZ select entities based on a config entry."""
|
"""Set up EZVIZ select entities based on a config entry."""
|
||||||
coordinator = entry.runtime_data
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(
|
entities = [
|
||||||
EzvizSelect(coordinator, camera)
|
EzvizSelect(coordinator, camera, ALARM_SOUND_MODE_SELECT_TYPE)
|
||||||
for camera in coordinator.data
|
for camera in coordinator.data
|
||||||
for switch in coordinator.data[camera]["switches"]
|
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):
|
class EzvizSelect(EzvizEntity, SelectEntity):
|
||||||
"""Representation of a EZVIZ select entity."""
|
"""Representation of a EZVIZ select entity."""
|
||||||
@ -58,31 +146,23 @@ class EzvizSelect(EzvizEntity, SelectEntity):
|
|||||||
self,
|
self,
|
||||||
coordinator: EzvizDataUpdateCoordinator,
|
coordinator: EzvizDataUpdateCoordinator,
|
||||||
serial: str,
|
serial: str,
|
||||||
|
description: EzvizSelectEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the select entity."""
|
||||||
super().__init__(coordinator, serial)
|
super().__init__(coordinator, serial)
|
||||||
self._attr_unique_id = f"{serial}_{SELECT_TYPE.key}"
|
self._attr_unique_id = f"{serial}_{description.key}"
|
||||||
self.entity_description = SELECT_TYPE
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_option(self) -> str | None:
|
def current_option(self) -> str | None:
|
||||||
"""Return the selected entity option to represent the entity state."""
|
"""Return the selected entity option to represent the entity state."""
|
||||||
sound_mode_value = getattr(
|
desc = cast(EzvizSelectEntityDescription, self.entity_description)
|
||||||
SoundMode, self.data[self.entity_description.key]
|
return desc.current_option(self)
|
||||||
).value
|
|
||||||
if sound_mode_value in [0, 1, 2]:
|
|
||||||
return self.options[sound_mode_value]
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def select_option(self, option: str) -> None:
|
def select_option(self, option: str) -> None:
|
||||||
"""Change the selected option."""
|
"""Change the selected option."""
|
||||||
sound_mode_value = self.options.index(option)
|
desc = cast(EzvizSelectEntityDescription, self.entity_description)
|
||||||
|
|
||||||
try:
|
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:
|
except (HTTPError, PyEzvizError) as err:
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(f"Cannot select option for {desc.key}") from err
|
||||||
f"Cannot set Warning sound level for {self.entity_id}"
|
|
||||||
) from err
|
|
||||||
|
@ -68,6 +68,16 @@
|
|||||||
"intensive": "Intensive",
|
"intensive": "Intensive",
|
||||||
"silent": "Silent"
|
"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": {
|
"image": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user