From 0ed87da796447bbd75f9f424cc152f00df4095c3 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:44:16 +0100 Subject: [PATCH] Await sound_alarm when turning on EZVIZ siren (#191237) --- homeassistant/components/ezviz/siren.py | 2 +- tests/components/ezviz/test_siren.py | 65 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/components/ezviz/test_siren.py diff --git a/homeassistant/components/ezviz/siren.py b/homeassistant/components/ezviz/siren.py index 1926f9af2b25..3a5a43cda128 100644 --- a/homeassistant/components/ezviz/siren.py +++ b/homeassistant/components/ezviz/siren.py @@ -102,7 +102,7 @@ class EzvizSirenEntity(EzvizBaseEntity, SirenEntity, RestoreEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on camera siren.""" try: - result = self.hass.async_add_executor_job( + result = await self.hass.async_add_executor_job( self.coordinator.ezviz_client.sound_alarm, self._serial, 2 ) diff --git a/tests/components/ezviz/test_siren.py b/tests/components/ezviz/test_siren.py new file mode 100644 index 000000000000..927c328539eb --- /dev/null +++ b/tests/components/ezviz/test_siren.py @@ -0,0 +1,65 @@ +"""Tests for EZVIZ siren entities.""" + +from unittest.mock import AsyncMock + +from pyezvizapi import PyEzvizError, SupportExt +import pytest +from pytest_homeassistant.common import MockConfigEntry + +from homeassistant.components.siren import DOMAIN as SIREN_DOMAIN, SERVICE_TURN_ON +from homeassistant.const import ATTR_ENTITY_ID, STATE_ON +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from . import setup_integration +from .test_init import _mock_camera_data + +ENTITY_ID = "siren.camera_1_siren" + + +@pytest.fixture(autouse=True) +def mock_siren_camera(mock_ezviz_client: AsyncMock) -> None: + """Return a camera supporting the siren.""" + mock_ezviz_client.load_cameras.return_value = { + "C123456789": _mock_camera_data( + supportExt={str(SupportExt.SupportActiveDefense.value): "1"} + ) + } + + +async def test_turn_on( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ezviz_client: AsyncMock, +) -> None: + """Test turning on the siren.""" + await setup_integration(hass, mock_config_entry) + + await hass.services.async_call( + SIREN_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True + ) + + mock_ezviz_client.sound_alarm.assert_called_once_with("C123456789", 2) + assert hass.states.get(ENTITY_ID).state == STATE_ON + + +async def test_turn_on_error( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ezviz_client: AsyncMock, +) -> None: + """Test turning on the siren raises when the API fails.""" + await setup_integration(hass, mock_config_entry) + + def sound_alarm(serial: str, enable: int) -> None: + raise PyEzvizError + + # A plain function, as the test harness runs Mock executor jobs inline + mock_ezviz_client.sound_alarm = sound_alarm + + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SIREN_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True + ) + + assert hass.states.get(ENTITY_ID).state != STATE_ON