55import subprocess
66from tempfile import NamedTemporaryFile
77
8- import voluptuous as vol
9-
10- from homeassistant .components .camera import PLATFORM_SCHEMA , Camera
8+ from homeassistant .components .camera import Camera
119from homeassistant .const import CONF_FILE_PATH , CONF_NAME , EVENT_HOMEASSISTANT_STOP
12- from homeassistant .helpers import config_validation as cv
13-
14- _LOGGER = logging .getLogger (__name__ )
1510
16- CONF_HORIZONTAL_FLIP = "horizontal_flip"
17- CONF_IMAGE_HEIGHT = "image_height"
18- CONF_IMAGE_QUALITY = "image_quality"
19- CONF_IMAGE_ROTATION = "image_rotation"
20- CONF_IMAGE_WIDTH = "image_width"
21- CONF_TIMELAPSE = "timelapse"
22- CONF_VERTICAL_FLIP = "vertical_flip"
23-
24- DEFAULT_HORIZONTAL_FLIP = 0
25- DEFAULT_IMAGE_HEIGHT = 480
26- DEFAULT_IMAGE_QUALITY = 7
27- DEFAULT_IMAGE_ROTATION = 0
28- DEFAULT_IMAGE_WIDTH = 640
29- DEFAULT_NAME = "Raspberry Pi Camera"
30- DEFAULT_TIMELAPSE = 1000
31- DEFAULT_VERTICAL_FLIP = 0
32-
33- PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
34- {
35- vol .Optional (CONF_FILE_PATH ): cv .isfile ,
36- vol .Optional (CONF_HORIZONTAL_FLIP , default = DEFAULT_HORIZONTAL_FLIP ): vol .All (
37- vol .Coerce (int ), vol .Range (min = 0 , max = 1 )
38- ),
39- vol .Optional (CONF_IMAGE_HEIGHT , default = DEFAULT_IMAGE_HEIGHT ): vol .Coerce (int ),
40- vol .Optional (CONF_IMAGE_QUALITY , default = DEFAULT_IMAGE_QUALITY ): vol .All (
41- vol .Coerce (int ), vol .Range (min = 0 , max = 100 )
42- ),
43- vol .Optional (CONF_IMAGE_ROTATION , default = DEFAULT_IMAGE_ROTATION ): vol .All (
44- vol .Coerce (int ), vol .Range (min = 0 , max = 359 )
45- ),
46- vol .Optional (CONF_IMAGE_WIDTH , default = DEFAULT_IMAGE_WIDTH ): vol .Coerce (int ),
47- vol .Optional (CONF_NAME , default = DEFAULT_NAME ): cv .string ,
48- vol .Optional (CONF_TIMELAPSE , default = 1000 ): vol .Coerce (int ),
49- vol .Optional (CONF_VERTICAL_FLIP , default = DEFAULT_VERTICAL_FLIP ): vol .All (
50- vol .Coerce (int ), vol .Range (min = 0 , max = 1 )
51- ),
52- }
11+ from .const import (
12+ CONF_HORIZONTAL_FLIP ,
13+ CONF_IMAGE_HEIGHT ,
14+ CONF_IMAGE_QUALITY ,
15+ CONF_IMAGE_ROTATION ,
16+ CONF_IMAGE_WIDTH ,
17+ CONF_OVERLAY_METADATA ,
18+ CONF_OVERLAY_TIMESTAMP ,
19+ CONF_TIMELAPSE ,
20+ CONF_VERTICAL_FLIP ,
21+ DOMAIN ,
5322)
5423
24+ _LOGGER = logging .getLogger (__name__ )
25+
5526
5627def kill_raspistill (* args ):
5728 """Kill any previously running raspistill process.."""
@@ -62,24 +33,18 @@ def kill_raspistill(*args):
6233
6334def setup_platform (hass , config , add_entities , discovery_info = None ):
6435 """Set up the Raspberry Camera."""
36+ # We only want this platform to be set up via discovery.
37+ # prevent initializing by erroneous platform config section in yaml conf
38+ if discovery_info is None :
39+ return
40+
6541 if shutil .which ("raspistill" ) is None :
6642 _LOGGER .error ("'raspistill' was not found" )
67- return False
68-
69- setup_config = {
70- CONF_NAME : config .get (CONF_NAME ),
71- CONF_IMAGE_WIDTH : config .get (CONF_IMAGE_WIDTH ),
72- CONF_IMAGE_HEIGHT : config .get (CONF_IMAGE_HEIGHT ),
73- CONF_IMAGE_QUALITY : config .get (CONF_IMAGE_QUALITY ),
74- CONF_IMAGE_ROTATION : config .get (CONF_IMAGE_ROTATION ),
75- CONF_TIMELAPSE : config .get (CONF_TIMELAPSE ),
76- CONF_HORIZONTAL_FLIP : config .get (CONF_HORIZONTAL_FLIP ),
77- CONF_VERTICAL_FLIP : config .get (CONF_VERTICAL_FLIP ),
78- CONF_FILE_PATH : config .get (CONF_FILE_PATH ),
79- }
43+ return
8044
8145 hass .bus .listen_once (EVENT_HOMEASSISTANT_STOP , kill_raspistill )
8246
47+ setup_config = hass .data [DOMAIN ]
8348 file_path = setup_config [CONF_FILE_PATH ]
8449
8550 def delete_temp_file (* args ):
@@ -100,7 +65,7 @@ def delete_temp_file(*args):
10065 # Check whether the file path has been whitelisted
10166 elif not hass .config .is_allowed_path (file_path ):
10267 _LOGGER .error ("'%s' is not a whitelisted directory" , file_path )
103- return False
68+ return
10469
10570 add_entities ([RaspberryCamera (setup_config )])
10671
@@ -142,6 +107,16 @@ def __init__(self, device_info):
142107 if device_info [CONF_VERTICAL_FLIP ]:
143108 cmd_args .append ("-vf" )
144109
110+ if device_info [CONF_OVERLAY_METADATA ]:
111+ cmd_args .append ("-a" )
112+ cmd_args .append (str (device_info [CONF_OVERLAY_METADATA ]))
113+
114+ if device_info [CONF_OVERLAY_TIMESTAMP ]:
115+ cmd_args .append ("-a" )
116+ cmd_args .append ("4" )
117+ cmd_args .append ("-a" )
118+ cmd_args .append (str (device_info [CONF_OVERLAY_TIMESTAMP ]))
119+
145120 subprocess .Popen (cmd_args , stdout = subprocess .DEVNULL , stderr = subprocess .STDOUT )
146121
147122 def camera_image (self ):
@@ -153,3 +128,8 @@ def camera_image(self):
153128 def name (self ):
154129 """Return the name of this camera."""
155130 return self ._name
131+
132+ @property
133+ def frame_interval (self ):
134+ """Return the interval between frames of the stream."""
135+ return self ._config [CONF_TIMELAPSE ] / 1000
0 commit comments