Skip to content

Commit

Permalink
Add sensor for gpu load
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Dec 13, 2022
1 parent f0a4e91 commit 64a4adf
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions custom_components/frigate/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ async def async_setup_entry(
elif key == "detectors":
for name in value.keys():
entities.append(DetectorSpeedSensor(coordinator, entry, name))
elif key == "gpus":
for name in value.keys():
entities.append(GpuLoadSensor(coordinator, entry, name))
elif key == "service":
# Temperature is only supported on PCIe Coral.
for name in value.get("temperatures", {}):
Expand Down Expand Up @@ -241,6 +244,73 @@ def icon(self) -> str:
return ICON_SPEEDOMETER


class GpuLoadSensor(FrigateEntity, CoordinatorEntity): # type: ignore[misc]
"""Frigate GPU Load class."""

_attr_entity_category = EntityCategory.DIAGNOSTIC

def __init__(
self,
coordinator: FrigateDataUpdateCoordinator,
config_entry: ConfigEntry,
gpu_name: str,
) -> None:
"""Construct a GpuLoadSensor."""
FrigateEntity.__init__(self, config_entry)
CoordinatorEntity.__init__(self, coordinator)
self._gpu_name = gpu_name
self._attr_entity_registry_enabled_default = False

@property
def unique_id(self) -> str:
"""Return a unique ID to use for this entity."""
return get_frigate_entity_unique_id(
self._config_entry.entry_id, "sensor_gpu_load", self._detector_name
)

@property
def device_info(self) -> DeviceInfo:
"""Get device information."""
return {
"identifiers": {get_frigate_device_identifier(self._config_entry)},
"name": NAME,
"model": self._get_model(),
"configuration_url": self._config_entry.data.get(CONF_URL),
"manufacturer": NAME,
}

@property
def name(self) -> str:
"""Return the name of the sensor."""
return f"{get_friendly_name(self._detector_name)} gpu load"

@property
def state(self) -> float | None:
"""Return the state of the sensor."""
if self.coordinator.data:
data = (
self.coordinator.data.get("gpus", {})
.get(self._gpu_name, {})
.get("gpu_usage")
)
if data is not None:
try:
return float(data.replace("%", ""))
except ValueError:
pass
return None

@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of the sensor."""
return "%"

@property
def icon(self) -> str:
"""Return the icon of the sensor."""
return ICON_SPEEDOMETER


class CameraFpsSensor(FrigateEntity, CoordinatorEntity): # type: ignore[misc]
"""Frigate Camera Fps class."""

Expand Down

0 comments on commit 64a4adf

Please sign in to comment.