glances_api 0.4.3__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2023 Fabian Affolter <fabian@affolter-engineering.ch>
3
+ Copyright (c) 2017-2024 Fabian Affolter <fabian@affolter-engineering.ch>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
- Name: glances-api
3
- Version: 0.4.3
2
+ Name: glances_api
3
+ Version: 0.5.1
4
4
  Summary: Python API for interacting with Glances
5
5
  Home-page: https://github.com/home-assistant-ecosystem/python-glances-api
6
6
  License: MIT
7
7
  Author: Fabian Affolter
8
8
  Author-email: fabian@affolter-engineering.ch
9
- Requires-Python: >=3.9,<4.0
9
+ Requires-Python: >=3.11,<4.0
10
10
  Classifier: Development Status :: 3 - Alpha
11
11
  Classifier: Environment :: Console
12
12
  Classifier: Intended Audience :: Developers
@@ -15,9 +15,8 @@ Classifier: Operating System :: MacOS :: MacOS X
15
15
  Classifier: Operating System :: Microsoft :: Windows
16
16
  Classifier: Operating System :: POSIX
17
17
  Classifier: Programming Language :: Python :: 3
18
- Classifier: Programming Language :: Python :: 3.9
19
- Classifier: Programming Language :: Python :: 3.10
20
18
  Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
21
20
  Classifier: Topic :: Utilities
22
21
  Requires-Dist: httpx (>=0.23,<1)
23
22
  Project-URL: Repository, https://github.com/home-assistant-ecosystem/python-glances-api/releases
@@ -18,7 +18,7 @@ class Glances:
18
18
  self,
19
19
  host: str = "localhost",
20
20
  port: int = 61208,
21
- version: int = 2,
21
+ version: int = 3,
22
22
  ssl: bool = False,
23
23
  verify_ssl: bool = True,
24
24
  username: str | None = None,
@@ -26,11 +26,16 @@ class Glances:
26
26
  httpx_client: httpx.AsyncClient | None = None,
27
27
  ):
28
28
  """Initialize the connection."""
29
+ if version == 2:
30
+ _LOGGER.warning(
31
+ "Glances api older than v3 will not be supported in the next release."
32
+ )
33
+
29
34
  schema = "https" if ssl else "http"
30
35
  self.url = f"{schema}://{host}:{port}/api/{version}"
31
36
  self.data: dict[str, Any] = {}
32
37
  self.plugins: list[str] = []
33
- self.values: Any | None = None
38
+ self.values: dict[str, Any] | None = None
34
39
  self.username = username
35
40
  self.password = password
36
41
  self.verify_ssl = verify_ssl
@@ -40,7 +45,7 @@ class Glances:
40
45
  """Retrieve the data."""
41
46
  url = f"{self.url}/{endpoint}"
42
47
 
43
- httpx_client: Any = (
48
+ httpx_client = (
44
49
  self.httpx_client
45
50
  if self.httpx_client
46
51
  else httpx.AsyncClient(verify=self.verify_ssl)
@@ -50,12 +55,16 @@ class Glances:
50
55
  async with httpx_client as client:
51
56
  if self.password is None:
52
57
  response = await client.get(url)
53
- else:
58
+ elif self.username is not None:
54
59
  response = await client.get(
55
60
  url, auth=(self.username, self.password)
56
61
  )
57
- except (httpx.ConnectError, httpx.TimeoutException):
58
- raise exceptions.GlancesApiConnectionError(f"Connection to {url} failed")
62
+ else:
63
+ raise ValueError("username and password must be provided.")
64
+ except (httpx.ConnectError, httpx.TimeoutException) as err:
65
+ raise exceptions.GlancesApiConnectionError(
66
+ f"Connection to {url} failed"
67
+ ) from err
59
68
 
60
69
  if response.status_code == httpx.codes.UNAUTHORIZED:
61
70
  raise exceptions.GlancesApiAuthorizationError(
@@ -72,11 +81,11 @@ class Glances:
72
81
  self.data = response.json()
73
82
  elif endpoint == "pluginslist":
74
83
  self.plugins = response.json()
75
- except TypeError:
84
+ except TypeError as err:
76
85
  _LOGGER.error("Can not load data from Glances")
77
86
  raise exceptions.GlancesApiConnectionError(
78
87
  "Unable to get the data from Glances"
79
- )
88
+ ) from err
80
89
 
81
90
  async def get_metrics(self, element: str) -> None:
82
91
  """Get all the metrics for a monitored element."""
@@ -88,7 +97,7 @@ class Glances:
88
97
  else:
89
98
  raise exceptions.GlancesApiError("Element data not available")
90
99
 
91
- async def get_ha_sensor_data(self) -> dict[str, Any] | None:
100
+ async def get_ha_sensor_data(self) -> dict[str, Any]:
92
101
  """Create a dictionary with data for Home Assistant sensors."""
93
102
  await self.get_data("all")
94
103
 
@@ -135,18 +144,27 @@ class Glances:
135
144
  }
136
145
  if data := self.data.get("quicklook"):
137
146
  sensor_data["cpu"] = {"cpu_use_percent": data["cpu"]}
147
+ if data := self.data.get("percpu"):
148
+ sensor_data["percpu"] = {}
149
+ for cpu in data:
150
+ sensor_data["percpu"][str(cpu["cpu_number"])] = {
151
+ "cpu_use_percent": cpu["total"]
152
+ }
138
153
  if networks := self.data.get("network"):
139
154
  sensor_data["network"] = {}
140
155
  for network in networks:
141
156
  sensor_data["network"][network["interface_name"]] = {
142
- "is_up": network["is_up"],
157
+ "is_up": network.get("is_up"),
143
158
  "rx": round(network["rx"] / 1024, 1),
144
159
  "tx": round(network["tx"] / 1024, 1),
145
160
  "speed": round(network["speed"] / 1024**3, 1),
146
161
  }
147
- if "docker" in self.data and (data := self.data["docker"].get("containers")):
162
+ data = self.data.get("dockers") or self.data.get("containers")
163
+ if data and (containers_data := data.get("containers")):
148
164
  active_containers = [
149
- container for container in data if container["Status"] == "running"
165
+ container
166
+ for container in containers_data
167
+ if container["Status"] == "running"
150
168
  ]
151
169
  sensor_data["docker"] = {"docker_active": len(active_containers)}
152
170
  cpu_use = 0.0
@@ -161,4 +179,14 @@ class Glances:
161
179
  sensor_data["raid"] = data
162
180
  if data := self.data.get("uptime"):
163
181
  sensor_data["uptime"] = data
182
+ if data := self.data.get("gpu"):
183
+ sensor_data["gpu"] = {}
184
+ for sensor in data:
185
+ sensor_data["gpu"][f"GPU_{sensor['gpu_id']}__{sensor['name']}"] = {
186
+ "name": sensor["name"],
187
+ "temperature": sensor["temperature"],
188
+ "mem": sensor["mem"],
189
+ "proc": sensor["proc"],
190
+ "fan_speed": sensor["fan_speed"] if "fan_speed" in sensor else 0,
191
+ }
164
192
  return sensor_data
@@ -4,22 +4,14 @@
4
4
  class GlancesApiError(Exception):
5
5
  """General GlancesApiError exception occurred."""
6
6
 
7
- pass
8
-
9
7
 
10
8
  class GlancesApiConnectionError(GlancesApiError):
11
9
  """When a connection error is encountered."""
12
10
 
13
- pass
14
-
15
11
 
16
12
  class GlancesApiAuthorizationError(GlancesApiError):
17
13
  """When a connection error is encountered."""
18
14
 
19
- pass
20
-
21
15
 
22
16
  class GlancesApiNoDataAvailable(GlancesApiError):
23
17
  """When no data is available."""
24
-
25
- pass
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "glances_api"
3
- version = "0.4.3"
3
+ version = "0.5.1"
4
4
  description = "Python API for interacting with Glances"
5
5
  authors = ["Fabian Affolter <fabian@affolter-engineering.ch>"]
6
6
  homepage = "https://github.com/home-assistant-ecosystem/python-glances-api"
@@ -18,7 +18,7 @@ classifiers = [
18
18
  ]
19
19
 
20
20
  [tool.poetry.dependencies]
21
- python = "^3.9"
21
+ python = "^3.11"
22
22
  httpx = ">=0.23,<1"
23
23
 
24
24
  [tool.poetry.dev-dependencies]
File without changes