From bf915cf3b4fff16790a99768b09d2a2fdb334bf0 Mon Sep 17 00:00:00 2001 From: Luca Soldi Date: Tue, 22 Mar 2016 08:33:56 +0100 Subject: [PATCH 1/5] Added Raspberry Pi Camera Component --- .../components/camera/raspberry_camera.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 homeassistant/components/camera/raspberry_camera.py diff --git a/homeassistant/components/camera/raspberry_camera.py b/homeassistant/components/camera/raspberry_camera.py new file mode 100644 index 00000000000..d3eb547c432 --- /dev/null +++ b/homeassistant/components/camera/raspberry_camera.py @@ -0,0 +1,70 @@ +"""Camera platform that has a Raspberry Pi camera.""" + +import os +import subprocess +import logging + +from homeassistant.components.camera import Camera + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the Raspberry Camera.""" + add_devices([ + RaspberryCamera(config) + ]) + + +class RaspberryCamera(Camera): + """Raspberry Pi camera.""" + + def __init__(self, device_info): + """Initialize Raspberry Pi camera component.""" + super().__init__() + + self._name = device_info.get("name", "Raspberry Pi Camera") + image_width = int(device_info.get("image_width", "640")) + image_height = int(device_info.get("image_height", "480")) + image_quality = int(device_info.get("image_quality", "7")) + image_rotation = int(device_info.get("image_rotation", "0")) + timelapse = int(device_info.get("timelapse", "1000")) + + horizontal_flip_arg = "" + horizontal_flip = int(device_info.get("horizontal_flip", "0")) + if horizontal_flip: + horizontal_flip_arg = " -hf " + + vertical_flip_arg = "" + vertical_flip = int(device_info.get("vertical_flip", "0")) + if vertical_flip: + vertical_flip_arg = " -vf" + + image_path = os.path.join(os.path.dirname(__file__), + 'image.jpg') + + # kill if there's raspistill instance + cmd = 'killall raspistill' + subprocess.call(cmd, shell=True) + # start new instance of raspistill + cmd = ('raspistill --nopreview -o ' + image_path + + ' -t 0 ' + '-w ' + str(image_width) + ' -h ' + + str(image_height) + ' -tl ' + str(timelapse) + ' -q ' + + str(image_quality) + str(horizontal_flip_arg) + + str(vertical_flip_arg) + ' -rot ' + str(image_rotation) + + '&') + + subprocess.call(cmd, shell=True) + + def camera_image(self): + """Return raspstill image response.""" + image_path = os.path.join(os.path.dirname(__file__), + 'image.jpg') + + with open(image_path, 'rb') as file: + return file.read() + + @property + def name(self): + """Return the name of this camera.""" + return self._name From 65ef836313cfff3a4f14b4fc802b8c457b7c75f6 Mon Sep 17 00:00:00 2001 From: Luca Soldi Date: Tue, 22 Mar 2016 22:35:09 +0100 Subject: [PATCH 2/5] Check raspistill exisance and check parameters in setup_platform --- .Python | 1 + .../components/camera/raspberry_camera.py | 62 ++++++++++++------- include/python2.7 | 1 + 3 files changed, 41 insertions(+), 23 deletions(-) create mode 120000 .Python create mode 120000 include/python2.7 diff --git a/.Python b/.Python new file mode 120000 index 00000000000..cc24a1e924a --- /dev/null +++ b/.Python @@ -0,0 +1 @@ +/System/Library/Frameworks/Python.framework/Versions/2.7/Python \ No newline at end of file diff --git a/homeassistant/components/camera/raspberry_camera.py b/homeassistant/components/camera/raspberry_camera.py index d3eb547c432..4f7a417d095 100644 --- a/homeassistant/components/camera/raspberry_camera.py +++ b/homeassistant/components/camera/raspberry_camera.py @@ -3,6 +3,7 @@ import os import subprocess import logging +import shutil from homeassistant.components.camera import Camera @@ -11,8 +12,35 @@ _LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Raspberry Camera.""" + if shutil.which("raspistill") is None: + _LOGGER.error("Error: raspistill not found") + return None + + horizontal_flip_arg = "" + horizontal_flip = int(config.get("horizontal_flip", "0")) + if horizontal_flip: + horizontal_flip_arg = " -hf " + + vertical_flip_arg = "" + vertical_flip = int(config.get("vertical_flip", "0")) + if vertical_flip: + vertical_flip_arg = " -vf" + + setup_config = ( + { + "name": config.get("name", "Raspberry Pi Camera"), + "image_width": int(config.get("image_width", "640")), + "image_height": int(config.get("image_height", "480")), + "image_quality": int(config.get("image_quality", "7")), + "image_rotation": int(config.get("image_rotation", "0")), + "timelapse": int(config.get("timelapse", "1000")), + "horizontal_flip_arg": horizontal_flip_arg, + "vertical_flip_arg": vertical_flip_arg + } + ) + add_devices([ - RaspberryCamera(config) + RaspberryCamera(setup_config) ]) @@ -23,22 +51,7 @@ class RaspberryCamera(Camera): """Initialize Raspberry Pi camera component.""" super().__init__() - self._name = device_info.get("name", "Raspberry Pi Camera") - image_width = int(device_info.get("image_width", "640")) - image_height = int(device_info.get("image_height", "480")) - image_quality = int(device_info.get("image_quality", "7")) - image_rotation = int(device_info.get("image_rotation", "0")) - timelapse = int(device_info.get("timelapse", "1000")) - - horizontal_flip_arg = "" - horizontal_flip = int(device_info.get("horizontal_flip", "0")) - if horizontal_flip: - horizontal_flip_arg = " -hf " - - vertical_flip_arg = "" - vertical_flip = int(device_info.get("vertical_flip", "0")) - if vertical_flip: - vertical_flip_arg = " -vf" + self._name = device_info["name"] image_path = os.path.join(os.path.dirname(__file__), 'image.jpg') @@ -47,12 +60,15 @@ class RaspberryCamera(Camera): cmd = 'killall raspistill' subprocess.call(cmd, shell=True) # start new instance of raspistill - cmd = ('raspistill --nopreview -o ' + image_path + - ' -t 0 ' + '-w ' + str(image_width) + ' -h ' + - str(image_height) + ' -tl ' + str(timelapse) + ' -q ' + - str(image_quality) + str(horizontal_flip_arg) + - str(vertical_flip_arg) + ' -rot ' + str(image_rotation) + - '&') + cmd = ('raspistill --nopreview -o ' + str(image_path) + + ' -t 0 ' + '-w ' + str(device_info["image_width"]) + + ' -h ' + str(device_info["image_height"]) + + ' -tl ' + str(device_info["timelapse"]) + + ' -q ' + str(device_info["image_quality"]) + + str(device_info["horizontal_flip_arg"]) + + str(device_info["vertical_flip_arg"]) + + ' -rot ' + str(device_info["image_rotation"]) + + ' &') subprocess.call(cmd, shell=True) diff --git a/include/python2.7 b/include/python2.7 new file mode 120000 index 00000000000..3fe034fccc4 --- /dev/null +++ b/include/python2.7 @@ -0,0 +1 @@ +/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 \ No newline at end of file From 9584f2dab4a3d36003d0f93f39ba2f6700ac14a0 Mon Sep 17 00:00:00 2001 From: LucaSoldi Date: Wed, 23 Mar 2016 14:53:52 +0100 Subject: [PATCH 3/5] Delete .Python --- .Python | 1 - 1 file changed, 1 deletion(-) delete mode 120000 .Python diff --git a/.Python b/.Python deleted file mode 120000 index cc24a1e924a..00000000000 --- a/.Python +++ /dev/null @@ -1 +0,0 @@ -/System/Library/Frameworks/Python.framework/Versions/2.7/Python \ No newline at end of file From 800397dbe4659495e312e3da79fc957b4ba18443 Mon Sep 17 00:00:00 2001 From: LucaSoldi Date: Wed, 23 Mar 2016 14:55:32 +0100 Subject: [PATCH 4/5] Delete python2.7 --- include/python2.7 | 1 - 1 file changed, 1 deletion(-) delete mode 120000 include/python2.7 diff --git a/include/python2.7 b/include/python2.7 deleted file mode 120000 index 3fe034fccc4..00000000000 --- a/include/python2.7 +++ /dev/null @@ -1 +0,0 @@ -/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 \ No newline at end of file From d0503cc02191eb6fec84790b4f88831870932a98 Mon Sep 17 00:00:00 2001 From: Luca Soldi Date: Sun, 27 Mar 2016 20:49:04 +0200 Subject: [PATCH 5/5] Add feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit √ add file_path config √ changed subprocess call --- .../components/camera/raspberry_camera.py | 86 ------------------- homeassistant/components/camera/rpi_camera.py | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+), 86 deletions(-) delete mode 100644 homeassistant/components/camera/raspberry_camera.py create mode 100644 homeassistant/components/camera/rpi_camera.py diff --git a/homeassistant/components/camera/raspberry_camera.py b/homeassistant/components/camera/raspberry_camera.py deleted file mode 100644 index 4f7a417d095..00000000000 --- a/homeassistant/components/camera/raspberry_camera.py +++ /dev/null @@ -1,86 +0,0 @@ -"""Camera platform that has a Raspberry Pi camera.""" - -import os -import subprocess -import logging -import shutil - -from homeassistant.components.camera import Camera - -_LOGGER = logging.getLogger(__name__) - - -def setup_platform(hass, config, add_devices, discovery_info=None): - """Setup the Raspberry Camera.""" - if shutil.which("raspistill") is None: - _LOGGER.error("Error: raspistill not found") - return None - - horizontal_flip_arg = "" - horizontal_flip = int(config.get("horizontal_flip", "0")) - if horizontal_flip: - horizontal_flip_arg = " -hf " - - vertical_flip_arg = "" - vertical_flip = int(config.get("vertical_flip", "0")) - if vertical_flip: - vertical_flip_arg = " -vf" - - setup_config = ( - { - "name": config.get("name", "Raspberry Pi Camera"), - "image_width": int(config.get("image_width", "640")), - "image_height": int(config.get("image_height", "480")), - "image_quality": int(config.get("image_quality", "7")), - "image_rotation": int(config.get("image_rotation", "0")), - "timelapse": int(config.get("timelapse", "1000")), - "horizontal_flip_arg": horizontal_flip_arg, - "vertical_flip_arg": vertical_flip_arg - } - ) - - add_devices([ - RaspberryCamera(setup_config) - ]) - - -class RaspberryCamera(Camera): - """Raspberry Pi camera.""" - - def __init__(self, device_info): - """Initialize Raspberry Pi camera component.""" - super().__init__() - - self._name = device_info["name"] - - image_path = os.path.join(os.path.dirname(__file__), - 'image.jpg') - - # kill if there's raspistill instance - cmd = 'killall raspistill' - subprocess.call(cmd, shell=True) - # start new instance of raspistill - cmd = ('raspistill --nopreview -o ' + str(image_path) + - ' -t 0 ' + '-w ' + str(device_info["image_width"]) + - ' -h ' + str(device_info["image_height"]) + - ' -tl ' + str(device_info["timelapse"]) + - ' -q ' + str(device_info["image_quality"]) + - str(device_info["horizontal_flip_arg"]) + - str(device_info["vertical_flip_arg"]) + - ' -rot ' + str(device_info["image_rotation"]) + - ' &') - - subprocess.call(cmd, shell=True) - - def camera_image(self): - """Return raspstill image response.""" - image_path = os.path.join(os.path.dirname(__file__), - 'image.jpg') - - with open(image_path, 'rb') as file: - return file.read() - - @property - def name(self): - """Return the name of this camera.""" - return self._name diff --git a/homeassistant/components/camera/rpi_camera.py b/homeassistant/components/camera/rpi_camera.py new file mode 100644 index 00000000000..cda48d1ddfa --- /dev/null +++ b/homeassistant/components/camera/rpi_camera.py @@ -0,0 +1,86 @@ +"""Camera platform that has a Raspberry Pi camera.""" + +import os +import subprocess +import logging +import shutil + +from homeassistant.components.camera import Camera + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the Raspberry Camera.""" + if shutil.which("raspistill") is None: + _LOGGER.error("Error: raspistill not found") + return False + + setup_config = ( + { + "name": config.get("name", "Raspberry Pi Camera"), + "image_width": int(config.get("image_width", "640")), + "image_height": int(config.get("image_height", "480")), + "image_quality": int(config.get("image_quality", "7")), + "image_rotation": int(config.get("image_rotation", "0")), + "timelapse": int(config.get("timelapse", "2000")), + "horizontal_flip": int(config.get("horizontal_flip", "0")), + "vertical_flip": int(config.get("vertical_flip", "0")), + "file_path": config.get("file_path", + os.path.join(os.path.dirname(__file__), + 'image.jpg')) + } + ) + + # check filepath given is writable + if not os.access(setup_config["file_path"], os.W_OK): + _LOGGER.error("Error: file path is not writable") + return False + + add_devices([ + RaspberryCamera(setup_config) + ]) + + +class RaspberryCamera(Camera): + """Raspberry Pi camera.""" + + def __init__(self, device_info): + """Initialize Raspberry Pi camera component.""" + super().__init__() + + self._name = device_info["name"] + self._config = device_info + + # kill if there's raspistill instance + subprocess.Popen(['killall', 'raspistill'], + stdout=subprocess.DEVNULL, + stderr=subprocess.STDOUT) + + cmd_args = [ + 'raspistill', '--nopreview', '-o', str(device_info["file_path"]), + '-t', '0', '-w', str(device_info["image_width"]), + '-h', str(device_info["image_height"]), + '-tl', str(device_info["timelapse"]), + '-q', str(device_info["image_quality"]), + '-rot', str(device_info["image_rotation"]) + ] + if device_info["horizontal_flip"]: + cmd_args.append("-hf") + + if device_info["vertical_flip"]: + cmd_args.append("-vf") + + subprocess.Popen(cmd_args, + stdout=subprocess.DEVNULL, + stderr=subprocess.STDOUT) + + def camera_image(self): + """Return raspstill image response.""" + with open(self._config["file_path"], 'rb') as file: + return file.read() + + @property + def name(self): + """Return the name of this camera.""" + return self._name