From c7ee74a573aeb4fc6ea69f83f3bb7167e59cc970 Mon Sep 17 00:00:00 2001 From: Landrash Date: Mon, 13 Jun 2016 01:26:29 +0200 Subject: [PATCH] Local file - Camera platform (#2282) --- homeassistant/components/camera/local_file.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 homeassistant/components/camera/local_file.py diff --git a/homeassistant/components/camera/local_file.py b/homeassistant/components/camera/local_file.py new file mode 100644 index 00000000000..463bf3eca5a --- /dev/null +++ b/homeassistant/components/camera/local_file.py @@ -0,0 +1,53 @@ +"""Camera that loads a picture from a local file.""" + +import logging +import os + +from homeassistant.components.camera import Camera + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the Camera.""" + # check for missing required configuration variable + if config.get("file_path") is None: + _LOGGER.error("Missing required variable: file_path") + return False + + setup_config = ( + { + "name": config.get("name", "Local File"), + "file_path": config.get("file_path") + } + ) + + # check filepath given is readable + if not os.access(setup_config["file_path"], os.R_OK): + _LOGGER.error("file path is not readable") + return False + + add_devices([ + LocalFile(setup_config) + ]) + + +class LocalFile(Camera): + """Local camera.""" + + def __init__(self, device_info): + """Initialize Local File Camera component.""" + super().__init__() + + self._name = device_info["name"] + self._config = device_info + + def camera_image(self): + """Return 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