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

fix: reflection of chime duration seconds #142

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/uiprotect/data/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings
from collections.abc import Callable
from datetime import datetime, timedelta
from functools import cache
from functools import cache, lru_cache
from ipaddress import IPv4Address
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, cast
Expand All @@ -25,6 +25,7 @@
convert_video_modes,
from_js_time,
serialize_point,
timedelta_total_seconds,
to_js_time,
utc_now,
)
Expand Down Expand Up @@ -900,6 +901,15 @@ class CameraAudioSettings(ProtectBaseObject):
style: list[AudioStyle]


@lru_cache
def _chime_type_from_total_seconds(total_seconds: float) -> ChimeType:
if total_seconds == 0.3:
return ChimeType.MECHANICAL
if total_seconds > 0.3:
return ChimeType.DIGITAL
return ChimeType.NONE


class Camera(ProtectMotionDeviceModel):
is_deleting: bool
# Microphone Sensitivity
Expand Down Expand Up @@ -1855,11 +1865,13 @@ async def set_glass_break_detection(self, enabled: bool) -> None:

@property
def chime_type(self) -> ChimeType:
if self.chime_duration.total_seconds() == 0.3:
return ChimeType.MECHANICAL
if self.chime_duration.total_seconds() > 0.3:
return ChimeType.DIGITAL
return ChimeType.NONE
return _chime_type_from_total_seconds(
timedelta_total_seconds(self.chime_duration)
)

@property
def chime_duration_seconds(self) -> float:
return timedelta_total_seconds(self.chime_duration)

@property
def is_digital_chime(self) -> bool:
Expand Down
5 changes: 5 additions & 0 deletions src/uiprotect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,8 @@ def make_required_getter(ufp_required_field: str) -> Callable[[T], bool]:
if "." not in ufp_required_field:
return partial(get_top_level_attr_as_bool, ufp_required_field)
return partial(get_nested_attr_as_bool, tuple(ufp_required_field.split(".")))


@lru_cache
def timedelta_total_seconds(td: timedelta) -> float:
return td.total_seconds()
3 changes: 2 additions & 1 deletion tests/data/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ async def test_camera_set_chime_duration_duration(
camera_obj.api.api_request.reset_mock()

camera_obj.feature_flags.has_chime = True
camera_obj.chime_duration = 300
camera_obj.chime_duration = timedelta(seconds=300)
assert camera_obj.chime_duration_seconds == 300
camera_obj.mic_volume = 10

if duration in {-1, 20}:
Expand Down
Loading