Add the ability to reload filesize platforms from yaml (#39347)
This commit is contained in:
parent
0db5bb27a8
commit
f449620d38
5 changed files with 64 additions and 1 deletions
|
@ -1 +1,4 @@
|
||||||
"""The filesize component."""
|
"""The filesize component."""
|
||||||
|
|
||||||
|
DOMAIN = "filesize"
|
||||||
|
PLATFORMS = ["sensor"]
|
||||||
|
|
|
@ -9,6 +9,9 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import DATA_MEGABYTES
|
from homeassistant.const import DATA_MEGABYTES
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.helpers.reload import setup_reload_service
|
||||||
|
|
||||||
|
from . import DOMAIN, PLATFORMS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -23,6 +26,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the file size sensor."""
|
"""Set up the file size sensor."""
|
||||||
|
|
||||||
|
setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
for path in config.get(CONF_FILE_PATHS):
|
for path in config.get(CONF_FILE_PATHS):
|
||||||
if not hass.config.is_allowed_path(path):
|
if not hass.config.is_allowed_path(path):
|
||||||
|
|
2
homeassistant/components/filesize/services.yaml
Normal file
2
homeassistant/components/filesize/services.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
reload:
|
||||||
|
description: Reload all filesize entities.
|
|
@ -2,9 +2,13 @@
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant import config as hass_config
|
||||||
|
from homeassistant.components.filesize import DOMAIN
|
||||||
from homeassistant.components.filesize.sensor import CONF_FILE_PATHS
|
from homeassistant.components.filesize.sensor import CONF_FILE_PATHS
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.const import SERVICE_RELOAD
|
||||||
|
from homeassistant.setup import async_setup_component, setup_component
|
||||||
|
|
||||||
|
from tests.async_mock import patch
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
TEST_DIR = os.path.join(os.path.dirname(__file__))
|
TEST_DIR = os.path.join(os.path.dirname(__file__))
|
||||||
|
@ -47,3 +51,47 @@ class TestFileSensor(unittest.TestCase):
|
||||||
state = self.hass.states.get("sensor.mock_file_test_filesize_txt")
|
state = self.hass.states.get("sensor.mock_file_test_filesize_txt")
|
||||||
assert state.state == "0.0"
|
assert state.state == "0.0"
|
||||||
assert state.attributes.get("bytes") == 4
|
assert state.attributes.get("bytes") == 4
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reload(hass, tmpdir):
|
||||||
|
"""Verify we can reload filter sensors."""
|
||||||
|
testfile = f"{tmpdir}/file"
|
||||||
|
await hass.async_add_executor_job(create_file, testfile)
|
||||||
|
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
"sensor",
|
||||||
|
{
|
||||||
|
"sensor": {
|
||||||
|
"platform": "filesize",
|
||||||
|
"file_paths": [testfile],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(hass.states.async_all()) == 1
|
||||||
|
|
||||||
|
assert hass.states.get("sensor.file")
|
||||||
|
|
||||||
|
yaml_path = os.path.join(
|
||||||
|
_get_fixtures_base_path(),
|
||||||
|
"fixtures",
|
||||||
|
"filesize/configuration.yaml",
|
||||||
|
)
|
||||||
|
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path), patch.object(
|
||||||
|
hass.config, "is_allowed_path", return_value=True
|
||||||
|
):
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_RELOAD,
|
||||||
|
{},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert hass.states.get("sensor.file") is None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_fixtures_base_path():
|
||||||
|
return os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
|
4
tests/fixtures/filesize/configuration.yaml
vendored
Normal file
4
tests/fixtures/filesize/configuration.yaml
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
sensor:
|
||||||
|
- platform: filesize
|
||||||
|
file_paths:
|
||||||
|
- "/dev/null"
|
Loading…
Add table
Reference in a new issue