2017-05-15 12:25:46 +00:00
|
|
|
"""The tests for local file sensor platform."""
|
2022-06-26 02:02:50 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2017-05-15 12:25:46 +00:00
|
|
|
from homeassistant.const import STATE_UNKNOWN
|
2022-02-12 17:49:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-10-16 11:30:03 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2017-05-15 12:25:46 +00:00
|
|
|
|
2023-02-08 19:28:44 +00:00
|
|
|
from tests.common import get_fixture_path
|
2017-05-15 12:25:46 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:30:03 +00:00
|
|
|
@patch("os.path.isfile", Mock(return_value=True))
|
|
|
|
@patch("os.access", Mock(return_value=True))
|
2022-02-12 17:49:37 +00:00
|
|
|
async def test_file_value(hass: HomeAssistant) -> None:
|
2020-10-16 11:30:03 +00:00
|
|
|
"""Test the File sensor."""
|
|
|
|
config = {
|
2022-06-26 02:02:50 +00:00
|
|
|
"sensor": {
|
|
|
|
"platform": "file",
|
|
|
|
"name": "file1",
|
|
|
|
"file_path": get_fixture_path("file_value.txt", "file"),
|
|
|
|
}
|
2020-10-16 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 02:02:50 +00:00
|
|
|
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
2020-10-16 11:30:03 +00:00
|
|
|
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))
|
2022-02-12 17:49:37 +00:00
|
|
|
async def test_file_value_template(hass: HomeAssistant) -> None:
|
2020-10-16 11:30:03 +00:00
|
|
|
"""Test the File sensor with JSON entries."""
|
|
|
|
config = {
|
|
|
|
"sensor": {
|
|
|
|
"platform": "file",
|
|
|
|
"name": "file2",
|
2022-06-26 02:02:50 +00:00
|
|
|
"file_path": get_fixture_path("file_value_template.txt", "file"),
|
2020-10-16 11:30:03 +00:00
|
|
|
"value_template": "{{ value_json.temperature }}",
|
2017-05-15 12:25:46 +00:00
|
|
|
}
|
2020-10-16 11:30:03 +00:00
|
|
|
}
|
2017-05-15 12:25:46 +00:00
|
|
|
|
2022-06-26 02:02:50 +00:00
|
|
|
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
2020-10-16 11:30:03 +00:00
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
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))
|
2022-02-12 17:49:37 +00:00
|
|
|
async def test_file_empty(hass: HomeAssistant) -> None:
|
2020-10-16 11:30:03 +00:00
|
|
|
"""Test the File sensor with an empty file."""
|
2022-06-26 02:02:50 +00:00
|
|
|
config = {
|
|
|
|
"sensor": {
|
|
|
|
"platform": "file",
|
|
|
|
"name": "file3",
|
|
|
|
"file_path": get_fixture_path("file_empty.txt", "file"),
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 12:25:46 +00:00
|
|
|
|
2022-06-26 02:02:50 +00:00
|
|
|
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
2020-10-16 11:30:03 +00:00
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
2017-05-15 12:25:46 +00:00
|
|
|
|
2020-10-16 11:30:03 +00:00
|
|
|
state = hass.states.get("sensor.file3")
|
|
|
|
assert state.state == STATE_UNKNOWN
|
2022-02-12 17:49:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@patch("os.path.isfile", Mock(return_value=True))
|
|
|
|
@patch("os.access", Mock(return_value=True))
|
|
|
|
async def test_file_path_invalid(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the File sensor with invalid path."""
|
|
|
|
config = {
|
2022-06-26 02:02:50 +00:00
|
|
|
"sensor": {
|
|
|
|
"platform": "file",
|
|
|
|
"name": "file4",
|
|
|
|
"file_path": get_fixture_path("file_value.txt", "file"),
|
|
|
|
}
|
2022-02-12 17:49:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 02:02:50 +00:00
|
|
|
with patch.object(hass.config, "is_allowed_path", return_value=False):
|
2022-02-12 17:49:37 +00:00
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.states.async_entity_ids("sensor")) == 0
|