From f318a3b5e2e0802c478b25960dc138e87a998fda Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 May 2024 21:16:21 +0900 Subject: [PATCH] Fix blocking I/O in the event loop to get MacOS system_info (#117290) * Fix blocking I/O in the event look to get MacOS system_info * split pr * Update homeassistant/helpers/system_info.py Co-authored-by: Jan-Philipp Benecke * Update homeassistant/helpers/system_info.py --------- Co-authored-by: Jan-Philipp Benecke --- homeassistant/helpers/system_info.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/system_info.py b/homeassistant/helpers/system_info.py index ec8badaddc3..69e03904caa 100644 --- a/homeassistant/helpers/system_info.py +++ b/homeassistant/helpers/system_info.py @@ -15,9 +15,12 @@ from homeassistant.loader import bind_hass from homeassistant.util.package import is_docker_env, is_virtual_env from .importlib import async_import_module +from .singleton import singleton _LOGGER = logging.getLogger(__name__) +_DATA_MAC_VER = "system_info_mac_ver" + @cache def is_official_image() -> bool: @@ -25,6 +28,12 @@ def is_official_image() -> bool: return os.path.isfile("/OFFICIAL_IMAGE") +@singleton(_DATA_MAC_VER) +async def async_get_mac_ver(hass: HomeAssistant) -> str: + """Return the macOS version.""" + return (await hass.async_add_executor_job(platform.mac_ver))[0] + + # Cache the result of getuser() because it can call getpwuid() which # can do blocking I/O to look up the username in /etc/passwd. cached_get_user = cache(getuser) @@ -65,7 +74,7 @@ async def async_get_system_info(hass: HomeAssistant) -> dict[str, Any]: info_object["user"] = None if platform.system() == "Darwin": - info_object["os_version"] = platform.mac_ver()[0] + info_object["os_version"] = await async_get_mac_ver(hass) elif platform.system() == "Linux": info_object["docker"] = is_docker_env()