From 3f2e18fe1787f2fe9ac4a42f1ac58b9eede58984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 3 Aug 2021 11:58:27 +0200 Subject: [PATCH] Add user to homeassistant system health (#53902) --- .../components/homeassistant/strings.json | 3 ++- .../components/homeassistant/system_health.py | 1 + homeassistant/helpers/system_info.py | 5 ++++- tests/helpers/test_system_info.py | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json index 7da4a5a9d8a..09be9283b5c 100644 --- a/homeassistant/components/homeassistant/strings.json +++ b/homeassistant/components/homeassistant/strings.json @@ -4,6 +4,7 @@ "arch": "CPU Architecture", "dev": "Development", "docker": "Docker", + "user": "User", "hassio": "Supervisor", "installation_type": "Installation Type", "os_name": "Operating System Family", @@ -14,4 +15,4 @@ "virtualenv": "Virtual Environment" } } -} +} \ No newline at end of file diff --git a/homeassistant/components/homeassistant/system_health.py b/homeassistant/components/homeassistant/system_health.py index ff3562a24f9..f13278ddfeb 100644 --- a/homeassistant/components/homeassistant/system_health.py +++ b/homeassistant/components/homeassistant/system_health.py @@ -22,6 +22,7 @@ async def system_health_info(hass): "dev": info.get("dev"), "hassio": info.get("hassio"), "docker": info.get("docker"), + "user": info.get("user"), "virtualenv": info.get("virtualenv"), "python_version": info.get("python_version"), "os_name": info.get("os_name"), diff --git a/homeassistant/helpers/system_info.py b/homeassistant/helpers/system_info.py index 6d6c912f8c9..766fa90af96 100644 --- a/homeassistant/helpers/system_info.py +++ b/homeassistant/helpers/system_info.py @@ -1,6 +1,7 @@ """Helper to gather system info.""" from __future__ import annotations +from getpass import getuser import os import platform from typing import Any @@ -22,6 +23,7 @@ async def async_get_system_info(hass: HomeAssistant) -> dict[str, Any]: "virtualenv": is_virtual_env(), "python_version": platform.python_version(), "docker": False, + "user": getuser(), "arch": platform.machine(), "timezone": str(hass.config.time_zone), "os_name": platform.system(), @@ -37,7 +39,8 @@ async def async_get_system_info(hass: HomeAssistant) -> dict[str, Any]: # Determine installation type on current data if info_object["docker"]: - info_object["installation_type"] = "Home Assistant Container" + if info_object["user"] == "root": + info_object["installation_type"] = "Home Assistant Container" elif is_virtual_env(): info_object["installation_type"] = "Home Assistant Core" diff --git a/tests/helpers/test_system_info.py b/tests/helpers/test_system_info.py index e27114c1a13..fd9d488596f 100644 --- a/tests/helpers/test_system_info.py +++ b/tests/helpers/test_system_info.py @@ -1,5 +1,6 @@ """Tests for the system info helper.""" import json +from unittest.mock import patch from homeassistant.const import __version__ as current_version @@ -9,4 +10,20 @@ async def test_get_system_info(hass): info = await hass.helpers.system_info.async_get_system_info() assert isinstance(info, dict) assert info["version"] == current_version + assert info["user"] is not None assert json.dumps(info) is not None + + +async def test_container_installationtype(hass): + """Test container installation type.""" + with patch("platform.system", return_value="Linux"), patch( + "os.path.isfile", return_value=True + ): + info = await hass.helpers.system_info.async_get_system_info() + assert info["installation_type"] == "Home Assistant Container" + + with patch("platform.system", return_value="Linux"), patch( + "os.path.isfile", return_value=True + ), patch("homeassistant.helpers.system_info.getuser", return_value="user"): + info = await hass.helpers.system_info.async_get_system_info() + assert info["installation_type"] == "Unknown"