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

Chime ringtones #346

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions src/uiprotect/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,19 +1806,24 @@ async def play_speaker(
*,
volume: int | None = None,
repeat_times: int | None = None,
ringtone_id: str | None = None,
track_no: int | None = None,
) -> None:
"""Plays chime tones on a chime"""
data: dict[str, Any] | None = None
if volume or repeat_times:
if volume or repeat_times or ringtone_id or track_no:
chime = self.bootstrap.chimes.get(device_id)
if chime is None:
raise BadRequest("Invalid chime ID %s", device_id)

data = {
"volume": volume or chime.volume,
"repeatTimes": repeat_times or chime.repeat_times,
"trackNo": chime.track_no,
"trackNo": track_no or chime.track_no,
}
if ringtone_id:
data["ringtoneId"] = ringtone_id
data.pop("trackNo", None)

await self.api_request(
f"chimes/{device_id}/play-speaker",
Expand Down
2 changes: 2 additions & 0 deletions src/uiprotect/data/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Doorlock,
Light,
ProtectAdoptableDeviceModel,
Ringtone,
Sensor,
Viewer,
)
Expand Down Expand Up @@ -183,6 +184,7 @@ class Bootstrap(ProtectBaseObject):
doorlocks: dict[str, Doorlock]
chimes: dict[str, Chime]
aiports: dict[str, AiPort]
ringtones: list[Ringtone]
last_update_id: str

# TODO:
Expand Down
2 changes: 2 additions & 0 deletions src/uiprotect/data/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Chime,
Doorlock,
Light,
Ringtone,
Sensor,
Viewer,
)
Expand Down Expand Up @@ -44,6 +45,7 @@
ModelType.AIPORT: AiPort,
ModelType.KEYRING: Keyring,
ModelType.ULP_USER: UlpUser,
ModelType.RINGTONE: Ringtone,
}


Expand Down
8 changes: 8 additions & 0 deletions src/uiprotect/data/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3388,3 +3388,11 @@ def callback() -> None:

class AiPort(Camera):
paired_cameras: list[str]

class Ringtone(ProtectBaseObject):
id: str
name: str
size: int
is_default: bool
nvr_mac: str
model_key: str
1 change: 1 addition & 0 deletions src/uiprotect/data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class ModelType(str, UnknownValuesEnumMixin, enum.Enum):
DEVICE_GROUP = "deviceGroup"
RECORDING_SCHEDULE = "recordingSchedule"
ULP_USER = "ulpUser"
RINGTONE = "ringtone"
KEYRING = "keyring"
UNKNOWN = "unknown"

Expand Down
18 changes: 18 additions & 0 deletions tests/sample_data/sample_bootstrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -7276,6 +7276,24 @@
"modelKey": "chime"
}
],
"ringtones": [
{
"id": "66a14fa502d44203e40003eb",
"name": "Default",
"size": 208,
"isDefault": true,
"nvrMac": "4B8290F6D7A3",
"modelKey": "ringtone"
},
{
"id": "66a14fa502da4203e40003ec",
"name": "custometest",
"size": 180,
"isDefault": false,
"nvrMac": "4B8290F6D7A3",
"modelKey": "ringtone"
}
],
RaHehl marked this conversation as resolved.
Show resolved Hide resolved
"aiports": [
{
"isDeleting": false,
Expand Down
96 changes: 96 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,99 @@ async def test_get_aiports(protect_client: ProtectApiClient, aiports):
objs = [create_from_unifi_dict(d) for d in aiports]

assert_equal_dump(objs, await protect_client.get_aiports())

@pytest.mark.asyncio()
async def test_play_speaker(protect_client: ProtectApiClient):
"""Test play_speaker with default parameters."""
device_id = "cf1a330397c08f919d02bd7c"
protect_client.api_request = AsyncMock()

await protect_client.play_speaker(device_id)

protect_client.api_request.assert_called_with(
f"chimes/{device_id}/play-speaker",
method="post",
json=None,
)


@pytest.mark.asyncio()
async def test_play_speaker_with_volume(protect_client: ProtectApiClient):
"""Test play_speaker with volume parameter."""
device_id = "cf1a330397c08f919d02bd7c"
volume = 5
chime = protect_client.bootstrap.chimes[device_id]
protect_client.api_request = AsyncMock()

await protect_client.play_speaker(device_id, volume=volume)

protect_client.api_request.assert_called_with(
f"chimes/{device_id}/play-speaker",
method="post",
json={
"volume": volume,
"repeatTimes": chime.repeat_times,
"trackNo": chime.track_no,
},
)


@pytest.mark.asyncio()
async def test_play_speaker_with_ringtone_id(protect_client: ProtectApiClient):
"""Test play_speaker with ringtone_id parameter."""
device_id = "cf1a330397c08f919d02bd7c"
ringtone_id = "ringtone_1"
chime = protect_client.bootstrap.chimes[device_id]
protect_client.api_request = AsyncMock()

await protect_client.play_speaker(device_id, ringtone_id=ringtone_id)

protect_client.api_request.assert_called_with(
f"chimes/{device_id}/play-speaker",
method="post",
json={
"volume": chime.volume,
"repeatTimes": chime.repeat_times,
"ringtoneId": ringtone_id,
},
)


@pytest.mark.asyncio()
async def test_play_speaker_invalid_chime_id(protect_client: ProtectApiClient):
"""Test play_speaker with invalid chime ID."""
device_id = "invalid_id"
protect_client.api_request = AsyncMock()

with pytest.raises(BadRequest):
await protect_client.play_speaker(device_id, volume=5)


@pytest.mark.asyncio()
async def test_play_speaker_with_all_parameters(protect_client: ProtectApiClient):
"""Test play_speaker with all parameters."""
device_id = "cf1a330397c08f919d02bd7c"
volume = 5
repeat_times = 3
ringtone_id = "ringtone_1"
track_no = 2
protect_client.api_request = AsyncMock()

await protect_client.play_speaker(
device_id,
volume=volume,
repeat_times=repeat_times,
ringtone_id=ringtone_id,
track_no=track_no,
)

protect_client.api_request.assert_called_with(
f"chimes/{device_id}/play-speaker",
method="post",
json={
"volume": volume,
"repeatTimes": repeat_times,
"ringtoneId": ringtone_id,
},
)

Loading