mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
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:
parent
54c34bb224
commit
687bbce900
@ -5,7 +5,7 @@ import os
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME, ATTR_ENTITY_ID
|
||||||
from homeassistant.components.camera import (
|
from homeassistant.components.camera import (
|
||||||
Camera, CAMERA_SERVICE_SCHEMA, PLATFORM_SCHEMA)
|
Camera, CAMERA_SERVICE_SCHEMA, PLATFORM_SCHEMA)
|
||||||
from homeassistant.components.camera.const import DOMAIN
|
from homeassistant.components.camera.const import DOMAIN
|
||||||
@ -14,6 +14,7 @@ from homeassistant.helpers import config_validation as cv
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_FILE_PATH = 'file_path'
|
CONF_FILE_PATH = 'file_path'
|
||||||
|
DATA_LOCAL_FILE = 'local_file_cameras'
|
||||||
DEFAULT_NAME = 'Local File'
|
DEFAULT_NAME = 'Local File'
|
||||||
SERVICE_UPDATE_FILE_PATH = 'local_file_update_file_path'
|
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):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Camera that works with local files."""
|
"""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]
|
file_path = config[CONF_FILE_PATH]
|
||||||
camera = LocalFile(config[CONF_NAME], file_path)
|
camera = LocalFile(config[CONF_NAME], file_path)
|
||||||
|
hass.data[DATA_LOCAL_FILE].append(camera)
|
||||||
|
|
||||||
def update_file_path_service(call):
|
def update_file_path_service(call):
|
||||||
"""Update the file path."""
|
"""Update the file path."""
|
||||||
file_path = call.data.get(CONF_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
|
return True
|
||||||
|
|
||||||
hass.services.register(
|
hass.services.register(
|
||||||
|
@ -124,12 +124,19 @@ async def test_update_file_path(hass):
|
|||||||
|
|
||||||
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
||||||
mock.patch('os.access', mock.Mock(return_value=True)):
|
mock.patch('os.access', mock.Mock(return_value=True)):
|
||||||
await async_setup_component(hass, 'camera', {
|
|
||||||
'camera': {
|
camera_1 = {
|
||||||
'platform': 'local_file',
|
'platform': 'local_file',
|
||||||
'file_path': 'mock/path.jpg'
|
'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
|
# Fetch state and check motion detection attribute
|
||||||
state = hass.states.get('camera.local_file')
|
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')
|
state = hass.states.get('camera.local_file')
|
||||||
assert state.attributes.get('file_path') == 'new/path.jpg'
|
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'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user