Skip to content

Commit

Permalink
Update siren, reset scheduled off if triggered while on. (#134)
Browse files Browse the repository at this point in the history
* Update siren.py

* Update manifest.json
  • Loading branch information
RenierM26 authored Aug 13, 2023
1 parent a308d70 commit 18f4e65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion custom_components/ezviz_cloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
36 changes: 27 additions & 9 deletions custom_components/ezviz_cloud/siren.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand All @@ -79,37 +82,52 @@ 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.
Camera firmware has hard coded turn off after 60 seconds.
"""
self._attr_is_on = False
self._delay_listener = None
self.async_write_ha_state()

0 comments on commit 18f4e65

Please sign in to comment.