Home Assistant Unofficial Reference 2024.12.1
system_health.py
Go to the documentation of this file.
1 """Provide info to system health."""
2 
3 from __future__ import annotations
4 
5 import os
6 from typing import Any
7 
8 from homeassistant.components import system_health
9 from homeassistant.core import HomeAssistant, callback
10 
11 from .coordinator import (
12  get_host_info,
13  get_info,
14  get_network_info,
15  get_os_info,
16  get_supervisor_info,
17 )
18 
19 SUPERVISOR_PING = "http://{ip_address}/supervisor/ping"
20 OBSERVER_URL = "http://{ip_address}:4357"
21 
22 
23 @callback
25  hass: HomeAssistant, register: system_health.SystemHealthRegistration
26 ) -> None:
27  """Register system health callbacks."""
28  register.async_register_info(system_health_info)
29 
30 
31 async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
32  """Get info for the info page."""
33  ip_address = os.environ["SUPERVISOR"]
34  info = get_info(hass) or {}
35  host_info = get_host_info(hass) or {}
36  supervisor_info = get_supervisor_info(hass)
37  network_info = get_network_info(hass) or {}
38 
39  healthy: bool | dict[str, str]
40  if supervisor_info is not None and supervisor_info.get("healthy"):
41  healthy = True
42  else:
43  healthy = {
44  "type": "failed",
45  "error": "Unhealthy",
46  }
47 
48  supported: bool | dict[str, str]
49  if supervisor_info is not None and supervisor_info.get("supported"):
50  supported = True
51  else:
52  supported = {
53  "type": "failed",
54  "error": "Unsupported",
55  }
56 
57  information = {
58  "host_os": host_info.get("operating_system"),
59  "update_channel": info.get("channel"),
60  "supervisor_version": f"supervisor-{info.get('supervisor')}",
61  "agent_version": host_info.get("agent_version"),
62  "docker_version": info.get("docker"),
63  "disk_total": f"{host_info.get('disk_total')} GB",
64  "disk_used": f"{host_info.get('disk_used')} GB",
65  "healthy": healthy,
66  "supported": supported,
67  "host_connectivity": network_info.get("host_internet"),
68  "supervisor_connectivity": network_info.get("supervisor_internet"),
69  "ntp_synchronized": host_info.get("dt_synchronized"),
70  "virtualization": host_info.get("virtualization"),
71  }
72 
73  if info.get("hassos") is not None:
74  os_info = get_os_info(hass) or {}
75  information["board"] = os_info.get("board")
76 
77  information["supervisor_api"] = system_health.async_check_can_reach_url(
78  hass,
79  SUPERVISOR_PING.format(ip_address=ip_address),
80  OBSERVER_URL.format(ip_address=ip_address),
81  )
82  information["version_api"] = system_health.async_check_can_reach_url(
83  hass,
84  f"https://version.home-assistant.io/{info.get('channel')}.json",
85  )
86 
87  information["installed_addons"] = ", ".join(
88  f"{addon['name']} ({addon['version']})"
89  for addon in (supervisor_info or {}).get("addons", [])
90  )
91 
92  return information
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
dict[str, Any]|None get_supervisor_info(HomeAssistant hass)
Definition: coordinator.py:99
dict[str, Any]|None get_info(HomeAssistant hass)
Definition: coordinator.py:69
dict[str, Any]|None get_host_info(HomeAssistant hass)
Definition: coordinator.py:79
dict[str, Any]|None get_network_info(HomeAssistant hass)
Definition: coordinator.py:109
dict[str, Any]|None get_os_info(HomeAssistant hass)
Definition: coordinator.py:169
None async_register(HomeAssistant hass, system_health.SystemHealthRegistration register)
dict[str, Any] system_health_info(HomeAssistant hass)