mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Use set instead of list in Systemmonitor (#106650)
This commit is contained in:
parent
5f3389b8e4
commit
767c55fbac
@ -86,7 +86,7 @@ async def validate_import_sensor_setup(
|
|||||||
async def get_sensor_setup_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
|
async def get_sensor_setup_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
|
||||||
"""Return process sensor setup schema."""
|
"""Return process sensor setup schema."""
|
||||||
hass = handler.parent_handler.hass
|
hass = handler.parent_handler.hass
|
||||||
processes = await hass.async_add_executor_job(get_all_running_processes)
|
processes = list(await hass.async_add_executor_job(get_all_running_processes))
|
||||||
return vol.Schema(
|
return vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_PROCESS): SelectSelector(
|
vol.Required(CONF_PROCESS): SelectSelector(
|
||||||
|
@ -267,7 +267,7 @@ def check_required_arg(value: Any) -> Any:
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def check_legacy_resource(resource: str, resources: list[str]) -> bool:
|
def check_legacy_resource(resource: str, resources: set[str]) -> bool:
|
||||||
"""Return True if legacy resource was configured."""
|
"""Return True if legacy resource was configured."""
|
||||||
# This function to check legacy resources can be removed
|
# This function to check legacy resources can be removed
|
||||||
# once we are removing the import from YAML
|
# once we are removing the import from YAML
|
||||||
@ -388,8 +388,8 @@ async def async_setup_entry(
|
|||||||
"""Set up System Montor sensors based on a config entry."""
|
"""Set up System Montor sensors based on a config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
sensor_registry: dict[tuple[str, str], SensorData] = {}
|
sensor_registry: dict[tuple[str, str], SensorData] = {}
|
||||||
legacy_resources: list[str] = entry.options.get("resources", [])
|
legacy_resources: set[str] = set(entry.options.get("resources", []))
|
||||||
loaded_resources: list[str] = []
|
loaded_resources: set[str] = set()
|
||||||
disk_arguments = await hass.async_add_executor_job(get_all_disk_mounts)
|
disk_arguments = await hass.async_add_executor_job(get_all_disk_mounts)
|
||||||
network_arguments = await hass.async_add_executor_job(get_all_network_interfaces)
|
network_arguments = await hass.async_add_executor_job(get_all_network_interfaces)
|
||||||
cpu_temperature = await hass.async_add_executor_job(_read_cpu_temperature)
|
cpu_temperature = await hass.async_add_executor_job(_read_cpu_temperature)
|
||||||
@ -405,7 +405,7 @@ async def async_setup_entry(
|
|||||||
is_enabled = check_legacy_resource(
|
is_enabled = check_legacy_resource(
|
||||||
f"{_type}_{argument}", legacy_resources
|
f"{_type}_{argument}", legacy_resources
|
||||||
)
|
)
|
||||||
loaded_resources.append(f"{_type}_{argument}")
|
loaded_resources.add(f"{_type}_{argument}")
|
||||||
entities.append(
|
entities.append(
|
||||||
SystemMonitorSensor(
|
SystemMonitorSensor(
|
||||||
sensor_registry,
|
sensor_registry,
|
||||||
@ -425,7 +425,7 @@ async def async_setup_entry(
|
|||||||
is_enabled = check_legacy_resource(
|
is_enabled = check_legacy_resource(
|
||||||
f"{_type}_{argument}", legacy_resources
|
f"{_type}_{argument}", legacy_resources
|
||||||
)
|
)
|
||||||
loaded_resources.append(f"{_type}_{argument}")
|
loaded_resources.add(f"{_type}_{argument}")
|
||||||
entities.append(
|
entities.append(
|
||||||
SystemMonitorSensor(
|
SystemMonitorSensor(
|
||||||
sensor_registry,
|
sensor_registry,
|
||||||
@ -449,7 +449,7 @@ async def async_setup_entry(
|
|||||||
sensor_registry[(_type, argument)] = SensorData(
|
sensor_registry[(_type, argument)] = SensorData(
|
||||||
argument, None, None, None, None
|
argument, None, None, None, None
|
||||||
)
|
)
|
||||||
loaded_resources.append(f"{_type}_{argument}")
|
loaded_resources.add(f"{_type}_{argument}")
|
||||||
entities.append(
|
entities.append(
|
||||||
SystemMonitorSensor(
|
SystemMonitorSensor(
|
||||||
sensor_registry,
|
sensor_registry,
|
||||||
@ -463,7 +463,7 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
sensor_registry[(_type, "")] = SensorData("", None, None, None, None)
|
sensor_registry[(_type, "")] = SensorData("", None, None, None, None)
|
||||||
is_enabled = check_legacy_resource(f"{_type}_", legacy_resources)
|
is_enabled = check_legacy_resource(f"{_type}_", legacy_resources)
|
||||||
loaded_resources.append(f"{_type}_")
|
loaded_resources.add(f"{_type}_")
|
||||||
entities.append(
|
entities.append(
|
||||||
SystemMonitorSensor(
|
SystemMonitorSensor(
|
||||||
sensor_registry,
|
sensor_registry,
|
||||||
|
@ -8,9 +8,9 @@ import psutil
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_all_disk_mounts() -> list[str]:
|
def get_all_disk_mounts() -> set[str]:
|
||||||
"""Return all disk mount points on system."""
|
"""Return all disk mount points on system."""
|
||||||
disks: list[str] = []
|
disks: set[str] = set()
|
||||||
for part in psutil.disk_partitions(all=True):
|
for part in psutil.disk_partitions(all=True):
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
if "cdrom" in part.opts or part.fstype == "":
|
if "cdrom" in part.opts or part.fstype == "":
|
||||||
@ -20,25 +20,25 @@ def get_all_disk_mounts() -> list[str]:
|
|||||||
continue
|
continue
|
||||||
usage = psutil.disk_usage(part.mountpoint)
|
usage = psutil.disk_usage(part.mountpoint)
|
||||||
if usage.total > 0 and part.device != "":
|
if usage.total > 0 and part.device != "":
|
||||||
disks.append(part.mountpoint)
|
disks.add(part.mountpoint)
|
||||||
_LOGGER.debug("Adding disks: %s", ", ".join(disks))
|
_LOGGER.debug("Adding disks: %s", ", ".join(disks))
|
||||||
return disks
|
return disks
|
||||||
|
|
||||||
|
|
||||||
def get_all_network_interfaces() -> list[str]:
|
def get_all_network_interfaces() -> set[str]:
|
||||||
"""Return all network interfaces on system."""
|
"""Return all network interfaces on system."""
|
||||||
interfaces: list[str] = []
|
interfaces: set[str] = set()
|
||||||
for interface, _ in psutil.net_if_addrs().items():
|
for interface, _ in psutil.net_if_addrs().items():
|
||||||
interfaces.append(interface)
|
interfaces.add(interface)
|
||||||
_LOGGER.debug("Adding interfaces: %s", ", ".join(interfaces))
|
_LOGGER.debug("Adding interfaces: %s", ", ".join(interfaces))
|
||||||
return interfaces
|
return interfaces
|
||||||
|
|
||||||
|
|
||||||
def get_all_running_processes() -> list[str]:
|
def get_all_running_processes() -> set[str]:
|
||||||
"""Return all running processes on system."""
|
"""Return all running processes on system."""
|
||||||
processes: list[str] = []
|
processes: set[str] = set()
|
||||||
for proc in psutil.process_iter(["name"]):
|
for proc in psutil.process_iter(["name"]):
|
||||||
if proc.name() not in processes:
|
if proc.name() not in processes:
|
||||||
processes.append(proc.name())
|
processes.add(proc.name())
|
||||||
_LOGGER.debug("Running processes: %s", ", ".join(processes))
|
_LOGGER.debug("Running processes: %s", ", ".join(processes))
|
||||||
return processes
|
return processes
|
||||||
|
Loading…
x
Reference in New Issue
Block a user