diff --git a/custom_components/dahua/__init__.py b/custom_components/dahua/__init__.py index 513c51f..8d8fd2e 100755 --- a/custom_components/dahua/__init__.py +++ b/custom_components/dahua/__init__.py @@ -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: @@ -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(): diff --git a/custom_components/dahua/client.py b/custom_components/dahua/client.py index 50c27ff..7a49e11 100755 --- a/custom_components/dahua/client.py +++ b/custom_components/dahua/client.py @@ -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. @@ -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 diff --git a/custom_components/dahua/const.py b/custom_components/dahua/const.py index 5438d7c..ec75962 100755 --- a/custom_components/dahua/const.py +++ b/custom_components/dahua/const.py @@ -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" diff --git a/custom_components/dahua/switch.py b/custom_components/dahua/switch.py index a654488..16eca97 100755 --- a/custom_components/dahua/switch.py +++ b/custom_components/dahua/switch.py @@ -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 @@ -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 @@ -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"""