1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Added file path validity checks to file sensor (#12505)

* Added file validity checks to file sensor

* Patched out 'is_allowed_path' for file sensor tests
This commit is contained in:
Frederik Bolding 2018-03-30 04:47:49 +02:00 committed by Paulus Schoutsen
parent 8617177ff1
commit 3e5462ebff
2 changed files with 8 additions and 3 deletions

View File

@ -25,7 +25,7 @@ DEFAULT_NAME = 'File'
ICON = 'mdi:file'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_FILE_PATH): cv.string,
vol.Required(CONF_FILE_PATH): cv.isfile,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
@ -43,8 +43,11 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if value_template is not None:
value_template.hass = hass
async_add_devices(
[FileSensor(name, file_path, unit, value_template)], True)
if hass.config.is_allowed_path(file_path):
async_add_devices(
[FileSensor(name, file_path, unit, value_template)], True)
else:
_LOGGER.error("'%s' is not a whitelisted directory", file_path)
class FileSensor(Entity):

View File

@ -18,6 +18,8 @@ class TestFileSensor(unittest.TestCase):
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
# Patch out 'is_allowed_path' as the mock files aren't allowed
self.hass.config.is_allowed_path = Mock(return_value=True)
mock_registry(self.hass)
def teardown_method(self, method):