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 <jan-philipp@bnck.me>

* Update homeassistant/helpers/system_info.py

---------

Co-authored-by: Jan-Philipp Benecke <jan-philipp@bnck.me>
This commit is contained in:
J. Nick Koston 2024-05-12 21:16:21 +09:00 committed by GitHub
parent 65a4e5a1af
commit f318a3b5e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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()