hass-core/tests/components/file/test_sensor.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.6 KiB
Python
Raw Normal View History

"""The tests for local file sensor platform."""
from unittest.mock import MagicMock, Mock, patch
2021-01-01 22:31:56 +01:00
import pytest
from homeassistant.components.file import DOMAIN
from homeassistant.const import STATE_UNKNOWN
2022-02-12 18:49:37 +01:00
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, get_fixture_path
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
async def test_file_value_yaml_setup(
hass: HomeAssistant, mock_is_allowed_path: MagicMock
) -> None:
"""Test the File sensor from YAML setup."""
config = {
"sensor": {
"platform": "file",
"name": "file1",
"file_path": get_fixture_path("file_value.txt", "file"),
}
}
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = hass.states.get("sensor.file1")
assert state.state == "21"
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
async def test_file_value_entry_setup(
hass: HomeAssistant, mock_is_allowed_path: MagicMock
) -> None:
"""Test the File sensor from an entry setup."""
data = {
"platform": "sensor",
"name": "file1",
"file_path": get_fixture_path("file_value.txt", "file"),
}
entry = MockConfigEntry(
domain=DOMAIN, data=data, title=f"test [{data['file_path']}]"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
state = hass.states.get("sensor.file1")
assert state.state == "21"
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
async def test_file_value_template(
hass: HomeAssistant, mock_is_allowed_path: MagicMock
) -> None:
"""Test the File sensor with JSON entries."""
data = {
"platform": "sensor",
"name": "file2",
"file_path": get_fixture_path("file_value_template.txt", "file"),
"value_template": "{{ value_json.temperature }}",
}
entry = MockConfigEntry(
domain=DOMAIN, data=data, title=f"test [{data['file_path']}]"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
state = hass.states.get("sensor.file2")
assert state.state == "26"
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
async def test_file_empty(hass: HomeAssistant, mock_is_allowed_path: MagicMock) -> None:
"""Test the File sensor with an empty file."""
data = {
"platform": "sensor",
"name": "file3",
"file_path": get_fixture_path("file_empty.txt", "file"),
}
entry = MockConfigEntry(
domain=DOMAIN, data=data, title=f"test [{data['file_path']}]"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
state = hass.states.get("sensor.file3")
assert state.state == STATE_UNKNOWN
2022-02-12 18:49:37 +01:00
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
@pytest.mark.parametrize("is_allowed", [False])
async def test_file_path_invalid(
hass: HomeAssistant, mock_is_allowed_path: MagicMock
) -> None:
2022-02-12 18:49:37 +01:00
"""Test the File sensor with invalid path."""
data = {
"platform": "sensor",
"name": "file4",
"file_path": get_fixture_path("file_value.txt", "file"),
2022-02-12 18:49:37 +01:00
}
entry = MockConfigEntry(
domain=DOMAIN, data=data, title=f"test [{data['file_path']}]"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
2022-02-12 18:49:37 +01:00
assert len(hass.states.async_entity_ids("sensor")) == 0