Skip to content

Commit

Permalink
Add Glances network sensors (home-assistant#114546)
Browse files Browse the repository at this point in the history
* Add Glances network sensors

* Add unit test for network and remove unused MOCK_DATA

* Add network icons
  • Loading branch information
wittypluck authored Apr 7, 2024
1 parent 32d62e4 commit 63d43a9
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 163 deletions.
6 changes: 6 additions & 0 deletions homeassistant/components/glances/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
},
"gpu_memory_usage": {
"default": "mdi:memory"
},
"network_rx": {
"default": "mdi:transmission-tower"
},
"network_tx": {
"default": "mdi:transmission-tower"
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion homeassistant/components/glances/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ class GlancesSensorEntityDescription(SensorEntityDescription):
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
("network", "rx"): GlancesSensorEntityDescription(
key="rx",
type="network",
translation_key="network_rx",
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT,
),
("network", "tx"): GlancesSensorEntityDescription(
key="tx",
type="network",
translation_key="network_tx",
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT,
),
}


Expand All @@ -279,7 +297,7 @@ async def async_setup_entry(
entities: list[GlancesSensor] = []

for sensor_type, sensors in coordinator.data.items():
if sensor_type in ["fs", "diskio", "sensors", "raid", "gpu"]:
if sensor_type in ["fs", "diskio", "sensors", "raid", "gpu", "network"]:
entities.extend(
GlancesSensor(
coordinator,
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/glances/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
},
"gpu_processor_usage": {
"name": "{sensor_label} processor usage"
},
"network_rx": {
"name": "{sensor_label} RX"
},
"network_tx": {
"name": "{sensor_label} TX"
}
}
},
Expand Down
167 changes: 5 additions & 162 deletions tests/components/glances/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,168 +12,6 @@
"verify_ssl": True,
}

MOCK_DATA = {
"cpu": {
"total": 10.6,
"user": 7.6,
"system": 2.1,
"idle": 88.8,
"nice": 0.0,
"iowait": 0.6,
},
"diskio": [
{
"time_since_update": 1,
"disk_name": "nvme0n1",
"read_count": 12,
"write_count": 466,
"read_bytes": 184320,
"write_bytes": 23863296,
"key": "disk_name",
},
],
"docker": {
"containers": [
{
"key": "name",
"name": "container1",
"Status": "running",
"cpu": {"total": 50.94973493230174},
"cpu_percent": 50.94973493230174,
"memory": {
"usage": 1120321536,
"limit": 3976318976,
"rss": 480641024,
"cache": 580915200,
"max_usage": 1309597696,
},
"memory_usage": 539406336,
},
{
"key": "name",
"name": "container2",
"Status": "running",
"cpu": {"total": 26.23567931034483},
"cpu_percent": 26.23567931034483,
"memory": {
"usage": 85139456,
"limit": 3976318976,
"rss": 33677312,
"cache": 35012608,
"max_usage": 87650304,
},
"memory_usage": 50126848,
},
]
},
"fs": [
{
"device_name": "/dev/sda8",
"fs_type": "ext4",
"mnt_point": "/ssl",
"size": 511320748032,
"used": 32910458880,
"free": 457917374464,
"percent": 6.7,
"key": "mnt_point",
},
{
"device_name": "/dev/sda8",
"fs_type": "ext4",
"mnt_point": "/media",
"size": 511320748032,
"used": 32910458880,
"free": 457917374464,
"percent": 6.7,
"key": "mnt_point",
},
],
"mem": {
"total": 3976318976,
"available": 2878337024,
"percent": 27.6,
"used": 1097981952,
"free": 2878337024,
"active": 567971840,
"inactive": 1679704064,
"buffers": 149807104,
"cached": 1334816768,
"shared": 1499136,
},
"sensors": [
{
"label": "cpu_thermal 1",
"value": 59,
"warning": None,
"critical": None,
"unit": "C",
"type": "temperature_core",
"key": "label",
},
{
"label": "err_temp",
"value": "ERR",
"warning": None,
"critical": None,
"unit": "C",
"type": "temperature_hdd",
"key": "label",
},
{
"label": "na_temp",
"value": "NA",
"warning": None,
"critical": None,
"unit": "C",
"type": "temperature_hdd",
"key": "label",
},
],
"system": {
"os_name": "Linux",
"hostname": "fedora-35",
"platform": "64bit",
"linux_distro": "Fedora Linux 35",
"os_version": "5.15.6-200.fc35.x86_64",
"hr_name": "Fedora Linux 35 64bit",
},
"raid": {
"md3": {
"status": "active",
"type": "raid1",
"components": {"sdh1": "2", "sdi1": "0"},
"available": "2",
"used": "2",
"config": "UU",
},
"md1": {
"status": "active",
"type": "raid1",
"components": {"sdg": "0", "sde": "1"},
"available": "2",
"used": "2",
"config": "UU",
},
"md4": {
"status": "active",
"type": "raid1",
"components": {"sdf1": "1", "sdb1": "0"},
"available": "2",
"used": "2",
"config": "UU",
},
"md0": {
"status": "active",
"type": "raid1",
"components": {"sdc": "2", "sdd": "3"},
"available": "2",
"used": "2",
"config": "UU",
},
},
"uptime": "3 days, 10:25:20",
}

MOCK_REFERENCE_DATE: datetime = datetime.fromisoformat("2024-02-13T14:13:12")

HA_SENSOR_DATA: dict[str, Any] = {
Expand Down Expand Up @@ -214,6 +52,11 @@
"config": "UU",
},
},
"network": {
"lo": {"is_up": True, "rx": 7646, "tx": 7646, "speed": 0.0},
"dummy0": {"is_up": False, "rx": 0.0, "tx": 0.0, "speed": 0.0},
"eth0": {"is_up": True, "rx": 3953, "tx": 5995, "speed": 9.8},
},
"uptime": "3 days, 10:25:20",
"gpu": {
"NVIDIA GeForce RTX 3080 (GPU 0)": {
Expand Down
Loading

0 comments on commit 63d43a9

Please sign in to comment.