diff --git a/custom_components/ezviz_cloud/manifest.json b/custom_components/ezviz_cloud/manifest.json index a18c4e5..e0c37c8 100644 --- a/custom_components/ezviz_cloud/manifest.json +++ b/custom_components/ezviz_cloud/manifest.json @@ -9,5 +9,5 @@ "issue_tracker": "https://github.com/RenierM26/ha-ezviz/issues", "loggers": ["paho_mqtt", "pyezviz"], "requirements": ["pyezviz==0.2.2.2"], - "version": "0.1.0.40" + "version": "0.1.0.41" } diff --git a/custom_components/ezviz_cloud/siren.py b/custom_components/ezviz_cloud/siren.py index 6b9be0b..1f08b38 100644 --- a/custom_components/ezviz_cloud/siren.py +++ b/custom_components/ezviz_cloud/siren.py @@ -1,6 +1,7 @@ """Support for EZVIZ sirens.""" from __future__ import annotations +from collections.abc import Callable from datetime import datetime, timedelta from typing import Any @@ -54,6 +55,7 @@ class EzvizSirenEntity(EzvizBaseEntity, SirenEntity, RestoreEntity): _attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF _attr_should_poll = False + _attr_assumed_state = True def __init__( self, @@ -66,6 +68,7 @@ def __init__( self._attr_unique_id = f"{serial}_{description.key}" self.entity_description = description self._attr_is_on = False + self._delay_listener: Callable | None = None async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" @@ -79,32 +82,46 @@ async def async_added_to_hass(self) -> None: async def async_turn_off(self, **kwargs: Any) -> None: """Turn off camera siren.""" try: - if await self.hass.async_add_executor_job( + result = await self.hass.async_add_executor_job( self.coordinator.ezviz_client.sound_alarm, self._serial, 1 - ): - self._attr_is_on = False - self.async_write_ha_state() + ) except (HTTPError, PyEzvizError) as err: raise HomeAssistantError( f"Failed to turn siren off for {self.name}" ) from err + if result: + if self._delay_listener is not None: + self._delay_listener() + self._delay_listener = None + + self._attr_is_on = False + self.async_write_ha_state() + async def async_turn_on(self, **kwargs: Any) -> None: """Turn on camera siren.""" try: - if await self.hass.async_add_executor_job( + result = self.hass.async_add_executor_job( self.coordinator.ezviz_client.sound_alarm, self._serial, 2 - ): - self._attr_is_on = True - evt.async_call_later(self.hass, OFF_DELAY, self.off_delay_listener) - self.async_write_ha_state() + ) except (HTTPError, PyEzvizError) as err: raise HomeAssistantError( f"Failed to turn siren on for {self.name}" ) from err + if result: + if self._delay_listener is not None: + self._delay_listener() + self._delay_listener = None + + self._attr_is_on = True + self._delay_listener = evt.async_call_later( + self.hass, OFF_DELAY, self.off_delay_listener + ) + self.async_write_ha_state() + @callback def off_delay_listener(self, now: datetime) -> None: """Switch device off after a delay. @@ -112,4 +129,5 @@ def off_delay_listener(self, now: datetime) -> None: Camera firmware has hard coded turn off after 60 seconds. """ self._attr_is_on = False + self._delay_listener = None self.async_write_ha_state()