From 65d255a6266a2a60fa836d54cba49dfed32c326e Mon Sep 17 00:00:00 2001 From: Jose Juan Montes Date: Mon, 27 Feb 2017 06:16:11 +0100 Subject: [PATCH] Local file camera now supports yet inexisting files. (#6157) --- homeassistant/components/camera/local_file.py | 14 ++++++--- tests/components/camera/test_local_file.py | 31 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/camera/local_file.py b/homeassistant/components/camera/local_file.py index 65defb4557b..85438820393 100644 --- a/homeassistant/components/camera/local_file.py +++ b/homeassistant/components/camera/local_file.py @@ -20,7 +20,7 @@ CONF_FILE_PATH = 'file_path' DEFAULT_NAME = 'Local File' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_FILE_PATH): cv.isfile, + vol.Required(CONF_FILE_PATH): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string }) @@ -31,8 +31,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): # check filepath given is readable if not os.access(file_path, os.R_OK): - _LOGGER.error("file path is not readable") - return False + _LOGGER.warning("Could not read camera %s image from file: %s", + config[CONF_NAME], file_path) add_devices([LocalFile(config[CONF_NAME], file_path)]) @@ -49,8 +49,12 @@ class LocalFile(Camera): def camera_image(self): """Return image response.""" - with open(self._file_path, 'rb') as file: - return file.read() + try: + with open(self._file_path, 'rb') as file: + return file.read() + except FileNotFoundError: + _LOGGER.warning("Could not read camera %s image from file: %s", + self._name, self._file_path) @property def name(self): diff --git a/tests/components/camera/test_local_file.py b/tests/components/camera/test_local_file.py index d43c138c570..55ddbd10741 100644 --- a/tests/components/camera/test_local_file.py +++ b/tests/components/camera/test_local_file.py @@ -8,7 +8,8 @@ from mock_open import MockOpen from homeassistant.bootstrap import setup_component -from tests.common import assert_setup_component, mock_http_component +from tests.common import mock_http_component +import logging @asyncio.coroutine @@ -42,19 +43,25 @@ def test_loading_file(hass, test_client): @asyncio.coroutine -def test_file_not_readable(hass): - """Test local file will not setup when file is not readable.""" +def test_file_not_readable(hass, caplog): + """Test a warning is shown setup when file is not readable.""" mock_http_component(hass) + @mock.patch('os.path.isfile', mock.Mock(return_value=True)) + @mock.patch('os.access', mock.Mock(return_value=False)) def run_test(): - with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \ - mock.patch('os.access', return_value=False), \ - assert_setup_component(0, 'camera'): - assert setup_component(hass, 'camera', { - 'camera': { - 'name': 'config_test', - 'platform': 'local_file', - 'file_path': 'mock.file', - }}) + + caplog.set_level( + logging.WARNING, logger='requests.packages.urllib3.connectionpool') + + assert setup_component(hass, 'camera', { + 'camera': { + 'name': 'config_test', + 'platform': 'local_file', + 'file_path': 'mock.file', + }}) + assert 'Could not read' in caplog.text + assert 'config_test' in caplog.text + assert 'mock.file' in caplog.text yield from hass.loop.run_in_executor(None, run_test)