Skip to content
Open
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
2 changes: 1 addition & 1 deletion homeassistant/components/librenms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .coordinator import LibrenmsConfigEntry, LibrenmsDataUpdateCoordinator

PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR]
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: LibrenmsConfigEntry) -> bool:
Expand Down
26 changes: 24 additions & 2 deletions homeassistant/components/librenms/entity.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Base entity for the LibreNMS integration."""
"""Base entities for the LibreNMS integration."""

from typing import override

from aiolibrenms.devices.models import LibrenmsDeviceInfo

from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
Expand Down Expand Up @@ -53,3 +53,25 @@ def available(self) -> bool:
def _data(self) -> LibrenmsDeviceInfo:
"""Get DeviceInfo from coordinator."""
return self.coordinator.data.devices[self.device_id]


class LibrenmsSystemEntity(CoordinatorEntity[LibrenmsDataUpdateCoordinator]):
"""Define LibreNMS base entity."""

_attr_has_entity_name = True

def __init__(
self,
coordinator: LibrenmsDataUpdateCoordinator,
) -> None:
"""Initialize."""
super().__init__(coordinator)

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
manufacturer="LibreNMS",
sw_version=coordinator.data.system.local_ver,
entry_type=DeviceEntryType.SERVICE,
configuration_url=coordinator.configuration_url,
name="LibreNMS",
)
21 changes: 21 additions & 0 deletions homeassistant/components/librenms/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"entity": {
"sensor": {
"database_version": {
"default": "mdi:database"
},
"netsnmp_version": {
"default": "mdi:network-outline"
},
"php_version": {
"default": "mdi:language-php"
},
"python_version": {
"default": "mdi:language-python"
},
"rrdtool_version": {
"default": "mdi:database-clock"
}
}
}
}
114 changes: 114 additions & 0 deletions homeassistant/components/librenms/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Sensor platform for the LibreNMS integration."""

from collections.abc import Callable
from dataclasses import dataclass
from typing import override

from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType

from .coordinator import (
LibrenmsConfigEntry,
LibrenmsData,
LibrenmsDataUpdateCoordinator,
)
from .entity import LibrenmsSystemEntity

# Coordinator is used to centralize the data updates
PARALLEL_UPDATES = 0


@dataclass(frozen=True, kw_only=True)
class LibrenmsSystemSensorEntityDescription(SensorEntityDescription):
"""Librenms system sensor entity description."""

value: Callable[[LibrenmsData], StateType]
is_suitable: Callable[[LibrenmsData], bool] = lambda _: True


SYSTEM_SENSOR_TYPES: tuple[LibrenmsSystemSensorEntityDescription, ...] = (
LibrenmsSystemSensorEntityDescription(
key="device_count",
translation_key="device_count",
state_class=SensorStateClass.MEASUREMENT,
value=lambda data: len(data.devices),
),
LibrenmsSystemSensorEntityDescription(
key="database_version",
translation_key="database_version",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda data: data.system.database_ver,
),
LibrenmsSystemSensorEntityDescription(
key="netsnmp_version",
translation_key="netsnmp_version",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda data: data.system.netsnmp_ver,
),
LibrenmsSystemSensorEntityDescription(
key="php_version",
translation_key="php_version",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda data: data.system.php_ver,
),
LibrenmsSystemSensorEntityDescription(
key="python_version",
translation_key="python_version",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda data: data.system.python_ver,
),
LibrenmsSystemSensorEntityDescription(
key="rrdtool_version",
translation_key="rrdtool_version",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda data: data.system.rrdtool_ver,
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: LibrenmsConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Add LibreNMS server state sensors."""
coordinator = entry.runtime_data
async_add_entities(
LibrenmsSystemSensorEntity(coordinator, description)
for description in SYSTEM_SENSOR_TYPES
if description.is_suitable(coordinator.data)
)


class LibrenmsSystemSensorEntity(LibrenmsSystemEntity, SensorEntity):
"""Define Librenms sensor entity."""

entity_description: LibrenmsSystemSensorEntityDescription

def __init__(
self,
coordinator: LibrenmsDataUpdateCoordinator,
description: LibrenmsSystemSensorEntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}"
self.entity_description = description

@property
@override
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
return self.entity_description.value(self.coordinator.data)
10 changes: 10 additions & 0 deletions homeassistant/components/librenms/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
"status": {
"name": "Status"
}
},
"sensor": {
"database_version": { "name": "Database version" },
"device_count": {
"name": "Total device count"
},
"netsnmp_version": { "name": "NetSNMP version" },
"php_version": { "name": "PHP version" },
"python_version": { "name": "Python version" },
"rrdtool_version": { "name": "RRDTool version" }
}
},
"exceptions": {
Expand Down
Loading
Loading