Local file camera now supports yet inexisting files. (#6157)

This commit is contained in:
Jose Juan Montes 2017-02-27 06:16:11 +01:00 committed by Paulus Schoutsen
parent 53a735a329
commit 65d255a626
2 changed files with 28 additions and 17 deletions

View File

@ -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."""
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):

View File

@ -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'):
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)