-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.py
748 lines (660 loc) · 27.4 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
"""
Support for ONVIF Cameras with FFmpeg as decoder.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/camera.onvif/
"""
import asyncio
import datetime as dt
import logging
import os
from aiohttp.client_exceptions import ClientConnectionError, ServerDisconnectedError
from haffmpeg.camera import CameraMjpeg
from haffmpeg.tools import IMAGE_JPEG, ImageFrame
import onvif
from onvif import ONVIFCamera, exceptions
import voluptuous as vol
from zeep.exceptions import Fault
import homeassistant.components.persistent_notification as pn
from homeassistant.components.camera import PLATFORM_SCHEMA, SUPPORT_STREAM, Camera
from homeassistant.components.camera.const import DOMAIN
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS, DATA_FFMPEG
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
)
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.service import async_extract_entity_ids
import homeassistant.util.dt as dt_util
from .const import (
ABSOLUTE_MOVE,
ATTR_CONTINUOUS_DURATION,
ATTR_DISTANCE,
ATTR_MOVE_MODE,
ATTR_PAN,
ATTR_PRESET_NAME,
ATTR_PRESET_OPERATION,
ATTR_PRESET_TOKEN,
ATTR_PTZ_VECTOR,
ATTR_SPEED,
ATTR_SPEED_VECTOR,
ATTR_TILT,
ATTR_ZOOM,
CONF_PROFILE_IDX,
CONF_RTSP_TRANSPORT,
CONF_CONTINUOUS_TIMEOUT_COMPLIANCE,
CONTINUOUS_MOVE,
DEFAULT_ARGUMENTS,
DEFAULT_NAME,
DEFAULT_PASSWORD,
DEFAULT_PORT,
DEFAULT_PROFILE_IDX,
DEFAULT_USERNAME,
DIR_DOWN,
DIR_LEFT,
DIR_RIGHT,
DIR_UP,
ENTITIES,
GET_PRESETS,
GOTO_HOME,
GOTO_PRESET,
ONVIF_DATA,
PTZ_NONE,
RELATIVE_MOVE,
RTSP_TRANSPORT_HTTP,
RTSP_TRANSPORT_RTSP,
RTSP_TRANSPORT_UDP,
SERVICE_ONVIF_CMD_REBOOT,
SERVICE_PTZ_MOVE,
SERVICE_PTZ_ADVANCED_MOVE,
SERVICE_PTZ_PRESET,
SET_HOME,
SET_PRESET,
STOP_MOVE,
ZOOM_IN,
ZOOM_OUT,
)
_LOGGER = logging.getLogger(__name__)
DOMAIN = "onvif"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
vol.Optional(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
CONF_RTSP_TRANSPORT: vol.In(
[RTSP_TRANSPORT_UDP, RTSP_TRANSPORT_HTTP, RTSP_TRANSPORT_RTSP]
),
vol.Optional(CONF_EXTRA_ARGUMENTS, default=DEFAULT_ARGUMENTS): cv.string,
vol.Optional(CONF_PROFILE_IDX, default=DEFAULT_PROFILE_IDX): vol.All(
vol.Coerce(int), vol.Range(min=0)
),
vol.Optional(CONF_CONTINUOUS_TIMEOUT_COMPLIANCE, default=True): cv.boolean,
}
)
SERVICE_PTZ_MOVE_SCHEMA = vol.Schema(
{
ATTR_ENTITY_ID: cv.entity_ids,
vol.Optional(ATTR_PAN, default=PTZ_NONE): vol.In([DIR_LEFT, DIR_RIGHT, PTZ_NONE]),
vol.Optional(ATTR_TILT, default=PTZ_NONE): vol.In([DIR_UP, DIR_DOWN, PTZ_NONE]),
vol.Optional(ATTR_ZOOM, default=PTZ_NONE): vol.In([ZOOM_OUT, ZOOM_IN, PTZ_NONE]),
ATTR_MOVE_MODE: vol.In(
[CONTINUOUS_MOVE, RELATIVE_MOVE, ABSOLUTE_MOVE,STOP_MOVE]
),
vol.Optional(ATTR_CONTINUOUS_DURATION, default=0): cv.small_float,
vol.Optional(ATTR_DISTANCE, default=0.1): cv.small_float,
vol.Optional(ATTR_SPEED, default=0.5): cv.small_float,
}
)
SERVICE_PTZ_ADVANCED_MOVE_SCHEMA = vol.Schema(
{
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_MOVE_MODE: vol.In(
[CONTINUOUS_MOVE, RELATIVE_MOVE, ABSOLUTE_MOVE, STOP_MOVE]
),
vol.Required(ATTR_PTZ_VECTOR): vol.All(
vol.ExactSequence((cv.string, cv.string, cv.string)), vol.Coerce(tuple)
),
vol.Optional(ATTR_SPEED_VECTOR, default=("1.0", "1.0", "1.0")): vol.All(
vol.ExactSequence((cv.string, cv.string, cv.string)), vol.Coerce(tuple)
),
vol.Optional(ATTR_CONTINUOUS_DURATION, default=0): cv.small_float
}
)
SERVICE_PTZ_PRESET_SCHEMA = vol.Schema(
{
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_PRESET_OPERATION: vol.In(
[GET_PRESETS, SET_PRESET, GOTO_PRESET, GOTO_HOME, SET_HOME]
),
vol.Optional(ATTR_PRESET_NAME, default=""): cv.string,
vol.Optional(ATTR_PRESET_TOKEN, default=""): cv.string,
}
)
SERVICE_ONVIF_REBOOT_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up a ONVIF camera."""
_LOGGER.debug("Setting up the ONVIF camera platform")
async def async_handle_ptz_move(service):
"""Handle PTZ Move service call."""
pan = service.data[ATTR_PAN]
tilt = service.data[ATTR_TILT]
zoom = service.data[ATTR_ZOOM]
distance = service.data[ATTR_DISTANCE]
speed = service.data[ATTR_SPEED]
move_mode = service.data[ATTR_MOVE_MODE]
continuous_timeout = service.data[ATTR_CONTINUOUS_DURATION]
timeout_compliance = config[CONF_CONTINUOUS_TIMEOUT_COMPLIANCE]
all_cameras = hass.data[ONVIF_DATA][ENTITIES]
entity_ids = service.data[ATTR_ENTITY_ID]
target_cameras = []
target_cameras = [
camera for camera in all_cameras if camera.entity_id in entity_ids
]
for camera in target_cameras:
await camera.async_perform_ptz_move(
pan, tilt, zoom, distance, speed, move_mode, continuous_timeout, timeout_compliance
)
async def async_handle_ptz_advanced_move(service):
"""Handle PTZ Move service call."""
ptz_vector = service.data[ATTR_PTZ_VECTOR]
speed_vector = service.data[ATTR_SPEED_VECTOR]
move_mode = service.data[ATTR_MOVE_MODE]
continuous_timeout = service.data[ATTR_CONTINUOUS_DURATION]
timeout_compliance = config[CONF_CONTINUOUS_TIMEOUT_COMPLIANCE]
all_cameras = hass.data[ONVIF_DATA][ENTITIES]
entity_ids = service.data[ATTR_ENTITY_ID]
target_cameras = []
target_cameras = [
camera for camera in all_cameras if camera.entity_id in entity_ids
]
for camera in target_cameras:
await camera.async_perform_ptz_advanced_move(
ptz_vector, speed_vector, move_mode, continuous_timeout,timeout_compliance
)
async def async_handle_ptz_preset(service):
"""Handle PTZ Preset service call."""
preset_operation = service.data[ATTR_PRESET_OPERATION]
preset_name = service.data[ATTR_PRESET_NAME]
preset_token = service.data[ATTR_PRESET_TOKEN]
all_cameras = hass.data[ONVIF_DATA][ENTITIES]
entity_ids = await async_extract_entity_ids(hass, service)
target_cameras = []
target_cameras = [
camera for camera in all_cameras if camera.entity_id in entity_ids
]
for camera in target_cameras:
await camera.async_perform_ptz_preset(
preset_operation, preset_name, preset_token
)
async def async_handle_reboot(service):
"""Handle ONVIF Reboot service call."""
all_cameras = hass.data[ONVIF_DATA][ENTITIES]
entity_ids = await async_extract_entity_ids(hass, service)
target_cameras = []
target_cameras = [
camera for camera in all_cameras if camera.entity_id in entity_ids
]
for camera in target_cameras:
await camera.async_perform_reboot()
hass.services.async_register(
DOMAIN, SERVICE_PTZ_MOVE, async_handle_ptz_move, schema=SERVICE_PTZ_MOVE_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_PTZ_ADVANCED_MOVE, async_handle_ptz_advanced_move, schema=SERVICE_PTZ_ADVANCED_MOVE_SCHEMA
)
hass.services.async_register(
DOMAIN,
SERVICE_PTZ_PRESET,
async_handle_ptz_preset,
schema=SERVICE_PTZ_PRESET_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_ONVIF_CMD_REBOOT,
async_handle_reboot,
schema=SERVICE_ONVIF_REBOOT_SCHEMA,
)
_LOGGER.debug("Constructing the ONVIFHassCamera")
hass_camera = ONVIFHassCamera(hass, config)
await hass_camera.async_initialize()
async_add_entities([hass_camera])
return
class ONVIFHassCamera(Camera):
"""An implementation of an ONVIF camera."""
def __init__(self, hass, config):
"""Initialize an ONVIF camera."""
super().__init__()
_LOGGER.debug("Importing dependencies")
_LOGGER.debug("Setting up the ONVIF camera component")
self._username = config.get(CONF_USERNAME)
self._password = config.get(CONF_PASSWORD)
self._host = config.get(CONF_HOST)
self._port = config.get(CONF_PORT)
self._name = config.get(CONF_NAME)
self._ffmpeg_arguments = config.get(CONF_EXTRA_ARGUMENTS)
self._profile_index = config.get(CONF_PROFILE_IDX)
self._rtsp_transport = config.get(CONF_RTSP_TRANSPORT)
self._media_service = None
self._ptz_service = None
self._image_service = None
self._input_uri = None
self._input_uri_for_log = None
self._profile_token = None
self._profiles = None
self._ptz_opt = None
self._ptz_presets = None
_LOGGER.debug(
"Setting up the ONVIF camera device @ '%s:%s'", self._host, self._port
)
self._camera = ONVIFCamera(
self._host,
self._port,
self._username,
self._password,
"{}/wsdl/".format(os.path.dirname(onvif.__file__)),
)
async def async_initialize(self):
"""
Initialize the camera.
Initializes the camera by obtaining the input uri and connecting to
the camera. Also retrieves the ONVIF profiles.
"""
try:
_LOGGER.debug("Updating service addresses")
await self._camera.update_xaddrs()
await self.async_check_date_and_time()
self._media_service = await self.async_obtain_media_service()
self._profiles = await self.async_obtain_profiles()
self._profile_token = self.index_to_profile_token()
await self.async_obtain_input_uri()
self._ptz_service = await self.async_obtain_ptz_service()
except ClientConnectionError as err:
_LOGGER.warning(
"Couldn't connect to camera '%s', but will retry later. Error: %s",
self._name,
err,
)
raise PlatformNotReady
except Fault as err:
_LOGGER.error(
"Couldn't connect to camera '%s', please verify "
"that the credentials are correct. Error: %s",
self._name,
err,
)
async def async_check_date_and_time(self):
"""Warns if camera and system date not synced."""
_LOGGER.debug("Setting up the ONVIF device management service")
devicemgmt = self._camera.create_devicemgmt_service()
_LOGGER.debug("Retrieving current camera date/time")
try:
system_date = dt_util.utcnow()
device_time = await devicemgmt.GetSystemDateAndTime()
if not device_time:
_LOGGER.debug(
"""Couldn't get camera '%s' date/time.
GetSystemDateAndTime() return null/empty""",
self._name,
)
return
if device_time.UTCDateTime:
tzone = dt_util.UTC
cdate = device_time.UTCDateTime
else:
tzone = (
dt_util.get_time_zone(device_time.TimeZone)
or dt_util.DEFAULT_TIME_ZONE
)
cdate = device_time.LocalDateTime
if cdate is None:
_LOGGER.warning("Could not retrieve date/time on this camera")
else:
cam_date = dt.datetime(
cdate.Date.Year,
cdate.Date.Month,
cdate.Date.Day,
cdate.Time.Hour,
cdate.Time.Minute,
cdate.Time.Second,
0,
tzone,
)
cam_date_utc = cam_date.astimezone(dt_util.UTC)
_LOGGER.debug("TimeZone for date/time: %s", tzone)
_LOGGER.debug("Camera date/time: %s", cam_date)
_LOGGER.debug("Camera date/time in UTC: %s", cam_date_utc)
_LOGGER.debug("System date/time: %s", system_date)
dt_diff = cam_date - system_date
dt_diff_seconds = dt_diff.total_seconds()
if dt_diff_seconds > 5:
_LOGGER.warning(
"The date/time on the camera (UTC) is '%s', "
"which is different from the system '%s', "
"this could lead to authentication issues",
cam_date_utc,
system_date,
)
except ServerDisconnectedError as err:
_LOGGER.warning(
"Couldn't get camera '%s' date/time. Error: %s", self._name, err
)
async def async_obtain_profiles(self):
"""Obtain onvif profiles object."""
try:
__profiles = await self._media_service.GetProfiles()
_LOGGER.debug("Retrieved '%d' profiles", len(__profiles))
return __profiles
except exceptions.ONVIFError as err:
_LOGGER.error(
"Couldn't retrieve profiles of camera '%s'. Error: %s", self._name, err,
)
return None
async def async_obtain_media_service(self):
"""Obtain onvif profiles object."""
try:
_LOGGER.debug(
"Connecting with ONVIF Camera: %s on port %s", self._host, self._port
)
__media_service = self._camera.create_media_service()
return __media_service
except exceptions.ONVIFError as err:
_LOGGER.error(
"Couldn't retrieve media_service of camera '%s'. Error: %s",
self._name,
err,
)
return None
def index_to_profile_token(self):
"""Return token name from a index over profiles object."""
if self._profile_index >= len(self._profiles):
_LOGGER.warning(
"ONVIF Camera '%s' doesn't provide profile %d."
" Using the last profile.",
self._name,
self._profile_index,
)
self._profile_index = -1
_LOGGER.debug("Using profile index '%d'", self._profile_index)
return self._profiles[self._profile_index].token
async def async_obtain_input_uri(self):
"""Set the input uri for the camera."""
_LOGGER.debug("Retrieving stream uri")
# Fix Onvif setup error on Goke GK7102 based IP camera #26781
# Assume not buggy camera and see if ClientConnectionError
# reload fresh self._media_service before one retry
for i in range(0, 2):
try:
req = self._media_service.create_type("GetStreamUri")
req.ProfileToken = self._profile_token
req.StreamSetup = {
"Stream": "RTP-Unicast",
"Transport": {"Protocol": self._rtsp_transport},
}
stream_uri = await self._media_service.GetStreamUri(req)
uri_no_auth = stream_uri.Uri
self._input_uri_for_log = uri_no_auth.replace(
"rtsp://", "rtsp://<user>:<password>@", 1
)
self._input_uri = uri_no_auth.replace(
"rtsp://", "rtsp://%s:%s@" % (self._username, self._password), 1
)
_LOGGER.debug(
"ONVIF Camera Using the following URL for %s: %s",
self._name,
self._input_uri_for_log,
)
break
except ClientConnectionError as err:
if i == 0:
_LOGGER.info(
"GetStreamUri on '%s'. Error: %s. Trying a workaround against known issue(#26781)",
self._name,
err,
)
self._media_service = self._camera.create_media_service()
pass
else:
_LOGGER.error(
"Couldn't setup camera '%s'. Error: %s", self._name, err
)
return (None, None)
async def async_obtain_ptz_service(self):
"""Set up PTZ service if available."""
_LOGGER.debug("Setting up the ONVIF PTZ service")
if self._camera.get_service("ptz") is None:
_LOGGER.debug("PTZ is not available")
return None
else:
_LOGGER.debug("Completed set up of the ONVIF camera component")
return self._camera.create_ptz_service()
async def async_perform_ptz_move(
self, pan, tilt, zoom, distance, speed, move_mode, continuous_timeout, timeout_compliance
):
"""Perform legacy PTZ actions on the camera + new move_modes"""
pan_val = (
distance if pan == DIR_RIGHT else -distance if pan == DIR_LEFT else 0
)
tilt_val = (
distance if tilt == DIR_UP else -distance if tilt == DIR_DOWN else 0
)
zoom_val = (
distance if zoom == ZOOM_IN else -distance if zoom == ZOOM_OUT else 0
)
speed_val = speed
await self.async_perform_ptz_advanced_move( (pan_val, tilt_val, zoom_val), (speed_val,speed_val,speed_val),move_mode,continuous_timeout,timeout_compliance )
async def async_perform_ptz_advanced_move(
self, ptz_vector, speed_vector, move_mode, continuous_timeout, timeout_compliance
):
"""Perform a PTZ action on the camera."""
_LOGGER.debug("async_perform_ptz_advanced_move")
if self._ptz_service is None:
_LOGGER.warning(
"PTZ Move actions are not supported on camera '%s'", self._name
)
return
if self._ptz_service:
pan_val = float(ptz_vector[0])
tilt_val = float(ptz_vector[1])
zoom_val = float(ptz_vector[2])
_LOGGER.debug(
"Calling %s PTZ Move on camera '%s'| Pan = %4.2f | Tilt = %4.2f | Zoom = %4.2f | Speed = %s | Timeout = %1.1f",
move_mode,
self._name,
pan_val,
tilt_val,
zoom_val,
speed_vector,
continuous_timeout
)
try:
req = self._ptz_service.create_type(move_mode)
req.ProfileToken = self._profile_token
if move_mode == CONTINUOUS_MOVE:
req.Velocity = {
"PanTilt": {"x": pan_val, "y": tilt_val},
"Zoom": {"x": zoom_val},
}
if continuous_timeout != 0:
req.Timeout = dt.timedelta(0, 0, continuous_timeout * 1000000)
await self._ptz_service.ContinuousMove(req)
if continuous_timeout != 0 and not timeout_compliance:
await asyncio.sleep(continuous_timeout)
req = self._ptz_service.create_type("Stop")
req.ProfileToken = self._profile_token
await self._ptz_service.Stop(req)
elif move_mode == STOP_MOVE:
req = self._ptz_service.create_type("Stop")
req.ProfileToken = self._profile_token
await self._ptz_service.Stop(req)
elif move_mode == RELATIVE_MOVE:
req.Translation = {
"PanTilt": {"x": pan_val, "y": tilt_val},
"Zoom": {"x": zoom_val},
}
req.Speed = {
"PanTilt": {"x": float(speed_vector[0]), "y": float(speed_vector[1])},
"Zoom": {"x": float(speed_vector[2])},
}
await self._ptz_service.RelativeMove(req)
elif move_mode == ABSOLUTE_MOVE:
req.Position = {
"PanTilt": {"x": pan_val, "y": tilt_val},
"Zoom": {"x": zoom_val},
}
req.Speed = {
"PanTilt": {"x": float(speed_vector[0]), "y": float(speed_vector[1])},
"Zoom": {"x": float(speed_vector[2])},
}
await self._ptz_service.AbsoluteMove(req)
except exceptions.ONVIFError as err:
if "Bad Request" in err.reason:
self._ptz_service = None
_LOGGER.debug("Camera '%s' doesn't support PTZ.", self._name)
else:
_LOGGER.debug("Camera '%s' doesn't support PTZ.", self._name)
async def async_perform_ptz_preset(
self, preset_operation, preset_name, preset_token
):
"""Perform a PTZ Preset action on the camera."""
if self._ptz_service:
if preset_operation in (
GOTO_HOME,
SET_HOME,
GOTO_PRESET,
SET_PRESET,
GET_PRESETS,
):
try:
_LOGGER.debug("Retrieved PTZ presets")
req = self._ptz_service.create_type(GET_PRESETS)
req.ProfileToken = self._profile_token
__presets = await self._ptz_service.GetPresets(req)
_LOGGER.debug(
"Calling PTZ preset| Operation = %s | PresetName = %s | PresetToken = %s",
preset_operation,
preset_name,
preset_token,
)
req = self._ptz_service.create_type(preset_operation)
req.ProfileToken = self._profile_token
if preset_operation == GOTO_PRESET:
preset_token = next(
(
preset["token"]
for preset in __presets
if preset["Name"] == preset_name
),
None,
)
_LOGGER.debug(
"PresetToken from PresetName | PresetName = %s | PresetToken = %s",
preset_name,
preset_token,
)
req.PresetToken = "%s" % preset_token
req.Speed = {
"PanTilt": {"x": 1.0, "y": 1.0},
"Zoom": {"x": 1.0},
}
await self._ptz_service.GotoPreset(req)
if preset_operation == SET_PRESET:
req.PresetToken = preset_token
req.PresetName = preset_name
await self._ptz_service.SetPreset(req)
if preset_operation == GET_PRESETS:
presets = []
if __presets is not None:
for preset in __presets:
presets.append(preset["Name"])
pn.create(self.hass, "\n".join(presets), title="Onvif PTZ Presets")
if preset_operation == GOTO_HOME:
await self._ptz_service.GotoHomePosition(req)
if preset_operation == SET_HOME:
await self._ptz_service.SetHomePosition(req)
except exceptions.ONVIFError as err:
if "Bad Request" in err.reason:
_LOGGER.error(
"Camera '%s' doesn't support PTZ %s operation.",
self._name,
preset_operation,
)
except Exception as err:
_LOGGER.info(
"Camera '%s' PTZ %s operation failed with that reason: %s",
self._name,
preset_operation,
err,
)
else:
_LOGGER.debug("PTZ %s operation is not implemented", preset_operation)
else:
self._ptz_service = None
_LOGGER.debug("Camera '%s' doesn't support PTZ.", self._name)
async def async_perform_reboot(self):
"""Perform a SystemReboot action on the camera."""
try:
_LOGGER.debug("Calling SystemReboot")
ret = await self._camera.devicemgmt.SystemReboot()
_LOGGER.debug("Camera '%s' Reboot command returned '%s'", self._name, ret)
except exceptions.ONVIFError as err:
_LOGGER.error(
"Couldn't reboot the camera '%s', please verify "
"that the camera supports the command. Error: %s",
self._name,
err,
)
async def async_added_to_hass(self):
"""Handle entity addition to hass."""
_LOGGER.debug("Camera '%s' added to hass", self._name)
if ONVIF_DATA not in self.hass.data:
self.hass.data[ONVIF_DATA] = {}
self.hass.data[ONVIF_DATA][ENTITIES] = []
self.hass.data[ONVIF_DATA][ENTITIES].append(self)
async def async_camera_image(self):
"""Return a still image response from the camera."""
_LOGGER.debug("Retrieving image from camera '%s'", self._name)
ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary, loop=self.hass.loop)
image = await asyncio.shield(
ffmpeg.get_image(
self._input_uri,
output_format=IMAGE_JPEG,
extra_cmd=self._ffmpeg_arguments,
)
)
return image
async def handle_async_mjpeg_stream(self, request):
"""Generate an HTTP MJPEG stream from the camera."""
_LOGGER.debug("Handling mjpeg stream from camera '%s'", self._name)
ffmpeg_manager = self.hass.data[DATA_FFMPEG]
stream = CameraMjpeg(ffmpeg_manager.binary, loop=self.hass.loop)
await stream.open_camera(self._input_uri, extra_cmd=self._ffmpeg_arguments)
try:
stream_reader = await stream.get_reader()
return await async_aiohttp_proxy_stream(
self.hass,
request,
stream_reader,
ffmpeg_manager.ffmpeg_stream_content_type,
)
finally:
await stream.close()
@property
def supported_features(self):
"""Return supported features."""
if self._input_uri:
return SUPPORT_STREAM
return 0
async def stream_source(self):
"""Return the stream source."""
return self._input_uri
@property
def name(self):
"""Return the name of this camera."""
return self._name