From 687bbce90074d7958043249473f015b4d5c30bab Mon Sep 17 00:00:00 2001 From: Robin Date: Sun, 28 Apr 2019 19:32:02 +0100 Subject: [PATCH] Fixes local_file camera service (#23479) * Fixes service Fixes service so only the target camera is updated * Update test_camera.py * fix lint --- homeassistant/components/local_file/camera.py | 14 +++++++++++-- tests/components/local_file/test_camera.py | 21 ++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/local_file/camera.py b/homeassistant/components/local_file/camera.py index 444f4109e98..5f17716abbb 100644 --- a/homeassistant/components/local_file/camera.py +++ b/homeassistant/components/local_file/camera.py @@ -5,7 +5,7 @@ import os import voluptuous as vol -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, ATTR_ENTITY_ID from homeassistant.components.camera import ( Camera, CAMERA_SERVICE_SCHEMA, PLATFORM_SCHEMA) from homeassistant.components.camera.const import DOMAIN @@ -14,6 +14,7 @@ from homeassistant.helpers import config_validation as cv _LOGGER = logging.getLogger(__name__) CONF_FILE_PATH = 'file_path' +DATA_LOCAL_FILE = 'local_file_cameras' DEFAULT_NAME = 'Local File' SERVICE_UPDATE_FILE_PATH = 'local_file_update_file_path' @@ -29,13 +30,22 @@ CAMERA_SERVICE_UPDATE_FILE_PATH = CAMERA_SERVICE_SCHEMA.extend({ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Camera that works with local files.""" + if DATA_LOCAL_FILE not in hass.data: + hass.data[DATA_LOCAL_FILE] = [] + file_path = config[CONF_FILE_PATH] camera = LocalFile(config[CONF_NAME], file_path) + hass.data[DATA_LOCAL_FILE].append(camera) def update_file_path_service(call): """Update the file path.""" file_path = call.data.get(CONF_FILE_PATH) - camera.update_file_path(file_path) + entity_ids = call.data.get(ATTR_ENTITY_ID) + cameras = hass.data[DATA_LOCAL_FILE] + + for camera in cameras: + if camera.entity_id in entity_ids: + camera.update_file_path(file_path) return True hass.services.register( diff --git a/tests/components/local_file/test_camera.py b/tests/components/local_file/test_camera.py index a96f9768be4..ade5eb4add3 100644 --- a/tests/components/local_file/test_camera.py +++ b/tests/components/local_file/test_camera.py @@ -124,12 +124,19 @@ async def test_update_file_path(hass): with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \ mock.patch('os.access', mock.Mock(return_value=True)): - await async_setup_component(hass, 'camera', { - 'camera': { - 'platform': 'local_file', - 'file_path': 'mock/path.jpg' + + camera_1 = { + 'platform': 'local_file', + 'file_path': 'mock/path.jpg' } - }) + camera_2 = { + 'platform': 'local_file', + 'name': 'local_file_camera_2', + 'file_path': 'mock/path_2.jpg' + } + await async_setup_component(hass, 'camera', { + 'camera': [camera_1, camera_2] + }) # Fetch state and check motion detection attribute state = hass.states.get('camera.local_file') @@ -148,3 +155,7 @@ async def test_update_file_path(hass): state = hass.states.get('camera.local_file') assert state.attributes.get('file_path') == 'new/path.jpg' + + # Check that local_file_camera_2 file_path is still as configured + state = hass.states.get('camera.local_file_camera_2') + assert state.attributes.get('file_path') == 'mock/path_2.jpg'