core/tests/components/folder_watcher/test_init.py

56 lines
1.8 KiB
Python
Raw Normal View History

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-30 01:10:20 +00:00
"""The tests for the folder_watcher component."""
import os
from unittest.mock import Mock, patch
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-30 01:10:20 +00:00
from homeassistant.components import folder_watcher
from homeassistant.setup import async_setup_component
from tests.common import MockDependency
async def test_invalid_path_setup(hass):
2018-08-19 20:29:08 +00:00
"""Test that an invalid path is not set up."""
assert not await async_setup_component(
2019-07-31 19:25:30 +00:00
hass,
folder_watcher.DOMAIN,
{folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: "invalid_path"}},
)
async def test_valid_path_setup(hass):
"""Test that a valid path is setup."""
cwd = os.path.join(os.path.dirname(__file__))
hass.config.whitelist_external_dirs = {cwd}
2019-07-31 19:25:30 +00:00
with patch.object(folder_watcher, "Watcher"):
assert await async_setup_component(
2019-07-31 19:25:30 +00:00
hass,
folder_watcher.DOMAIN,
{folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: cwd}},
)
2019-07-31 19:25:30 +00:00
@MockDependency("watchdog", "events")
def test_event(mock_watchdog):
"""Check that Home Assistant events are fired correctly on watchdog event."""
2019-07-31 19:25:30 +00:00
class MockPatternMatchingEventHandler:
"""Mock base class for the pattern matcher event handler."""
def __init__(self, patterns):
pass
2019-07-31 19:25:30 +00:00
mock_watchdog.events.PatternMatchingEventHandler = MockPatternMatchingEventHandler
hass = Mock()
2019-07-31 19:25:30 +00:00
handler = folder_watcher.create_event_handler(["*"], hass)
handler.on_created(
Mock(is_directory=False, src_path="/hello/world.txt", event_type="created")
)
assert hass.bus.fire.called
assert hass.bus.fire.mock_calls[0][1][0] == folder_watcher.DOMAIN
assert hass.bus.fire.mock_calls[0][1][1] == {
2019-07-31 19:25:30 +00:00
"event_type": "created",
"path": "/hello/world.txt",
"file": "world.txt",
"folder": "/hello",
}