1 """The System Monitor integration."""
3 from dataclasses
import dataclass
6 import psutil_home_assistant
as ha_psutil
14 from .coordinator
import SystemMonitorCoordinator
15 from .util
import get_all_disk_mounts
17 _LOGGER = logging.getLogger(__name__)
19 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
21 type SystemMonitorConfigEntry = ConfigEntry[SystemMonitorData]
26 """Runtime data definition."""
28 coordinator: SystemMonitorCoordinator
29 psutil_wrapper: ha_psutil.PsutilWrapper
33 hass: HomeAssistant, entry: SystemMonitorConfigEntry
35 """Set up System Monitor from a config entry."""
36 psutil_wrapper = await hass.async_add_executor_job(ha_psutil.PsutilWrapper)
38 disk_arguments =
list(
39 await hass.async_add_executor_job(get_all_disk_mounts, hass, psutil_wrapper)
41 legacy_resources: set[str] = set(entry.options.get(
"resources", []))
42 for resource
in legacy_resources:
43 if resource.startswith(
"disk_"):
44 split_index = resource.rfind(
"_")
45 _type = resource[:split_index]
46 argument = resource[split_index + 1 :]
47 _LOGGER.debug(
"Loading legacy %s with argument %s", _type, argument)
48 disk_arguments.append(argument)
50 _LOGGER.debug(
"disk arguments to be added: %s", disk_arguments)
53 hass, psutil_wrapper, disk_arguments
55 await coordinator.async_config_entry_first_refresh()
58 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
59 entry.async_on_unload(entry.add_update_listener(update_listener))
64 """Unload System Monitor config entry."""
65 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
69 """Handle options update."""
70 await hass.config_entries.async_reload(entry.entry_id)
74 """Migrate old entry."""
80 if entry.version == 1
and entry.minor_version < 3:
81 new_options = {**entry.options}
82 if entry.minor_version == 1:
85 if processes := entry.options.get(SENSOR_DOMAIN):
86 new_options[BINARY_SENSOR_DOMAIN] = processes
87 hass.config_entries.async_update_entry(
88 entry, options=new_options, version=1, minor_version=2
91 if entry.minor_version == 2:
92 new_options = {**entry.options}
93 if SENSOR_DOMAIN
in new_options:
94 new_options.pop(SENSOR_DOMAIN)
95 hass.config_entries.async_update_entry(
96 entry, options=new_options, version=1, minor_version=3
100 "Migration to version %s.%s successful", entry.version, entry.minor_version
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, SystemMonitorConfigEntry entry)
None update_listener(HomeAssistant hass, ConfigEntry entry)