Skip to content
Open
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
12 changes: 11 additions & 1 deletion ring_doorbell/doorbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ def existing_doorbell_type(self) -> str | None:
"""
try:
if (dtype := self._get_chime_setting("type")) is not None:
return DOORBELL_EXISTING_TYPE[dtype]
return DOORBELL_EXISTING_TYPE.get(dtype)
except AttributeError:
return None
else:
return None

def _validate_existing_doorbell_type(self) -> None:
"""Raise if the device reports an unsupported existing doorbell type."""
dtype = self._get_chime_setting("type")
if dtype is not None and dtype not in DOORBELL_EXISTING_TYPE:
msg = f"Unsupported existing doorbell type: {dtype}"
raise RingError(msg)

async def async_set_existing_doorbell_type(self, value: int) -> None:
"""
Return existing doorbell type.
Expand All @@ -204,6 +211,7 @@ async def async_set_existing_doorbell_type(self, value: int) -> None:
1: Digital
2: Not Present
"""
self._validate_existing_doorbell_type()
if value not in DOORBELL_EXISTING_TYPE:
msg = f"value must be in {MSG_EXISTING_TYPE}"
raise RingError(msg)
Expand All @@ -226,6 +234,7 @@ def existing_doorbell_type_enabled(self) -> bool | None:

async def async_set_existing_doorbell_type_enabled(self, value: bool) -> None: # noqa: FBT001
"""Enable/disable the existing doorbell if Digital/Mechanical."""
self._validate_existing_doorbell_type()
if self.existing_doorbell_type:
if not isinstance(value, bool):
raise RingError(MSG_BOOLEAN_REQUIRED)
Expand Down Expand Up @@ -255,6 +264,7 @@ def existing_doorbell_type_duration(self) -> int | None:

async def async_set_existing_doorbell_type_duration(self, value: int) -> None:
"""Set duration for Digital chime."""
self._validate_existing_doorbell_type()
if self.existing_doorbell_type:
if not (
(isinstance(value, int))
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,43 @@ async def test_in_home_chime(ring, aioresponses_mock, devices_fixture):
assert expected in res.output


async def test_in_home_chime_unknown_type(ring, aioresponses_mock, devices_fixture):
"""In-home-chime setters must not print success for unsupported types (#542)."""
runner = CliRunner()
dev = ring.devices()["doorbots"][0]
settings = dev._attrs["settings"]["chime_settings"]

with runner.isolated_filesystem():
for unknown_type in (3, 5):
settings["type"] = unknown_type
assert dev.existing_doorbell_type is None

for args, success_text in (
(
["--device-name", "Front Door", "enabled", "False"],
"has been disabled",
),
(
["--device-name", "Front Door", "type", "mechanical"],
"has been set to",
),
(
["--device-name", "Front Door", "duration", "5"],
"has been set to 5 seconds",
),
):
aioresponses_mock.requests.clear()
res = await runner.invoke(in_home_chime, args, obj=ring)
assert res.exit_code != 0
assert success_text not in res.output
assert not aioresponses_mock.requests
assert (
f"Unsupported existing doorbell type: {unknown_type}" in res.output
or f"Unsupported existing doorbell type: {unknown_type}"
in str(res.exception)
)


async def test_open_door(ring, aioresponses_mock, devices_fixture):
runner = CliRunner()

Expand Down
54 changes: 54 additions & 0 deletions tests/test_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,57 @@ async def test_set_existing_doorbell_type(ring, aioresponses_mock):
# Attempting to set the doorbell duration to an invalid value
with pytest.raises(RingError, match=f"Must be within the {0}-{1}."):
await dev.async_set_existing_doorbell_type_duration(11)


def test_existing_doorbell_type_unknown(ring):
"""Unknown chime types must return None, not raise KeyError (#542)."""
dev = ring.devices()["doorbots"][0]
assert dev.existing_doorbell_type == "Mechanical"

settings = dev._attrs["settings"]["chime_settings"]
for unknown_type in (3, 5):
settings["type"] = unknown_type
assert dev.existing_doorbell_type is None
assert dev.existing_doorbell_type_enabled is False
assert dev.existing_doorbell_type_duration is None


async def test_set_existing_doorbell_type_unknown(ring, aioresponses_mock):
"""Setters must reject unsupported chime types, not silently succeed (#542)."""
dev = ring.devices()["doorbots"][0]
assert dev.existing_doorbell_type == "Mechanical"

settings = dev._attrs["settings"]["chime_settings"]
for unknown_type in (3, 5):
settings["type"] = unknown_type
assert dev.existing_doorbell_type is None

aioresponses_mock.requests.clear()
with pytest.raises(
RingError, match=f"Unsupported existing doorbell type: {unknown_type}"
):
await dev.async_set_existing_doorbell_type(0)
assert not aioresponses_mock.requests

aioresponses_mock.requests.clear()
with pytest.raises(
RingError, match=f"Unsupported existing doorbell type: {unknown_type}"
):
await dev.async_set_existing_doorbell_type_enabled(value=True)
assert not aioresponses_mock.requests

aioresponses_mock.requests.clear()
with pytest.raises(
RingError, match=f"Unsupported existing doorbell type: {unknown_type}"
):
await dev.async_set_existing_doorbell_type_duration(5)
assert not aioresponses_mock.requests

# A missing chime type keeps its existing silent no-op behaviour.
settings.pop("type", None)
assert dev.existing_doorbell_type is None
aioresponses_mock.requests.clear()
await dev.async_set_existing_doorbell_type(0)
await dev.async_set_existing_doorbell_type_enabled(value=True)
await dev.async_set_existing_doorbell_type_duration(5)
assert not aioresponses_mock.requests