From 01a6b85a359dd1923eeb94bc11efdf74ba0b53e7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Feb 2024 22:31:02 -1000 Subject: [PATCH] Avoid creating system monitor disk sensors for non-dirs (#111695) Avoid creating system monitor sensors for non-dirs Currently we create sensors for /etc/hosts, /etc/asound.conf, since they are bind mounts in the container. These all have to have their own coordinator --- homeassistant/components/systemmonitor/util.py | 6 ++++++ tests/components/systemmonitor/conftest.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/homeassistant/components/systemmonitor/util.py b/homeassistant/components/systemmonitor/util.py index befe9024120..c67d4771ff4 100644 --- a/homeassistant/components/systemmonitor/util.py +++ b/homeassistant/components/systemmonitor/util.py @@ -30,6 +30,12 @@ def get_all_disk_mounts(hass: HomeAssistant) -> set[str]: # Ignore disks which are memory continue try: + if not os.path.isdir(part.mountpoint): + _LOGGER.debug( + "Mountpoint %s was excluded because it is not a directory", + part.mountpoint, + ) + continue usage = psutil_wrapper.psutil.disk_usage(part.mountpoint) except PermissionError: _LOGGER.debug( diff --git a/tests/components/systemmonitor/conftest.py b/tests/components/systemmonitor/conftest.py index c48678dcb5f..b12c11e73e6 100644 --- a/tests/components/systemmonitor/conftest.py +++ b/tests/components/systemmonitor/conftest.py @@ -162,6 +162,7 @@ def mock_psutil(mock_process: list[MockProcess]) -> Generator: sdiskpart("test", "/", "ext4", "", 1, 1), sdiskpart("test2", "/media/share", "ext4", "", 1, 1), sdiskpart("test3", "/incorrect", "", "", 1, 1), + sdiskpart("hosts", "/etc/hosts", "bind", "", 1, 1), sdiskpart("proc", "/proc/run", "proc", "", 1, 1), ] mock_psutil.boot_time.return_value = 1708786800.0 @@ -172,6 +173,11 @@ def mock_psutil(mock_process: list[MockProcess]) -> Generator: @pytest.fixture def mock_os() -> Generator: """Mock os.""" + + def isdir(path: str) -> bool: + """Mock os.path.isdir.""" + return path != "/etc/hosts" + with patch( "homeassistant.components.systemmonitor.coordinator.os" ) as mock_os, patch( @@ -179,4 +185,5 @@ def mock_os() -> Generator: ) as mock_os_util: mock_os_util.name = "nt" mock_os.getloadavg.return_value = (1, 2, 3) + mock_os_util.path.isdir = isdir yield mock_os