Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group sensors by type #98

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions custom_components/proxmoxve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def device_info(
DOMAIN,
f"{config_entry.entry_id}_{ProxmoxType.Node.upper()}_{node}",
)
default_model = api_category.upper()
model = api_category.upper()

elif api_category is ProxmoxType.Node:
coordinator = coordinators[node]
Expand All @@ -514,7 +514,7 @@ def device_info(
identifier = f"{config_entry.entry_id}_{api_category.upper()}_{node}"
url = f"https://{host}:{port}/#v1:0:=node/{node}"
via_device = ("", "")
default_model = model_processor
model = model_processor

if create:
device_registry = dr.async_get(hass)
Expand All @@ -523,9 +523,9 @@ def device_info(
entry_type=dr.DeviceEntryType.SERVICE,
configuration_url=url,
identifiers={(DOMAIN, identifier)},
default_manufacturer="Proxmox VE",
manufacturer="Proxmox VE",
name=name,
default_model=default_model,
model=model,
sw_version=proxmox_version,
hw_version=None,
via_device=via_device,
Expand All @@ -534,9 +534,9 @@ def device_info(
entry_type=dr.DeviceEntryType.SERVICE,
configuration_url=url,
identifiers={(DOMAIN, identifier)},
default_manufacturer="Proxmox VE",
manufacturer="Proxmox VE",
name=name,
default_model=default_model,
model=model,
sw_version=proxmox_version,
hw_version=None,
via_device=via_device,
Expand Down
251 changes: 55 additions & 196 deletions custom_components/proxmoxve/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
api_category: ProxmoxType | None = None # Set when the sensor applies to only QEMU or LXC, if None applies to both.


PROXMOX_SENSOR_NODES: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.CPU,
name="CPU used",
icon="mdi:cpu-64-bit",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
PROXMOX_SENSOR_DISK: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key="disk_free",
name="Disk free",
Expand Down Expand Up @@ -110,6 +101,8 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
)
PROXMOX_SENSOR_MEMORY: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.MEMORY_FREE,
name="Memory free",
Expand Down Expand Up @@ -166,6 +159,8 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
)
PROXMOX_SENSOR_SWAP: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.SWAP_FREE,
name="Swap free",
Expand All @@ -174,7 +169,8 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key="swap_free_perc",
Expand All @@ -195,20 +191,20 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key="swap_used",
key=ProxmoxKeyAPIParse.SWAP_USED,
name="Swap used",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
),
),
ProxmoxSensorEntityDescription(
key="swap_used_perc",
name="Swap used percentage",
Expand All @@ -218,7 +214,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
value_fn=lambda x: (x.swap_used / x.swap_total) if x.swap_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
entity_registry_enabled_default=False,
),
)
PROXMOX_SENSOR_UPTIME: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.UPTIME,
name="Uptime",
Expand All @@ -229,124 +228,7 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
device_class=SensorDeviceClass.TIMESTAMP,
),
)


PROXMOX_SENSOR_VM: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.CPU,
name="CPU used",
icon="mdi:cpu-64-bit",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x >= 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
ProxmoxSensorEntityDescription(
key="disk_free",
name="Disk free",
icon="mdi:harddisk",
native_unit_of_measurement=UnitOfInformation.BYTES,
value_fn=lambda x: (x.disk_total - x.disk_used),
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key="disk_free_perc",
name="Disk free percentage",
icon="mdi:harddisk",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: 1 - x.disk_used / x.disk_total if x.disk_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.DISK_TOTAL,
name="Disk total",
icon="mdi:harddisk-plus",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.DISK_USED,
name="Disk used",
icon="mdi:harddisk",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key="disk_used_perc",
name="Disk used percentage",
icon="mdi:harddisk",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: x.disk_used / x.disk_total if x.disk_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.MEMORY_FREE,
name="Memory free",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
ProxmoxSensorEntityDescription(
key="memory_free_perc",
name="Memory free percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: x.memory_free / x.memory_total if x.memory_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.MEMORY_TOTAL,
name="Memory total",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.MEMORY_USED,
name="Memory used",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
ProxmoxSensorEntityDescription(
key="memory_used_perc",
name="Memory used percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: x.memory_used / x.memory_total if x.memory_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
PROXMOX_SENSOR_NETWORK: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.NETWORK_IN,
name="Network in",
Expand All @@ -369,75 +251,52 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
),
)
PROXMOX_SENSOR_CPU: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key="node",
name="Node",
icon="mdi:server",
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.UPTIME,
name="Uptime",
icon="mdi:database-clock-outline",
conversion_fn=lambda x: (
dt_util.utcnow() - timedelta(seconds=x) if x > 0 else None
),
device_class=SensorDeviceClass.TIMESTAMP,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.SWAP_FREE,
name="Swap free",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key="swap_free_perc",
name="Swap free percentage",
icon="mdi:memory",
key=ProxmoxKeyAPIParse.CPU,
name="CPU used",
icon="mdi:cpu-64-bit",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: (x.swap_free / x.swap_total) if x.swap_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
entity_registry_enabled_default=False,
),
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.SWAP_TOTAL,
name="Swap total",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
conversion_fn=lambda x: (x * 100) if x >= 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
suggested_display_precision=1,
),
)

PROXMOX_SENSOR_NODES: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
*PROXMOX_SENSOR_CPU,
*PROXMOX_SENSOR_DISK,
*PROXMOX_SENSOR_MEMORY,
*PROXMOX_SENSOR_SWAP,
*PROXMOX_SENSOR_UPTIME,
)

PROXMOX_SENSOR_QEMU: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key=ProxmoxKeyAPIParse.SWAP_USED,
name="Swap used",
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
entity_registry_enabled_default=False,
key="node",
name="Node",
icon="mdi:server",
),
*PROXMOX_SENSOR_CPU,
*PROXMOX_SENSOR_DISK,
*PROXMOX_SENSOR_MEMORY,
*PROXMOX_SENSOR_NETWORK,
*PROXMOX_SENSOR_UPTIME,
)

PROXMOX_SENSOR_LXC: Final[tuple[ProxmoxSensorEntityDescription, ...]] = (
ProxmoxSensorEntityDescription(
key="swap_used_perc",
name="Swap used percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: (x.swap_used / x.swap_total) if x.swap_total > 0 else 0,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
entity_registry_enabled_default=False,
key="node",
name="Node",
icon="mdi:server",
),
*PROXMOX_SENSOR_CPU,
*PROXMOX_SENSOR_DISK,
*PROXMOX_SENSOR_MEMORY,
*PROXMOX_SENSOR_NETWORK,
*PROXMOX_SENSOR_SWAP,
*PROXMOX_SENSOR_UPTIME,
)


Expand Down Expand Up @@ -477,7 +336,7 @@ async def async_setup_entry(
# unfound vm case
if coordinator.data is None:
continue
for description in PROXMOX_SENSOR_VM:
for description in PROXMOX_SENSOR_QEMU:
if description.api_category in (None, ProxmoxType.QEMU):
sensors.append(
create_sensor(
Expand All @@ -499,7 +358,7 @@ async def async_setup_entry(
# unfound container case
if coordinator.data is None:
continue
for description in PROXMOX_SENSOR_VM:
for description in PROXMOX_SENSOR_LXC:
if description.api_category in (None, ProxmoxType.LXC):
sensors.append(
create_sensor(
Expand Down
Loading