Don't error with missing information in systemmonitor diagnostics (#127868)

pull/128176/head
G Johansson 2024-10-09 10:44:54 +02:00 committed by Franck Nijhof
parent a1e42cac7a
commit c31e0336dc
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 95 additions and 4 deletions

View File

@ -37,17 +37,29 @@ class SensorData:
def as_dict(self) -> dict[str, Any]:
"""Return as dict."""
disk_usage = None
if self.disk_usage:
disk_usage = {k: str(v) for k, v in self.disk_usage.items()}
io_counters = None
if self.io_counters:
io_counters = {k: str(v) for k, v in self.io_counters.items()}
addresses = None
if self.addresses:
addresses = {k: str(v) for k, v in self.addresses.items()}
temperatures = None
if self.temperatures:
temperatures = {k: str(v) for k, v in self.temperatures.items()}
return {
"disk_usage": {k: str(v) for k, v in self.disk_usage.items()},
"disk_usage": disk_usage,
"swap": str(self.swap),
"memory": str(self.memory),
"io_counters": {k: str(v) for k, v in self.io_counters.items()},
"addresses": {k: str(v) for k, v in self.addresses.items()},
"io_counters": io_counters,
"addresses": addresses,
"load": str(self.load),
"cpu_percent": str(self.cpu_percent),
"boot_time": str(self.boot_time),
"processes": str(self.processes),
"temperatures": {k: str(v) for k, v in self.temperatures.items()},
"temperatures": temperatures,
}

View File

@ -62,3 +62,58 @@
}),
})
# ---
# name: test_diagnostics_missing_items[test_diagnostics_missing_items]
dict({
'coordinators': dict({
'data': dict({
'addresses': None,
'boot_time': '2024-02-24 15:00:00+00:00',
'cpu_percent': '10.0',
'disk_usage': dict({
'/': 'sdiskusage(total=536870912000, used=322122547200, free=214748364800, percent=60.0)',
'/home/notexist/': 'sdiskusage(total=536870912000, used=322122547200, free=214748364800, percent=60.0)',
'/media/share': 'sdiskusage(total=536870912000, used=322122547200, free=214748364800, percent=60.0)',
}),
'io_counters': None,
'load': '(1, 2, 3)',
'memory': 'VirtualMemory(total=104857600, available=41943040, percent=40.0, used=62914560, free=31457280)',
'processes': "[tests.components.systemmonitor.conftest.MockProcess(pid=1, name='python3', status='sleeping', started='2024-02-23 15:00:00'), tests.components.systemmonitor.conftest.MockProcess(pid=1, name='pip', status='sleeping', started='2024-02-23 15:00:00')]",
'swap': 'sswap(total=104857600, used=62914560, free=41943040, percent=60.0, sin=1, sout=1)',
'temperatures': dict({
'cpu0-thermal': "[shwtemp(label='cpu0-thermal', current=50.0, high=60.0, critical=70.0)]",
}),
}),
'last_update_success': True,
}),
'entry': dict({
'data': dict({
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'systemmonitor',
'minor_version': 3,
'options': dict({
'binary_sensor': dict({
'process': list([
'python3',
'pip',
]),
}),
'resources': list([
'disk_use_percent_/',
'disk_use_percent_/home/notexist/',
'memory_free_',
'network_out_eth0',
'process_python3',
]),
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'System Monitor',
'unique_id': None,
'version': 1,
}),
})
# ---

View File

@ -2,6 +2,7 @@
from unittest.mock import Mock
from freezegun.api import FrozenDateTimeFactory
from syrupy import SnapshotAssertion
from syrupy.filters import props
@ -24,3 +25,26 @@ async def test_diagnostics(
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_added_config_entry
) == snapshot(exclude=props("last_update", "entry_id", "created_at", "modified_at"))
async def test_diagnostics_missing_items(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_psutil: Mock,
mock_os: Mock,
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test diagnostics."""
mock_psutil.net_if_addrs.return_value = None
mock_psutil.net_io_counters.return_value = None
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == snapshot(
exclude=props("last_update", "entry_id", "created_at", "modified_at"),
name="test_diagnostics_missing_items",
)