Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ezviz battery camera work mode #130478

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
123 changes: 105 additions & 18 deletions homeassistant/components/ezviz/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

from __future__ import annotations

from collections.abc import Callable

Check warning on line 5 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L5

Added line #L5 was not covered by tests
from dataclasses import dataclass

from pyezviz.constants import DeviceSwitchType, SoundMode
from pyezviz.constants import (

Check warning on line 8 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L8

Added line #L8 was not covered by tests
BatteryCameraWorkMode,
DeviceCatagories,
DeviceSwitchType,
SoundMode,
)
from pyezviz.exceptions import HTTPError, PyEzvizError

from homeassistant.components.select import SelectEntity, SelectEntityDescription
Expand All @@ -21,19 +27,92 @@
PARALLEL_UPDATES = 1


class EzvizSelectEntityActionBase:

Check warning on line 30 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L30

Added line #L30 was not covered by tests
"""Base class to handle retrieval and selection of a EzvizSelectEntity."""

current_option: Callable[[EzvizSelect], str | None]
select_option: Callable[[EzvizSelect, str, str], None]

Check warning on line 34 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L33-L34

Added lines #L33 - L34 were not covered by tests


class AlarmSoundModeAction(EzvizSelectEntityActionBase):

Check warning on line 37 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L37

Added line #L37 was not covered by tests
"""Class dedicated to Alarm Sound Mode."""

@staticmethod
def current_option(ezvizSelect: EzvizSelect) -> str | None:

Check warning on line 41 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L40-L41

Added lines #L40 - L41 were not covered by tests
"""Return the selected entity option to represent the entity state."""
sound_mode_value = getattr(

Check warning on line 43 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L43

Added line #L43 was not covered by tests
SoundMode, ezvizSelect.data[ezvizSelect.entity_description.key]
).value
if sound_mode_value in [0, 1, 2]:
return ezvizSelect.options[sound_mode_value]

Check warning on line 47 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L46-L47

Added lines #L46 - L47 were not covered by tests

return None

Check warning on line 49 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L49

Added line #L49 was not covered by tests

@staticmethod
def select_option(ezvizSelect: EzvizSelect, serial: str, option: str) -> None:

Check warning on line 52 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L51-L52

Added lines #L51 - L52 were not covered by tests
"""Change the selected option."""
sound_mode_value = ezvizSelect.options.index(option)

Check warning on line 54 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L54

Added line #L54 was not covered by tests

ezvizSelect.coordinator.ezviz_client.alarm_sound(serial, sound_mode_value, 1)

Check warning on line 56 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L56

Added line #L56 was not covered by tests


class BatteryWorkModeAction(EzvizSelectEntityActionBase):

Check warning on line 59 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L59

Added line #L59 was not covered by tests
"""Class dedicated to Battery Work Mode."""

@staticmethod
def current_option(ezvizSelect: EzvizSelect) -> str | None:

Check warning on line 63 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L62-L63

Added lines #L62 - L63 were not covered by tests
"""Return the selected entity option to represent the entity state."""
battery_work_mode = getattr(

Check warning on line 65 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L65

Added line #L65 was not covered by tests
BatteryCameraWorkMode,
ezvizSelect.data[ezvizSelect.entity_description.key],
BatteryCameraWorkMode.UNKNOWN,
)
if battery_work_mode == BatteryCameraWorkMode.UNKNOWN:
return None

Check warning on line 71 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L70-L71

Added lines #L70 - L71 were not covered by tests

return battery_work_mode.name.lower()

Check warning on line 73 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L73

Added line #L73 was not covered by tests

@staticmethod
def select_option(ezvizSelect: EzvizSelect, serial: str, option: str) -> None:

Check warning on line 76 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L75-L76

Added lines #L75 - L76 were not covered by tests
"""Change the selected option."""
battery_work_mode = getattr(BatteryCameraWorkMode, option.upper())

Check warning on line 78 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L78

Added line #L78 was not covered by tests

ezvizSelect.coordinator.ezviz_client.set_battery_camera_work_mode(

Check warning on line 80 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L80

Added line #L80 was not covered by tests
serial, battery_work_mode.value
)


@dataclass(frozen=True, kw_only=True)
class EzvizSelectEntityDescription(SelectEntityDescription):
"""Describe a EZVIZ Select entity."""

supported_switch: int
action_handler: type[EzvizSelectEntityActionBase]

Check warning on line 90 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L90

Added line #L90 was not covered by tests


SELECT_TYPE = EzvizSelectEntityDescription(
ALARM_SOUND_MODE_SELECT_TYPE = EzvizSelectEntityDescription(

Check warning on line 93 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L93

Added line #L93 was not covered by tests
key="alarm_sound_mod",
translation_key="alarm_sound_mode",
entity_category=EntityCategory.CONFIG,
options=["soft", "intensive", "silent"],
supported_switch=DeviceSwitchType.ALARM_TONE.value,
action_handler=AlarmSoundModeAction,
)

BATTERY_WORK_MODE_SELECT_TYPE = EzvizSelectEntityDescription(

Check warning on line 102 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L102

Added line #L102 was not covered by tests
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,
action_handler=BatteryWorkModeAction,
)


Expand All @@ -45,46 +124,54 @@
DATA_COORDINATOR
]

async_add_entities(
EzvizSelect(coordinator, camera)
entities_to_add: list[EzvizSelect] = []

Check warning on line 127 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L127

Added line #L127 was not covered by tests

entities_to_add.extend(

Check warning on line 129 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L129

Added line #L129 was not covered by tests
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
)

entities_to_add.extend(

Check warning on line 136 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L136

Added line #L136 was not covered by tests
EzvizSelect(coordinator, camera, BATTERY_WORK_MODE_SELECT_TYPE)
for camera in coordinator.data
if coordinator.data[camera]["device_category"]
== DeviceCatagories.BATTERY_CAMERA_DEVICE_CATEGORY.value
)

async_add_entities(entities_to_add)

Check warning on line 143 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L143

Added line #L143 was not covered by tests


class EzvizSelect(EzvizEntity, SelectEntity):
"""Representation of a EZVIZ select entity."""

entity_description: EzvizSelectEntityDescription

Check warning on line 149 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L149

Added line #L149 was not covered by tests

def __init__(
self,
coordinator: EzvizDataUpdateCoordinator,
serial: str,
description: EzvizSelectEntityDescription,
) -> None:
"""Initialize the sensor."""
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

Check warning on line 160 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L159-L160

Added lines #L159 - L160 were not covered by tests

@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
return self.entity_description.action_handler.current_option(self)

Check warning on line 165 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L165

Added line #L165 was not covered by tests

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)
return self.entity_description.action_handler.select_option(

Check warning on line 170 in homeassistant/components/ezviz/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/ezviz/select.py#L170

Added line #L170 was not covered by tests
self, self._serial, option
)

except (HTTPError, PyEzvizError) as err:
raise HomeAssistantError(
f"Cannot set Warning sound level for {self.entity_id}"
f"Cannot select option for {self.entity_description.key}"
) from err
10 changes: 10 additions & 0 deletions homeassistant/components/ezviz/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down