init home-assistant
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Tests for the folder_watcher component."""
|
||||
50
home-assistant/tests/components/folder_watcher/conftest.py
Normal file
50
home-assistant/tests/components/folder_watcher/conftest.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Fixtures for Folder Watcher integration tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.folder_watcher.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[None]:
|
||||
"""Mock setting up a config entry."""
|
||||
with patch(
|
||||
"homeassistant.components.folder_watcher.async_setup_entry", return_value=True
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def load_int(
|
||||
hass: HomeAssistant, tmp_path: Path, freezer: FrozenDateTimeFactory
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Folder watcher integration in Home Assistant."""
|
||||
freezer.move_to("2022-04-19 10:31:02+00:00")
|
||||
path = tmp_path.as_posix()
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
title=f"Folder Watcher {path!s}",
|
||||
data={},
|
||||
options={"folder": str(path), "patterns": ["*"]},
|
||||
entry_id="1",
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return config_entry
|
||||
@@ -0,0 +1,62 @@
|
||||
# serializer version: 1
|
||||
# name: test_event_entity[1-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'event_types': list([
|
||||
'closed',
|
||||
'created',
|
||||
'deleted',
|
||||
'modified',
|
||||
'moved',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'event',
|
||||
'entity_category': None,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': None,
|
||||
'platform': 'folder_watcher',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'folder_watcher',
|
||||
'unique_id': '1',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_event_entity[1-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'dest_file': 'hello2.txt',
|
||||
'event_type': 'moved',
|
||||
'event_types': list([
|
||||
'closed',
|
||||
'created',
|
||||
'deleted',
|
||||
'modified',
|
||||
'moved',
|
||||
]),
|
||||
'file': 'hello.txt',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2022-04-19T10:31:02.000+00:00',
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Test the Folder Watcher config flow."""
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.folder_watcher.const import (
|
||||
CONF_FOLDER,
|
||||
CONF_PATTERNS,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, tmp_path: Path) -> None:
|
||||
"""Test we get the form."""
|
||||
path = tmp_path.as_posix()
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == f"Folder Watcher {path}"
|
||||
assert result["options"] == {CONF_FOLDER: path, CONF_PATTERNS: ["*"]}
|
||||
|
||||
|
||||
async def test_form_not_allowed_path(hass: HomeAssistant, tmp_path: Path) -> None:
|
||||
"""Test we handle not allowed path."""
|
||||
path = tmp_path.as_posix()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "not_allowed_dir"}
|
||||
|
||||
hass.config.allowlist_external_dirs = {tmp_path}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == f"Folder Watcher {path}"
|
||||
assert result["options"] == {CONF_FOLDER: path, CONF_PATTERNS: ["*"]}
|
||||
|
||||
|
||||
async def test_form_not_directory(hass: HomeAssistant, tmp_path: Path) -> None:
|
||||
"""Test we handle not a directory."""
|
||||
path = tmp_path.as_posix()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: "not_a_directory"},
|
||||
)
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "not_dir"}
|
||||
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == f"Folder Watcher {path}"
|
||||
assert result["options"] == {CONF_FOLDER: path, CONF_PATTERNS: ["*"]}
|
||||
|
||||
|
||||
async def test_form_not_readable_dir(hass: HomeAssistant, tmp_path: Path) -> None:
|
||||
"""Test we handle not able to read directory."""
|
||||
path = tmp_path.as_posix()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch("os.access", return_value=False):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "not_readable_dir"}
|
||||
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == f"Folder Watcher {path}"
|
||||
assert result["options"] == {CONF_FOLDER: path, CONF_PATTERNS: ["*"]}
|
||||
|
||||
|
||||
async def test_form_already_configured(hass: HomeAssistant, tmp_path: Path) -> None:
|
||||
"""Test we abort when entry is already configured."""
|
||||
path = tmp_path.as_posix()
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=f"Folder Watcher {path}",
|
||||
data={CONF_FOLDER: path},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_FOLDER: path},
|
||||
)
|
||||
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
53
home-assistant/tests/components/folder_watcher/test_event.py
Normal file
53
home-assistant/tests/components/folder_watcher/test_event.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""The event entity tests for Folder Watcher."""
|
||||
|
||||
from pathlib import Path
|
||||
from time import sleep
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_event_entity(
|
||||
hass: HomeAssistant,
|
||||
load_int: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test the event entity."""
|
||||
entry = load_int
|
||||
await hass.async_block_till_done()
|
||||
|
||||
file = tmp_path.joinpath("hello.txt")
|
||||
file.write_text("Hello, world!")
|
||||
new_file = tmp_path.joinpath("hello2.txt")
|
||||
file.rename(new_file)
|
||||
|
||||
await hass.async_add_executor_job(sleep, 0.1)
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
assert entity_entries
|
||||
|
||||
def limit_attrs(prop, path):
|
||||
exclude_attrs = {
|
||||
"entity_id",
|
||||
"friendly_name",
|
||||
"folder",
|
||||
"path",
|
||||
"dest_folder",
|
||||
"dest_path",
|
||||
}
|
||||
return prop in exclude_attrs
|
||||
|
||||
for entity_entry in entity_entries:
|
||||
assert entity_entry == snapshot(
|
||||
name=f"{entity_entry.unique_id}-entry", exclude=limit_attrs
|
||||
)
|
||||
assert (state := hass.states.get(entity_entry.entity_id))
|
||||
assert state == snapshot(
|
||||
name=f"{entity_entry.unique_id}-state", exclude=limit_attrs
|
||||
)
|
||||
131
home-assistant/tests/components/folder_watcher/test_init.py
Normal file
131
home-assistant/tests/components/folder_watcher/test_init.py
Normal file
@@ -0,0 +1,131 @@
|
||||
"""The tests for the folder_watcher component."""
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
|
||||
from homeassistant.components import folder_watcher
|
||||
from homeassistant.components.folder_watcher.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_invalid_path_setup(
|
||||
hass: HomeAssistant,
|
||||
tmp_path: Path,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
) -> None:
|
||||
"""Test that an invalid path is not set up."""
|
||||
freezer.move_to("2022-04-19 10:31:02+00:00")
|
||||
path = tmp_path.as_posix()
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
title=f"Folder Watcher {path!s}",
|
||||
data={},
|
||||
options={"folder": str(path), "patterns": ["*"]},
|
||||
entry_id="1",
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
assert len(issue_registry.issues) == 1
|
||||
|
||||
|
||||
async def test_valid_path_setup(
|
||||
hass: HomeAssistant, tmp_path: Path, freezer: FrozenDateTimeFactory
|
||||
) -> None:
|
||||
"""Test that a valid path is setup."""
|
||||
freezer.move_to("2022-04-19 10:31:02+00:00")
|
||||
path = tmp_path.as_posix()
|
||||
hass.config.allowlist_external_dirs = {path}
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
title=f"Folder Watcher {path!s}",
|
||||
data={},
|
||||
options={"folder": str(path), "patterns": ["*"]},
|
||||
entry_id="1",
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
def test_event() -> None:
|
||||
"""Check that Home Assistant events are fired correctly on watchdog event."""
|
||||
|
||||
class MockPatternMatchingEventHandler:
|
||||
"""Mock base class for the pattern matcher event handler."""
|
||||
|
||||
def __init__(self, patterns) -> None:
|
||||
pass
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.folder_watcher.PatternMatchingEventHandler",
|
||||
MockPatternMatchingEventHandler,
|
||||
):
|
||||
hass = Mock()
|
||||
handler = folder_watcher.create_event_handler(["*"], hass, "1")
|
||||
handler.on_created(
|
||||
SimpleNamespace(
|
||||
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] == {
|
||||
"event_type": "created",
|
||||
"path": "/hello/world.txt",
|
||||
"file": "world.txt",
|
||||
"folder": "/hello",
|
||||
}
|
||||
|
||||
|
||||
def test_move_event() -> None:
|
||||
"""Check that Home Assistant events are fired correctly on watchdog event."""
|
||||
|
||||
class MockPatternMatchingEventHandler:
|
||||
"""Mock base class for the pattern matcher event handler."""
|
||||
|
||||
def __init__(self, patterns) -> None:
|
||||
pass
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.folder_watcher.PatternMatchingEventHandler",
|
||||
MockPatternMatchingEventHandler,
|
||||
):
|
||||
hass = Mock()
|
||||
handler = folder_watcher.create_event_handler(["*"], hass, "1")
|
||||
handler.on_moved(
|
||||
SimpleNamespace(
|
||||
is_directory=False,
|
||||
src_path="/hello/world.txt",
|
||||
dest_path="/hello/earth.txt",
|
||||
event_type="moved",
|
||||
)
|
||||
)
|
||||
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] == {
|
||||
"event_type": "moved",
|
||||
"path": "/hello/world.txt",
|
||||
"dest_path": "/hello/earth.txt",
|
||||
"file": "world.txt",
|
||||
"dest_file": "earth.txt",
|
||||
"folder": "/hello",
|
||||
"dest_folder": "/hello",
|
||||
}
|
||||
Reference in New Issue
Block a user