1 """ONVIF event parsers."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine
13 from .models
import Event
15 PARSERS: Registry[str, Callable[[str, Any], Coroutine[Any, Any, Event |
None]]] = (
19 VIDEO_SOURCE_MAPPING = {
20 "vsconf":
"VideoSourceToken",
25 """Extract the message content and the topic."""
26 return msg.Topic._value_1, msg.Message._value_1
30 """Normalize video source.
32 Some cameras do not set the VideoSourceToken correctly so we get duplicate
33 sensors, so we need to normalize it to the correct value.
35 return VIDEO_SOURCE_MAPPING.get(source, source)
39 """Convert strings to datetimes, if invalid, return None."""
42 ret = dt_util.parse_datetime(value)
46 return dt_util.as_local(ret)
50 @PARSERS.register("tns1:VideoSource/MotionAlarm")
52 """Handle parsing event message.
54 Topic: tns1:VideoSource/MotionAlarm
58 source = payload.Source.SimpleItem[0].Value
60 f
"{uid}_{topic}_{source}",
65 payload.Data.SimpleItem[0].Value ==
"true",
67 except (AttributeError, KeyError):
71 @PARSERS.register("tns1:VideoSource/ImageTooBlurry/AnalyticsService")
72 @PARSERS.register("tns1:VideoSource/ImageTooBlurry/ImagingService")
73 @PARSERS.register("tns1:VideoSource/ImageTooBlurry/RecordingService")
75 """Handle parsing event message.
77 Topic: tns1:VideoSource/ImageTooBlurry/*
81 source = payload.Source.SimpleItem[0].Value
83 f
"{uid}_{topic}_{source}",
88 payload.Data.SimpleItem[0].Value ==
"true",
89 EntityCategory.DIAGNOSTIC,
91 except (AttributeError, KeyError):
95 @PARSERS.register("tns1:VideoSource/ImageTooDark/AnalyticsService")
96 @PARSERS.register("tns1:VideoSource/ImageTooDark/ImagingService")
97 @PARSERS.register("tns1:VideoSource/ImageTooDark/RecordingService")
99 """Handle parsing event message.
101 Topic: tns1:VideoSource/ImageTooDark/*
105 source = payload.Source.SimpleItem[0].Value
107 f
"{uid}_{topic}_{source}",
112 payload.Data.SimpleItem[0].Value ==
"true",
113 EntityCategory.DIAGNOSTIC,
115 except (AttributeError, KeyError):
119 @PARSERS.register("tns1:VideoSource/ImageTooBright/AnalyticsService")
120 @PARSERS.register("tns1:VideoSource/ImageTooBright/ImagingService")
121 @PARSERS.register("tns1:VideoSource/ImageTooBright/RecordingService")
123 """Handle parsing event message.
125 Topic: tns1:VideoSource/ImageTooBright/*
129 source = payload.Source.SimpleItem[0].Value
131 f
"{uid}_{topic}_{source}",
136 payload.Data.SimpleItem[0].Value ==
"true",
137 EntityCategory.DIAGNOSTIC,
139 except (AttributeError, KeyError):
143 @PARSERS.register("tns1:VideoSource/GlobalSceneChange/AnalyticsService")
144 @PARSERS.register("tns1:VideoSource/GlobalSceneChange/ImagingService")
145 @PARSERS.register("tns1:VideoSource/GlobalSceneChange/RecordingService")
147 """Handle parsing event message.
149 Topic: tns1:VideoSource/GlobalSceneChange/*
153 source = payload.Source.SimpleItem[0].Value
155 f
"{uid}_{topic}_{source}",
156 "Global Scene Change",
160 payload.Data.SimpleItem[0].Value ==
"true",
162 except (AttributeError, KeyError):
166 @PARSERS.register("tns1:AudioAnalytics/Audio/DetectedSound")
168 """Handle parsing event message.
170 Topic: tns1:AudioAnalytics/Audio/DetectedSound
177 for source
in payload.Source.SimpleItem:
178 if source.Name ==
"AudioSourceConfigurationToken":
179 audio_source = source.Value
180 if source.Name ==
"AudioAnalyticsConfigurationToken":
181 audio_analytics = source.Value
182 if source.Name ==
"Rule":
186 f
"{uid}_{topic}_{audio_source}_{audio_analytics}_{rule}",
191 payload.Data.SimpleItem[0].Value ==
"true",
193 except (AttributeError, KeyError):
197 @PARSERS.register("tns1:RuleEngine/FieldDetector/ObjectsInside")
199 """Handle parsing event message.
201 Topic: tns1:RuleEngine/FieldDetector/ObjectsInside
208 for source
in payload.Source.SimpleItem:
209 if source.Name ==
"VideoSourceConfigurationToken":
211 if source.Name ==
"VideoAnalyticsConfigurationToken":
212 video_analytics = source.Value
213 if source.Name ==
"Rule":
217 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
222 payload.Data.SimpleItem[0].Value ==
"true",
224 except (AttributeError, KeyError):
229 @PARSERS.register("tns1:RuleEngine/CellMotionDetector/Motion")
231 """Handle parsing event message.
233 Topic: tns1:RuleEngine/CellMotionDetector/Motion
240 for source
in payload.Source.SimpleItem:
241 if source.Name ==
"VideoSourceConfigurationToken":
243 if source.Name ==
"VideoAnalyticsConfigurationToken":
244 video_analytics = source.Value
245 if source.Name ==
"Rule":
249 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
250 "Cell Motion Detection",
254 payload.Data.SimpleItem[0].Value ==
"true",
256 except (AttributeError, KeyError):
260 @PARSERS.register("tns1:RuleEngine/MotionRegionDetector/Motion")
262 """Handle parsing event message.
264 Topic: tns1:RuleEngine/MotionRegionDetector/Motion
271 for source
in payload.Source.SimpleItem:
272 if source.Name ==
"VideoSourceConfigurationToken":
274 if source.Name ==
"VideoAnalyticsConfigurationToken":
275 video_analytics = source.Value
276 if source.Name ==
"Rule":
280 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
281 "Motion Region Detection",
285 payload.Data.SimpleItem[0].Value
in [
"1",
"true"],
287 except (AttributeError, KeyError):
291 @PARSERS.register("tns1:RuleEngine/TamperDetector/Tamper")
293 """Handle parsing event message.
295 Topic: tns1:RuleEngine/TamperDetector/Tamper
302 for source
in payload.Source.SimpleItem:
303 if source.Name ==
"VideoSourceConfigurationToken":
305 if source.Name ==
"VideoAnalyticsConfigurationToken":
306 video_analytics = source.Value
307 if source.Name ==
"Rule":
311 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
316 payload.Data.SimpleItem[0].Value ==
"true",
317 EntityCategory.DIAGNOSTIC,
319 except (AttributeError, KeyError):
323 @PARSERS.register("tns1:RuleEngine/MyRuleDetector/DogCatDetect")
325 """Handle parsing event message.
327 Topic: tns1:RuleEngine/MyRuleDetector/DogCatDetect
332 for source
in payload.Source.SimpleItem:
333 if source.Name ==
"Source":
337 f
"{uid}_{topic}_{video_source}",
342 payload.Data.SimpleItem[0].Value ==
"true",
344 except (AttributeError, KeyError):
348 @PARSERS.register("tns1:RuleEngine/MyRuleDetector/VehicleDetect")
350 """Handle parsing event message.
352 Topic: tns1:RuleEngine/MyRuleDetector/VehicleDetect
357 for source
in payload.Source.SimpleItem:
358 if source.Name ==
"Source":
362 f
"{uid}_{topic}_{video_source}",
367 payload.Data.SimpleItem[0].Value ==
"true",
369 except (AttributeError, KeyError):
373 @PARSERS.register("tns1:RuleEngine/MyRuleDetector/PeopleDetect")
375 """Handle parsing event message.
377 Topic: tns1:RuleEngine/MyRuleDetector/PeopleDetect
382 for source
in payload.Source.SimpleItem:
383 if source.Name ==
"Source":
387 f
"{uid}_{topic}_{video_source}",
392 payload.Data.SimpleItem[0].Value ==
"true",
394 except (AttributeError, KeyError):
398 @PARSERS.register("tns1:RuleEngine/MyRuleDetector/FaceDetect")
400 """Handle parsing event message.
402 Topic: tns1:RuleEngine/MyRuleDetector/FaceDetect
407 for source
in payload.Source.SimpleItem:
408 if source.Name ==
"Source":
412 f
"{uid}_{topic}_{video_source}",
417 payload.Data.SimpleItem[0].Value ==
"true",
419 except (AttributeError, KeyError):
423 @PARSERS.register("tns1:RuleEngine/MyRuleDetector/Visitor")
425 """Handle parsing event message.
427 Topic: tns1:RuleEngine/MyRuleDetector/Visitor
432 for source
in payload.Source.SimpleItem:
433 if source.Name ==
"Source":
437 f
"{uid}_{topic}_{video_source}",
442 payload.Data.SimpleItem[0].Value ==
"true",
444 except (AttributeError, KeyError):
448 @PARSERS.register("tns1:Device/Trigger/DigitalInput")
450 """Handle parsing event message.
452 Topic: tns1:Device/Trigger/DigitalInput
456 source = payload.Source.SimpleItem[0].Value
458 f
"{uid}_{topic}_{source}",
463 payload.Data.SimpleItem[0].Value ==
"true",
465 except (AttributeError, KeyError):
469 @PARSERS.register("tns1:Device/Trigger/Relay")
471 """Handle parsing event message.
473 Topic: tns1:Device/Trigger/Relay
477 source = payload.Source.SimpleItem[0].Value
479 f
"{uid}_{topic}_{source}",
484 payload.Data.SimpleItem[0].Value ==
"active",
486 except (AttributeError, KeyError):
490 @PARSERS.register("tns1:Device/HardwareFailure/StorageFailure")
492 """Handle parsing event message.
494 Topic: tns1:Device/HardwareFailure/StorageFailure
498 source = payload.Source.SimpleItem[0].Value
500 f
"{uid}_{topic}_{source}",
505 payload.Data.SimpleItem[0].Value ==
"true",
506 EntityCategory.DIAGNOSTIC,
508 except (AttributeError, KeyError):
512 @PARSERS.register("tns1:Monitoring/ProcessorUsage")
514 """Handle parsing event message.
516 Topic: tns1:Monitoring/ProcessorUsage
520 usage =
float(payload.Data.SimpleItem[0].Value)
531 EntityCategory.DIAGNOSTIC,
533 except (AttributeError, KeyError):
537 @PARSERS.register("tns1:Monitoring/OperatingTime/LastReboot")
539 """Handle parsing event message.
541 Topic: tns1:Monitoring/OperatingTime/LastReboot
553 EntityCategory.DIAGNOSTIC,
555 except (AttributeError, KeyError):
559 @PARSERS.register("tns1:Monitoring/OperatingTime/LastReset")
561 """Handle parsing event message.
563 Topic: tns1:Monitoring/OperatingTime/LastReset
575 EntityCategory.DIAGNOSTIC,
576 entity_enabled=
False,
578 except (AttributeError, KeyError):
582 @PARSERS.register("tns1:Monitoring/Backup/Last")
584 """Handle parsing event message.
586 Topic: tns1:Monitoring/Backup/Last
599 EntityCategory.DIAGNOSTIC,
600 entity_enabled=
False,
602 except (AttributeError, KeyError):
606 @PARSERS.register("tns1:Monitoring/OperatingTime/LastClockSynchronization")
608 """Handle parsing event message.
610 Topic: tns1:Monitoring/OperatingTime/LastClockSynchronization
617 "Last Clock Synchronization",
622 EntityCategory.DIAGNOSTIC,
623 entity_enabled=
False,
625 except (AttributeError, KeyError):
629 @PARSERS.register("tns1:RecordingConfig/JobState")
631 """Handle parsing event message.
633 Topic: tns1:RecordingConfig/JobState
638 source = payload.Source.SimpleItem[0].Value
640 f
"{uid}_{topic}_{source}",
641 "Recording Job State",
645 payload.Data.SimpleItem[0].Value ==
"Active",
646 EntityCategory.DIAGNOSTIC,
648 except (AttributeError, KeyError):
652 @PARSERS.register("tns1:RuleEngine/LineDetector/Crossed")
654 """Handle parsing event message.
656 Topic: tns1:RuleEngine/LineDetector/Crossed
663 for source
in payload.Source.SimpleItem:
664 if source.Name ==
"VideoSourceConfigurationToken":
665 video_source = source.Value
666 if source.Name ==
"VideoAnalyticsConfigurationToken":
667 video_analytics = source.Value
668 if source.Name ==
"Rule":
672 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
673 "Line Detector Crossed",
677 payload.Data.SimpleItem[0].Value,
678 EntityCategory.DIAGNOSTIC,
680 except (AttributeError, KeyError):
684 @PARSERS.register("tns1:RuleEngine/CountAggregation/Counter")
686 """Handle parsing event message.
688 Topic: tns1:RuleEngine/CountAggregation/Counter
695 for source
in payload.Source.SimpleItem:
696 if source.Name ==
"VideoSourceConfigurationToken":
698 if source.Name ==
"VideoAnalyticsConfigurationToken":
699 video_analytics = source.Value
700 if source.Name ==
"Rule":
704 f
"{uid}_{topic}_{video_source}_{video_analytics}_{rule}",
705 "Count Aggregation Counter",
709 payload.Data.SimpleItem[0].Value,
710 EntityCategory.DIAGNOSTIC,
712 except (AttributeError, KeyError):
716 @PARSERS.register("tns1:UserAlarm/IVA/HumanShapeDetect")
718 """Handle parsing event message.
720 Topic: tns1:UserAlarm/IVA/HumanShapeDetect
725 for source
in payload.Source.SimpleItem:
726 if source.Name ==
"VideoSourceConfigurationToken":
731 f
"{uid}_{topic}_{video_source}",
732 "Human Shape Detect",
736 payload.Data.SimpleItem[0].Value ==
"true",
738 except (AttributeError, KeyError):
tuple[str, Any] extract_message(Any msg)
Event|None async_parse_last_reset(str uid, msg)
Event|None async_parse_linedetector_crossed(str uid, msg)
Event|None async_parse_image_too_dark(str uid, msg)
Event|None async_parse_visitor_detector(str uid, msg)
Event|None async_parse_count_aggregation_counter(str uid, msg)
Event|None async_parse_detected_sound(str uid, msg)
Event|None async_parse_jobstate(str uid, msg)
Event|None async_parse_relay(str uid, msg)
Event|None async_parse_cell_motion_detector(str uid, msg)
Event|None async_parse_digital_input(str uid, msg)
Event|None async_parse_vehicle_detector(str uid, msg)
Event|None async_parse_last_clock_sync(str uid, msg)
Event|None async_parse_face_detector(str uid, msg)
Event|None async_parse_tamper_detector(str uid, msg)
Event|None async_parse_human_shape_detect(str uid, msg)
Event|None async_parse_storage_failure(str uid, msg)
Event|None async_parse_dog_cat_detector(str uid, msg)
Event|None async_parse_person_detector(str uid, msg)
Event|None async_parse_field_detector(str uid, msg)
Event|None async_parse_processor_usage(str uid, msg)
Event|None async_parse_image_too_blurry(str uid, msg)
datetime.datetime|None local_datetime_or_none(str value)
Event|None async_parse_scene_change(str uid, msg)
Event|None async_parse_motion_alarm(str uid, msg)
Event|None async_parse_last_reboot(str uid, msg)
Event|None async_parse_motion_region_detector(str uid, msg)
Event|None async_parse_backup_last(str uid, msg)
str _normalize_video_source(str source)
Event|None async_parse_image_too_bright(str uid, msg)