Skip to content

Commit

Permalink
Fix for Zone alarm device triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
pergolafabio committed Jun 28, 2023
1 parent da9b955 commit 10443cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
23 changes: 20 additions & 3 deletions hikvision-doorbell/src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,28 @@ async def video_intercom_alarm(
# Put sensor back to idle
call_sensor.set_state('idle')
case VideoInterComAlarmType.ZONE_ALARM:
#zone_name = alarm_info.uAlarmInfo.struZoneAlarm.byZoneName
#zone_name = str(alarm_info.uAlarmInfo.struZoneAlarm.byZoneName,'UTF-8')
zone_type = alarm_info.uAlarmInfo.struZoneAlarm.byZoneType
zone_number = alarm_info.uAlarmInfo.struZoneAlarm.dwZonendex
logger.info("Zone Alarm detected on doorbell {} type {} number {} ", doorbell._config.name, zone_type, zone_number)
trigger = DeviceTriggerMetadata(name=f"zone_alarm_{zone_type}", type="alarm", subtype=f"Zone {zone_number}")
match zone_type:
case 0:
zone_type= "Panic Button"
case 1:
zone_type= "Door Magnetic"
case 2:
zone_type= "Smoke Detector"
case 3:
zone_type= "Active Infared"
case 4:
zone_type= "Passive Infared"
case 11:
zone_type= "Gas Detector"
case 21:
zone_type= "Doorbell"
case _:
zone_type= "Unknown type: " + str(zone_type)
logger.info("Zone Alarm detected on doorbell {} type {} on zone {} ", doorbell._config.name, zone_type, (zone_number+1))
trigger = DeviceTriggerMetadata(name=f"zone_alarm_{zone_type}", type="alarm", subtype=f"Zone {zone_number+1}")
self.handle_device_trigger(doorbell, trigger)
case VideoInterComAlarmType.DOOR_NOT_OPEN | VideoInterComAlarmType.DOOR_NOT_CLOSED:
# Get information about the door that caused this alarm
Expand Down
2 changes: 1 addition & 1 deletion hikvision-doorbell/src/sdk/hcnetsdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class NET_DVR_ZONE_ALARM_INFO(Structure):
("byRes", BYTE * 219),
]

class NET_DVR_VIDEO_INTERCOM_ALARM_INFO_UNION(Structure):
class NET_DVR_VIDEO_INTERCOM_ALARM_INFO_UNION(Union):
_fields_ = [
("byLen", BYTE * 256),
("struZoneAlarm", NET_DVR_ZONE_ALARM_INFO),
Expand Down
6 changes: 3 additions & 3 deletions hikvision-doorbell/tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ class TestDeviceTrigger:
def test_zone_alarm(self, mocked_doorbell: Doorbell, handler: MQTTHandler, mocker: MockerFixture):
video_intercom_alarm = mocker.patch("sdk.hcnetsdk.NET_DVR_VIDEO_INTERCOM_ALARM")
video_intercom_alarm.byAlarmType = VIDEO_INTERCOM_ALARM_ALARMTYPE_ZONE_ALARM
video_intercom_alarm.uAlarmInfo.struZoneAlarm.byZoneType = 0
video_intercom_alarm.uAlarmInfo.struZoneAlarm.dwZonendex = 0
video_intercom_alarm.uAlarmInfo.struZoneAlarm.byZoneType = 1
video_intercom_alarm.uAlarmInfo.struZoneAlarm.dwZonendex = 1

asyncio.run(handler.video_intercom_alarm(mocked_doorbell, 0, None, video_intercom_alarm, 0, None))

# Check that the entity is saved in the dict
assert handler._sensors[mocked_doorbell]["zone_alarm_0"] is not None
#assert handler._sensors[mocked_doorbell]["zone_alarm_0"] is not None

def test_door_not_open(self, mocked_doorbell: Doorbell, handler: MQTTHandler, mocker: MockerFixture):
video_intercom_alarm = mocker.patch("sdk.hcnetsdk.NET_DVR_VIDEO_INTERCOM_ALARM")
Expand Down

0 comments on commit 10443cb

Please sign in to comment.