glances_api 0.5.0__tar.gz → 0.6.0__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
2
  Name: glances_api
3
- Version: 0.5.0
3
+ Version: 0.6.0
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
@@ -1,4 +1,5 @@
1
1
  """Client to interact with the Glances API."""
2
+
2
3
  from __future__ import annotations
3
4
 
4
5
  import logging
@@ -35,7 +36,7 @@ class Glances:
35
36
  self.url = f"{schema}://{host}:{port}/api/{version}"
36
37
  self.data: dict[str, Any] = {}
37
38
  self.plugins: list[str] = []
38
- self.values: Any | None = None
39
+ self.values: dict[str, Any] | None = None
39
40
  self.username = username
40
41
  self.password = password
41
42
  self.verify_ssl = verify_ssl
@@ -45,7 +46,7 @@ class Glances:
45
46
  """Retrieve the data."""
46
47
  url = f"{self.url}/{endpoint}"
47
48
 
48
- httpx_client: Any = (
49
+ httpx_client = (
49
50
  self.httpx_client
50
51
  if self.httpx_client
51
52
  else httpx.AsyncClient(verify=self.verify_ssl)
@@ -55,12 +56,16 @@ class Glances:
55
56
  async with httpx_client as client:
56
57
  if self.password is None:
57
58
  response = await client.get(url)
58
- else:
59
+ elif self.username is not None:
59
60
  response = await client.get(
60
61
  url, auth=(self.username, self.password)
61
62
  )
62
- except (httpx.ConnectError, httpx.TimeoutException):
63
- raise exceptions.GlancesApiConnectionError(f"Connection to {url} failed")
63
+ else:
64
+ raise ValueError("username and password must be provided.")
65
+ except (httpx.ConnectError, httpx.TimeoutException) as err:
66
+ raise exceptions.GlancesApiConnectionError(
67
+ f"Connection to {url} failed"
68
+ ) from err
64
69
 
65
70
  if response.status_code == httpx.codes.UNAUTHORIZED:
66
71
  raise exceptions.GlancesApiAuthorizationError(
@@ -77,11 +82,11 @@ class Glances:
77
82
  self.data = response.json()
78
83
  elif endpoint == "pluginslist":
79
84
  self.plugins = response.json()
80
- except TypeError:
85
+ except TypeError as err:
81
86
  _LOGGER.error("Can not load data from Glances")
82
87
  raise exceptions.GlancesApiConnectionError(
83
88
  "Unable to get the data from Glances"
84
- )
89
+ ) from err
85
90
 
86
91
  async def get_metrics(self, element: str) -> None:
87
92
  """Get all the metrics for a monitored element."""
@@ -149,10 +154,11 @@ class Glances:
149
154
  if networks := self.data.get("network"):
150
155
  sensor_data["network"] = {}
151
156
  for network in networks:
157
+ time_since_update = network["time_since_update"]
152
158
  sensor_data["network"][network["interface_name"]] = {
153
- "is_up": network["is_up"],
154
- "rx": round(network["rx"] / 1024, 1),
155
- "tx": round(network["tx"] / 1024, 1),
159
+ "is_up": network.get("is_up"),
160
+ "rx": round(network["rx"] / time_since_update),
161
+ "tx": round(network["tx"] / time_since_update),
156
162
  "speed": round(network["speed"] / 1024**3, 1),
157
163
  }
158
164
  data = self.data.get("dockers") or self.data.get("containers")
@@ -178,11 +184,18 @@ class Glances:
178
184
  if data := self.data.get("gpu"):
179
185
  sensor_data["gpu"] = {}
180
186
  for sensor in data:
181
- sensor_data["gpu"][f"GPU_{sensor['gpu_id']}__{sensor['name']}"] = {
182
- "name": sensor["name"],
183
- "temperature": sensor["temperature"],
184
- "mem": sensor["mem"],
185
- "proc": sensor["proc"],
186
- "fan_speed": sensor["fan_speed"] if "fan_speed" in sensor else 0,
187
+ sensor_data["gpu"][f"{sensor['name']} (GPU {sensor['gpu_id']})"] = {
188
+ "temperature": sensor.get("temperature", 0),
189
+ "mem": sensor.get("mem", 0),
190
+ "proc": sensor.get("proc", 0),
191
+ "fan_speed": sensor.get("fan_speed", 0),
192
+ }
193
+ if data := self.data.get("diskio"):
194
+ sensor_data["diskio"] = {}
195
+ for disk in data:
196
+ time_since_update = disk["time_since_update"]
197
+ sensor_data["diskio"][disk["disk_name"]] = {
198
+ "read": round(disk["read_bytes"] / time_since_update),
199
+ "write": round(disk["write_bytes"] / time_since_update),
187
200
  }
188
201
  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.5.0"
3
+ version = "0.6.0"
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,12 +18,12 @@ 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]
25
- black = "^23.3"
26
- pytest = "^7.3.0"
25
+ black = "^24.3"
26
+ pytest = "^8"
27
27
  pytest-httpx = ">0.15,<1"
28
28
  pytest-asyncio = "^0.16.0"
29
29
  isort = "^5.10.0"
File without changes