core/homeassistant/helpers/system_info.py

87 lines
2.8 KiB
Python
Raw Normal View History

"""Helper to gather system info."""
2021-03-17 17:34:19 +00:00
from __future__ import annotations
from functools import cache
from getpass import getuser
import logging
import os
import platform
2021-03-17 17:34:19 +00:00
from typing import Any
from homeassistant.const import __version__ as current_version
from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass
from homeassistant.util.package import is_docker_env, is_virtual_env
_LOGGER = logging.getLogger(__name__)
@cache
def is_official_image() -> bool:
"""Return True if Home Assistant is running in an official container."""
return os.path.isfile("/OFFICIAL_IMAGE")
# 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)
@bind_hass
async def async_get_system_info(hass: HomeAssistant) -> dict[str, Any]:
"""Return info about the system."""
is_hassio = hass.components.hassio.is_hassio()
info_object = {
"installation_type": "Unknown",
2019-07-31 19:25:30 +00:00
"version": current_version,
"dev": "dev" in current_version,
"hassio": is_hassio,
2019-07-31 19:25:30 +00:00
"virtualenv": is_virtual_env(),
"python_version": platform.python_version(),
"docker": False,
"arch": platform.machine(),
"timezone": str(hass.config.time_zone),
"os_name": platform.system(),
2020-02-27 21:59:30 +00:00
"os_version": platform.release(),
}
2021-11-14 09:53:19 +00:00
try:
info_object["user"] = cached_get_user()
2021-11-14 09:53:19 +00:00
except KeyError:
info_object["user"] = None
2022-01-13 19:41:11 +00:00
if platform.system() == "Darwin":
2019-07-31 19:25:30 +00:00
info_object["os_version"] = platform.mac_ver()[0]
elif platform.system() == "Linux":
info_object["docker"] = is_docker_env()
# Determine installation type on current data
if info_object["docker"]:
if info_object["user"] == "root" and is_official_image():
info_object["installation_type"] = "Home Assistant Container"
else:
info_object["installation_type"] = "Unsupported Third Party Container"
elif is_virtual_env():
info_object["installation_type"] = "Home Assistant Core"
# Enrich with Supervisor information
if is_hassio:
if not (info := hass.components.hassio.get_info()):
_LOGGER.warning("No Home Assistant Supervisor info available")
info = {}
host = hass.components.hassio.get_host_info() or {}
info_object["supervisor"] = info.get("supervisor")
info_object["host_os"] = host.get("operating_system")
info_object["docker_version"] = info.get("docker")
2020-11-10 22:56:50 +00:00
info_object["chassis"] = host.get("chassis")
if info.get("hassos") is not None:
info_object["installation_type"] = "Home Assistant OS"
else:
info_object["installation_type"] = "Home Assistant Supervised"
return info_object