From 3b0d0e9928a54948c5a5543236e37ea075be2a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Fri, 14 Aug 2020 09:24:38 +0200 Subject: [PATCH] Add disk total, used, free to host/info API (#1909) --- API.md | 15 +++++++++------ supervisor/api/host.py | 10 ++++++++-- supervisor/const.py | 3 +++ supervisor/host/info.py | 14 ++++++++++++++ supervisor/misc/hardware.py | 10 ++++++++++ tests/misc/test_hardware.py | 18 ++++++++++++++++++ 6 files changed, 62 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index 00ab200de..17e41db45 100644 --- a/API.md +++ b/API.md @@ -25,7 +25,7 @@ On success / Code 200: To access the API you need use an authorization header with a `Bearer` token. -The token is available for add-ons and Home Assistant using the +The token is available for add-ons and Home Assistant using the `SUPERVISOR_TOKEN` environment variable. ### Supervisor @@ -242,13 +242,16 @@ return: ```json { - "hostname": "hostname|null", - "features": ["shutdown", "reboot", "hostname", "services", "hassos"], - "operating_system": "HassOS XY|Ubuntu 16.4|null", - "kernel": "4.15.7|null", "chassis": "specific|null", + "cpe": "xy|null", "deployment": "stable|beta|dev|null", - "cpe": "xy|null" + "disk_total": 32.0, + "disk_used": 30.0, + "disk_free": 2.0, + "features": ["shutdown", "reboot", "hostname", "services", "hassos"], + "hostname": "hostname|null", + "kernel": "4.15.7|null", + "operating_system": "HassOS XY|Ubuntu 16.4|null" } ``` diff --git a/supervisor/api/host.py b/supervisor/api/host.py index b5be7fe2f..d463fcf83 100644 --- a/supervisor/api/host.py +++ b/supervisor/api/host.py @@ -11,6 +11,9 @@ from ..const import ( ATTR_CPE, ATTR_DEPLOYMENT, ATTR_DESCRIPTON, + ATTR_DISK_FREE, + ATTR_DISK_TOTAL, + ATTR_DISK_USED, ATTR_FEATURES, ATTR_HOSTNAME, ATTR_KERNEL, @@ -39,11 +42,14 @@ class APIHost(CoreSysAttributes): return { ATTR_CHASSIS: self.sys_host.info.chassis, ATTR_CPE: self.sys_host.info.cpe, + ATTR_DEPLOYMENT: self.sys_host.info.deployment, + ATTR_DISK_FREE: self.sys_host.info.free_space, + ATTR_DISK_TOTAL: self.sys_host.info.total_space, + ATTR_DISK_USED: self.sys_host.info.used_space, ATTR_FEATURES: self.sys_host.supported_features, ATTR_HOSTNAME: self.sys_host.info.hostname, - ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system, - ATTR_DEPLOYMENT: self.sys_host.info.deployment, ATTR_KERNEL: self.sys_host.info.kernel, + ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system, } @api_process diff --git a/supervisor/const.py b/supervisor/const.py index 4d4339ee1..0cbfd4ed3 100644 --- a/supervisor/const.py +++ b/supervisor/const.py @@ -164,6 +164,9 @@ ATTR_AUDIO_OUTPUT = "audio_output" ATTR_INPUT = "input" ATTR_OUTPUT = "output" ATTR_DISK = "disk" +ATTR_DISK_FREE = "disk_free" +ATTR_DISK_TOTAL = "disk_total" +ATTR_DISK_USED = "disk_used" ATTR_SERIAL = "serial" ATTR_SECURITY = "security" ATTR_BUILD_FROM = "build_from" diff --git a/supervisor/host/info.py b/supervisor/host/info.py index 66bcb05f3..26569b213 100644 --- a/supervisor/host/info.py +++ b/supervisor/host/info.py @@ -51,6 +51,20 @@ class InfoCenter(CoreSysAttributes): """Return local CPE.""" return self.sys_dbus.hostname.cpe + @property + def total_space(self) -> float: + """Return total space (GiB) on disk for supervisor data directory.""" + return self.coresys.hardware.get_disk_total_space( + self.coresys.config.path_supervisor + ) + + @property + def used_space(self) -> float: + """Return used space (GiB) on disk for supervisor data directory.""" + return self.coresys.hardware.get_disk_used_space( + self.coresys.config.path_supervisor + ) + @property def free_space(self) -> float: """Return available space (GiB) on disk for supervisor data directory.""" diff --git a/supervisor/misc/hardware.py b/supervisor/misc/hardware.py index addd90065..df3f95b29 100644 --- a/supervisor/misc/hardware.py +++ b/supervisor/misc/hardware.py @@ -196,6 +196,16 @@ class Hardware: return datetime.utcfromtimestamp(int(found.group(1))) + def get_disk_total_space(self, path: Union[str, Path]) -> float: + """Return total space (GiB) on disk for path.""" + total, _, _ = shutil.disk_usage(path) + return round(total / (1024.0 ** 3), 1) + + def get_disk_used_space(self, path: Union[str, Path]) -> float: + """Return used space (GiB) on disk for path.""" + _, used, _ = shutil.disk_usage(path) + return round(used / (1024.0 ** 3), 1) + def get_disk_free_space(self, path: Union[str, Path]) -> float: """Return free space (GiB) on disk for path.""" _, _, free = shutil.disk_usage(path) diff --git a/tests/misc/test_hardware.py b/tests/misc/test_hardware.py index cbe2eda30..6c0f63d6f 100644 --- a/tests/misc/test_hardware.py +++ b/tests/misc/test_hardware.py @@ -41,3 +41,21 @@ def test_free_space(): free = system.get_disk_free_space("/data") assert free == 2.0 + + +def test_total_space(): + """Test total space helper.""" + system = Hardware() + with patch("shutil.disk_usage", return_value=(10 * (1024.0 ** 3), 42, 42)): + total = system.get_disk_total_space("/data") + + assert total == 10.0 + + +def test_used_space(): + """Test used space helper.""" + system = Hardware() + with patch("shutil.disk_usage", return_value=(42, 8 * (1024.0 ** 3), 42)): + used = system.get_disk_used_space("/data") + + assert used == 8.0