hass-core/tests/components/test_folder_watcher.py
Robin df78eecc1b Adds folder_watcher component (#12918)
* Create watchdog_file_watcher.py

* Rename watchdog_file_watcher.py to folder_watcher.py

* Address a number of issues

* Adds filter

* Adds pattern matching

* Adds create_event_handler()

* Update folder_watcher.py

* Adds run_setup()

* Remove stop_watching()

* Adds shutdown()

* Update config to allow patterns on each folder

* Update to patterns from filters

* Adds watchdog

* Fix indents on schema

* Update folder_watcher.py

* Create test_file_watcher.py

* Fix lints

* Add test_invalid_path()

* Adds folder_watcher

* Update test_file_watcher.py

* Update folder_watcher.py

* Simplify config

* Adapt for new config

* Run observer.schedule() on EVENT_HOMEASSISTANT_START

* Amend Watcher removing entity and tidying startup

* Tidy config

* Rename process to on_any_event for consistency

* Rename on_any_event back to process

Using `on_any_event` resulted in 2 events being fired

* Update folder_watcher.py

* Fix return False on setup

* Update test_file_watcher.py

* Update folder_watcher.py

* Adds watchdog

* Undo adding watchdog

* Update test_file_watcher.py

* Update test_file_watcher.py

* Update test_file_watcher.py

* Update test_file_watcher.py

* Update test_file_watcher.py

* Add event

* Update test_file_watcher.py

* Update .coveragerc

* Update test_file_watcher.py

* Update test_file_watcher.py

* debug + join

* test event

* lint

* lint

* Rename test_file_watcher.py to test_folder_watcher.py

* hound

* Tidy test

* Further refine test

* Adds to test_all

* Fix test for py35

* Change test again

* Update test_folder_watcher.py

* Fix test

* Add watchdog to test

* Update folder_watcher.py

* add watchdog

* Update folder_watcher.py
2018-03-29 18:10:20 -07:00

64 lines
2 KiB
Python

"""The tests for the folder_watcher component."""
import unittest
from unittest.mock import MagicMock
import os
from homeassistant.components import folder_watcher
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
CWD = os.path.join(os.path.dirname(__file__))
FILE = 'file.txt'
class TestFolderWatcher(unittest.TestCase):
"""Test the file_watcher component."""
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.config.whitelist_external_dirs = set((CWD))
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_invalid_path_setup(self):
"""Test that a invalid path is not setup."""
config = {
folder_watcher.DOMAIN: [{
folder_watcher.CONF_FOLDER: 'invalid_path'
}]
}
self.assertFalse(
setup_component(self.hass, folder_watcher.DOMAIN, config))
def test_valid_path_setup(self):
"""Test that a valid path is setup."""
config = {
folder_watcher.DOMAIN: [{folder_watcher.CONF_FOLDER: CWD}]
}
self.assertTrue(setup_component(
self.hass, folder_watcher.DOMAIN, config))
def test_event(self):
"""Check that HASS events are fired correctly on watchdog event."""
from watchdog.events import FileModifiedEvent
# Cant use setup_component as need to retrieve Watcher object.
w = folder_watcher.Watcher(CWD,
folder_watcher.DEFAULT_PATTERN,
self.hass)
w.startup(None)
self.hass.bus.fire = MagicMock()
# Trigger a fake filesystem event through the Watcher Observer emitter.
(emitter,) = w._observer.emitters
emitter.queue_event(FileModifiedEvent(FILE))
# Wait for the event to propagate.
self.hass.block_till_done()
assert self.hass.bus.fire.called