Skip to content

Commit

Permalink
Add event notifications boolean switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jakon89 committed Oct 25, 2024
1 parent 6522072 commit a26b9b4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions custom_components/dahua/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ async def _async_update_data(self):
asyncio.ensure_future(self.client.async_get_config_lighting(self._channel, self._profile_mode)))
if self._supports_disarming_linkage:
coros.append(asyncio.ensure_future(self.client.async_get_disarming_linkage()))
coros.append(asyncio.ensure_future(self.client.async_get_event_notifications()))
if self._supports_coaxial_control:
coros.append(asyncio.ensure_future(self.client.async_get_coaxial_control_io_status()))
if self._supports_smart_motion_detection:
Expand Down Expand Up @@ -592,6 +593,10 @@ def is_disarming_linkage_enabled(self) -> bool:
""" Returns true if disarming linkage is enable """
return self.data.get("table.DisableLinkage.Enable", "").lower() == "true"

def is_event_notifications_enabled(self) -> bool:
""" Returns true if event notifications is enable """
return self.data.get("table.DisableEventNotify.Enable", "").lower() == "false"

def is_smart_motion_detection_enabled(self) -> bool:
""" Returns true if smart motion detection is enabled """
if self.supports_smart_motion_detection_amcrest():
Expand Down
23 changes: 23 additions & 0 deletions custom_components/dahua/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ async def async_set_disarming_linkage(self, channel: int, enabled: bool) -> dict
url = "/cgi-bin/configManager.cgi?action=setConfig&DisableLinkage[{0}].Enable={1}".format(channel, value)
return await self.get(url)

async def async_set_event_notifications(self, channel: int, enabled: bool) -> dict:
"""
async_set_event_notifications will set the camera's disarming event notifications (Event -> Disarming -> Event Notifications in the UI)
"""

value = "true"
if enabled:
value = "false"

url = "/cgi-bin/configManager.cgi?action=setConfig&DisableEventNotify[{0}].Enable={1}".format(channel, value)
return await self.get(url)

async def async_set_record_mode(self, channel: int, mode: str) -> dict:
"""
async_set_record_mode sets the record mode.
Expand Down Expand Up @@ -631,6 +643,17 @@ async def async_get_disarming_linkage(self) -> dict:
url = "/cgi-bin/configManager.cgi?action=getConfig&name=DisableLinkage"
return await self.get(url)

async def async_get_event_notifications(self) -> dict:
"""
async_get_event_notifications will return false if the event notifications in disarmed state are enabled
returns
table.DisableEventNotify.Enable=false
"""

url = "/cgi-bin/configManager.cgi?action=getConfig&name=DisableEventNotify"
return await self.get(url)

async def async_access_control_open_door(self, door_id: int = 1) -> dict:
"""
async_access_control_open_door opens a door via a VTO
Expand Down
1 change: 1 addition & 0 deletions custom_components/dahua/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
INFRARED_ICON = "mdi:weather-night"
DISARMING_ICON = "mdi:alarm-check"
VOLUME_HIGH_ICON = "mdi:volume-high"
BELL_ICON = "mdi:bell-ring"

# Device classes - https://www.home-assistant.io/integrations/binary_sensor/#device-class
MOTION_SENSOR_DEVICE_CLASS = "motion"
Expand Down
43 changes: 42 additions & 1 deletion custom_components/dahua/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from homeassistant.components.switch import SwitchEntity
from custom_components.dahua import DahuaDataUpdateCoordinator

from .const import DOMAIN, DISARMING_ICON, MOTION_DETECTION_ICON, SIREN_ICON
from .const import DOMAIN, DISARMING_ICON, MOTION_DETECTION_ICON, SIREN_ICON, BELL_ICON
from .entity import DahuaBaseEntity
from .client import SIREN_TYPE

Expand All @@ -27,6 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry, async_add_devices):
try:
await coordinator.client.async_get_disarming_linkage()
devices.append(DahuaDisarmingLinkageBinarySwitch(coordinator, entry))
devices.append(DahuaDisarmingEventNotificationsLinkageBinarySwitch(coordinator, entry))
except ClientError as exception:
pass

Expand Down Expand Up @@ -117,6 +118,46 @@ def is_on(self):
"""
return self._coordinator.is_disarming_linkage_enabled()

class DahuaDisarmingEventNotificationsLinkageBinarySwitch(DahuaBaseEntity, SwitchEntity):
"""will set the camera's event notifications when device is disarmed (Event -> Disarming -> Event Notifications in the UI)"""

async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
"""Turn on/enable event notifications"""
channel = self._coordinator.get_channel()
await self._coordinator.client.async_set_event_notifications(channel, True)
await self._coordinator.async_refresh()

async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
"""Turn off/disable event notifications"""
channel = self._coordinator.get_channel()
await self._coordinator.client.async_set_event_notifications(channel, False)
await self._coordinator.async_refresh()

@property
def name(self):
"""Return the name of the switch."""
return self._coordinator.get_device_name() + " " + "Event Notifications"

@property
def unique_id(self):
"""
A unique identifier for this entity. Needs to be unique within a platform (ie light.hue). Should not be
configurable by the user or be changeable see
https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements
"""
return self._coordinator.get_serial_number() + "_event_notifications"

@property
def icon(self):
"""Return the icon of this switch."""
return BELL_ICON

@property
def is_on(self):
"""
Return true if the switch is on.
"""
return self._coordinator.is_event_notifications_enabled()

class DahuaSmartMotionDetectionBinarySwitch(DahuaBaseEntity, SwitchEntity):
"""Enables or disables the Smart Motion Detection option in the camera"""
Expand Down

0 comments on commit a26b9b4

Please sign in to comment.