Skip to content

Commit

Permalink
Fixes local_file camera service (home-assistant#23479)
Browse files Browse the repository at this point in the history
* Fixes service

Fixes service so only the target camera is updated

* Update test_camera.py

* fix lint
  • Loading branch information
robmarkcole authored Apr 28, 2019
1 parent 54c34bb commit 687bbce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 12 additions & 2 deletions homeassistant/components/local_file/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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
Expand All @@ -14,6 +14,7 @@
_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'

Expand All @@ -29,13 +30,22 @@

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(
Expand Down
21 changes: 16 additions & 5 deletions tests/components/local_file/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'

0 comments on commit 687bbce

Please sign in to comment.