Fixes local_file camera service (#23479)

* Fixes service

Fixes service so only the target camera is updated

* Update test_camera.py

* fix lint
This commit is contained in:
Robin 2019-04-28 19:32:02 +01:00 committed by GitHub
parent 54c34bb224
commit 687bbce900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -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(

View File

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